This is an assignment question, and is most likely a problem with pointers. imput file is :
1
2
-1
3
-2
#include<stdio.h>
#include<math.h>
#define N 100
void distance(float* x, int q, float* ra, float* rb);
int main(void){
FILE* fout = fopen("lengthout.txt" , "w");
FILE* fin = fopen("lengthin.txt", "r");
float x[N], ra, rb;
int q=0;
while(fscanf(fin, "%d\n", x[q]) !=EOF){
q++;
}
distance(x, q, &ra, &rb);
fprintf(fout, "The array has %d entries. The norms are: \n" , q);
fprintf(fout, "1-norm = %f \n", ra);
fprintf(fout, "2-norm = %f \n", rb);
fclose(fout);
fclose(fin);
return 0;
}
void distance(float* x, int q, float* ra, float* rb){
float pra=0, prb=0;
int t;
for(t=0;t<q;t++){
pra = pra + fabs(x[t]);
}
*ra = pra;
for(t=0;t<q;t++){
prb = prb + pow(x[t], 2);
}
*rb = sqrt(prb);
}