gpt4 book ai didi

c - 不知道如何初始化这个局部变量

转载 作者:行者123 更新时间:2023-11-30 15:38:24 25 4
gpt4 key购买 nike

我是c初学者,我无法初始化它局部变量 dotP。当我运行代码时它给出我遇到这个错误:错误 C4700:使用了未初始化的局部变量“dotP”。请告诉我代码以及在哪里插入初始化代码。任何其他更正将不胜感激。

 #include<stdio.h>
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

#define N 10
float AUX;


typedef struct complex{
double a;
double b;
}complex;


double randN(double min, double max){

double range = (max - min);
double div = max / range;
return min + (rand() / div);

}

void fillVector(complex* v1, complex* v2){
int i = 0;


for (i = 0; i< N; i++){
v1[i].a = randN(0, 100);
v1[i].b = randN(0, 100);
v2[i].a = randN(0, 100);
v2[i].b = randN(0, 100);
}
}


void calculate(complex* v1, complex* v2){
int i = 0, j = 0;


complex dotP;
complex c1, c2;

for (i = 0; i< N; i++){
for (j = 0; j <N; j++){
c1.a = v1[i].a;
c1.b = v1[i].b;
c2.a = v2[j].a; c2.b = v2[j].b;
//


dotP.a += ((c1.a*c2.a) - (c1.b*c2.b)); /* take care of real part */
dotP.b += ((c1.a*c2.b) + (c1.b*c2.a)); /*take care of imaginary */
}
}
}


int main()
{

clock_t begin, end;
double time_spent;


complex* v1 = (complex *)malloc(sizeof(complex)*N);
complex* v2 = (complex *)malloc(sizeof(complex)*N);

begin = clock(); /* init clock */
calculate(v1, v2); /*consume time */
end = clock(); /*stop clock */
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Elapsed: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);/*showdata*/

return 0;
}

最佳答案

或者:

complex dotP;
...
dotP.a = 0.0;
dotP.b = 0.0;

或者

complex dotP = {0.0, 0.0};

关于c - 不知道如何初始化这个局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21765859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com