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.

>>I get an error whenever I try to run it.
What was the error?

>>x = (int*)malloc(length*4);

what makes you think the size of an integer is 4??? Maybe it is, and maybe it isn't. The size of an integer depends on the compiler you are using. A more portable way to write that is x = malloc(length * sizeof(int)); And notice that C language does not require the typecast like C++ does. So if your source code file name has .cpp extension then you are writing c++ code, not c code.


line 42: Depending on the values you put in the two arrays, summing p like that could easily cause integer overflow. Might be better to use a 64-bit integer to reduce that chances of that happening.

Edited 5 Years Ago by Ancient Dragon: n/a

>>x = (int*)malloc(length*4);

what makes you think the size of an integer is 4??? Maybe it is, and maybe it isn't. The size of an integer depends on the compiler you are using. A more portable way to write that is x = malloc(length * sizeof(int)); And notice that C language does not require the typecast like C++ does. So if your source code file name has .cpp extension then you are writing c++ code, not c code.


line 42: Depending on the values you put in the two arrays, summing p like that could easily cause integer overflow. Might be better to use a 64-bit integer to reduce that chances of that happening.

I had the sizeof in there before, I have no idea why I changed it. How would I go about using a 64 bit int?

depends on the operating system. MS-Windows and VC++ 2010 have __int64 data type. Other compilers might support long long. VC++ 2010 allows long long but the compiler treats it as 32-bit long integer. If you are not sure what your compiler supports then just write a simple program to print out its size

#include <stdio.h>
int main()
{
   printf("sizeof(__int64) = %d\n", sizeof(__int64));
   printf("sizeof(long long) = %d\n", sizeof(long long));
}

Edited 5 Years Ago by Ancient Dragon: n/a

I changed it but I still get the error Expression: (stream != NULL)

I compiled and ran your program without changing anything and did not get that error. It ran and produced this output file

The number 550 was read in

Maybe there is something wrong with your input file? Attached is the file I used

Edited 5 Years Ago by Ancient Dragon: n/a

Attachments
10
1
2
3
4
5
6
7
8
9
10
10
10
10
10
10
10
10
10
10
10

Wow I can't believe it actually works. It has something to do with how this compiler works and I really can't think of anyway to fix it. I have the zip of my project folder here I'm not sure what is wrong, maybe the text file is in the wrong location. Sorry for bothering you like this.

I can't open that zip file, but make sure the data file is in the same folder as the source files. Your program should also check to see if fopen() succeeded, which it is not doing.

Thank you so much. I got it working. The main.c file was saved in the wrong place.

This question has already been answered. Start a new discussion instead.