C vector product with file input . dat

Asked

Viewed 348 times

0

Be the code below, where the input vector.dat file, on each line, has 6 numbers where the first 3 are components of a vector "u" and the last 3 are components of a vector "v". The vector.dat output file must have 3 components in each line of the vector product of u per 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 line of "vector.dat" in an array p[6] right after it performs the vector product.

The problem is that by placing a 45-line "vector.dat" file the program prints a "vector.dat" file with only 38 lines (it should print 45). When placing a "vector.dat" file with 4 lines the program generates a "vector.dat" file with 3 lines.

The code always seems to generate a file with fewer output vectors than input vectors.

I don’t know if the error is in the passage

  while(fscanf(entrada,"%lf",&componente) != EOF){

for example.

1 answer

0

I just found that mistake:

 else if(i = N)

Where it should surely be:

else if(i == N)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.