Be the code below, where the input file vecores.dat, in each line, has 6 numbers where the first 3 are components of an "u" vector and the last 3 are components of a "v" vector. output file vector.dat should have 3 components in each vector product line of u by v.
#include <stdio.h>
#define N 6
int main(){
FILE *entrada = NULL;
FILE *saida = NULL;
int i=0;
double componente;
double p[N];
double vetorial_x, vetorial_y, vetorial_z;
entrada = fopen("vetores.dat","r");
if(entrada == NULL) printf("O arquivo vetores.dat não existe\n");
saida = fopen("vetorial.dat","w");
while(fscanf(entrada,"%lf",&componente) != EOF){
if(i < N){
p[i] = componente;
i++;
}
else if(i = N){
vetorial_x = p[1]*p[5] - p[2]*p[4];
vetorial_y = p[2]*p[3] - p[0]*p[5];
vetorial_z = p[0]*p[4] - p[1]*p[3];
fprintf(saida,"%.3lf\t%.3lf\t%.3lf",vetorial_x,vetorial_y,vetorial_z);
i=1;
p[0] = componente;
}
}
return 0;
}
The program adds the 6 numbers found in each row of "vector.dat" into an array p [6] shortly after it performs the vector product.
The problem is that when placing a file "vector.dat" with 45 lines the program prints a file "vector.dat" with only 38 lines (it should print 45). When placing a file "vector.dat" with 4 lines the program generates a file "vector.dat" with 3 lines.
The code always seems to generate a file with fewer output vectors than input vectors.
I do not know if the error is in the pass
while(fscanf(entrada,"%lf",&componente) != EOF){
for example.