我对c很新,而我正在尝试做的是学习并使用malloc和realloc。我的程序采用整数x输入,然后循环直到满足x,同时还采用其他整数输入。然后我做了各种计算。但是当我知道它应该是别的东西时,我的输出是0。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void)
{
calculations();
}
void calculations()
{
int i;
int x;
int total = 0;
double squareRoot;
double overall;
scanf("%d", &x);
int* array = malloc(x * sizeof(int));
if (!array) {
printf("There isn't enough memory \n");
return;
}
int c = 0;
while (c < x) {
scanf("%d", &array[c]);
total += array[c] * array[c];
c++;
}
squareRoot = sqrt(total);
array = realloc(array, x * sizeof(int));
int b = 0;
while (b < x) {
overall = array[c] / squareRoot;
printf("%.3f ", overall);
b++;
}
}