I have to read in a file and do dot product calculations on two arrays but I get an error whenever I try to run it.
Here is my code,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
FILE* input;
FILE* output;
int product=0;
int length=0;
int* x=0;
int* y=0;
int count=0;
int i=0;
int p=0;
input = fopen("input.txt", "r");
output = fopen("output.txt", "w");
fscanf(input, "%d\n", &length);
x = (int*)malloc(length*4);
y = (int*)malloc(length*4);
for(count=0;count<length;count++){
fscanf(input, "%d\n", &x[count]);
}
for(count=0;count<length;count++){
fscanf(input, "%d\n", &y[count]);
}
{
for(i=0;i<length;i++)
p += x[i]*y[i];
product = p;
fprintf(output, "The number %d was read in\n", product);
}
fclose(input);
fclose(output);
free(x);
free(y);
return 0;
}
10
1
2
3
4
5
6
7
8
9
10
10
10
10
10
10
10
10
10
10
10
and here is the way my input file is formatted.
Thanks for any help.