Ah, you have three problems in your code still.
1. I have no idea about if there is a convenient way to use array with sscanf, the ugly way here is :
Code:
sscanf(text, "%lf %lf %lf %lf %lf", v, v+1, v+2, v+3, v+4);
Remeber, array is pointer and pointer is array on some occasions. So, you needn't dereference an array identifier when you need a pointer to the array.
2. array index starts with 0 nor 1. so there are problems within you loop structure.
Code:
for (k=0; k<5; k++) {
mid += pow(v[k],2);
}
norm = sqrt(mid);
for (k=0; k<5;k++) {
vec[k] = v[k]/norm;
}
3. the third is what tabstop said, you need adding a loop structure to print all elements of an array.
Code:
printf("\nOriginal Vector = [");
for (k = 0; k < 5; k++) {
printf("%.6f ", v[k]);
}
printf(" ]\n");
printf("\nNormalized Vector = [");
for (k = 0; k < 5; k++) {
printf("%.6f ", vec[k]);
}
printf(" ]\n");
below is the code I have modified, it generates the output what you want.
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
int k;
char text[81];
double v[5], vec[5];
double norm, mid;
FILE *Inf_1, *Outf_1;
mid = 0;
Inf_1 = fopen("vec_in.dat", "r");
Outf_1 = fopen("vec_out.dat", "w");
if ((Inf_1 == NULL) || (Outf_1 == NULL))
{
printf("Error oepning the file\n");
exit(1);
}
while(fgets(text,81,Inf_1) != NULL)
{
sscanf(text, "%lf %lf %lf %lf %lf", v, v+1, v+2, v+3, v+4);
for (k=0; k<5; k++) {
mid += pow(v[k],2);
}
norm = sqrt(mid);
for (k=0; k<5;k++) {
vec[k] = v[k]/norm;
}
}
printf("\nOriginal Vector = [");
for (k = 0; k < 5; k++) {
printf("%.6f ", v[k]);
}
printf(" ]\n");
printf("\nNormalized Vector = [");
for (k = 0; k < 5; k++) {
printf("%.6f ", vec[k]);
}
printf(" ]\n");
fprintf(Outf_1,"\nOriginal Vector = [");
for (k = 0; k < 5; k++) {
fprintf(Outf_1,"%.6f", v[k]);
}
fprintf(Outf_1," ]\n");
fprintf(Outf_1,"\nNormalized Vector = [");
for (k = 0; k < 5; k++) {
fprintf(Outf_1,"%.6f", vec[k]);
}
fprintf(Outf_1,"\n");
fclose(Inf_1);
fclose(Outf_1);
system("pause");
exit(0);
}