C code in c++ keeps crashing, using arrays and pointers

I'm working with arrays and pointers and I feel like my readvalues function is incorrect and is causing the exe file to not respond and crash can somebody please tell me what I did wrong.

This is the initial question I had to solve

Write a program, maxmin.c, which uses two functions, readvalues and range, to find the
maximum and minimum of a set of floats stored in the file samplesin.txt. You can assume
that the number of values in the input file is less than 100.
Your main must use the following steps:
• open the two files samplesin.txt and maxminout.txt.
• call the function readvalues which reads the values from samplesin.txt into the array
x and returns the value of n (number of values in the file).
• call the function range which computes the maximum and minimum values found in the
array x.
• write the maximum and minimum values to the file maxminout.txt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<math.h>

int readvalues(FILE* fin, float *x);
void range(float *x, int n, float *pmin, float *pmax);

int main(void)
{
	float x[100];
	float pmin, pmax;
	int n;
	
	FILE* fin= fopen("samplesin.txt","r");
	FILE* fout= fopen("maxminout.txt","w");
	
	readvalues(fin,x);
	range (x, n, &pmin, &pmax);
	
	fprintf(fout,"There are %d values in the array\n", n);
	fprintf(fout,"The maximum value in the array is %f\n", pmax);
	fprintf(fout,"The minimum value in the array is %f\n", pmin);
	
	fclose(fin);
	fclose(fout);
	
	system("pause");
	return 0;
}

int readvalues(FILE* fin, float *x){
	int n;
	while (fscanf(fin, "%d", n) !=EOF){
	n++;}
	return n;
}

void range(float *x, int n, float *pmin, float *pmax){
	
	float answer_1, answer_2;
	int i;	
	
	if(x[i]>answer_1){
			answer_1 = x[i];
			
		}
	if(x[i]<answer_2){
		answer_2 = x[i];}
		

	*pmin = answer_1;
	*pmax = answer_2;
	
	return;
}



Last edited on
Several problems are apparent:
 
while (fscanf(fin, "%d", n) !=EOF)

1) That needs to be &n to scan into an integer.
2) Why are you scanning into a local int? Don't you want to scan into the next float in the array?

1
2
3
4
5
6
float answer_1, answer_2;
int i;	

if(x[i]>answer_1)
...
if(x[i]<answer_2){

3) answer_1, answer_2 and i are all uninitialized variables. You're comparing against garbage.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Hint: You can edit your previous post, highlight your code and press the <> formatting button.

Last edited on
I feel like my readvalues function is incorrect

Yes, there are several things wrong with your readvalues function. The foremost being that fscanf requires a pointer and you're feeding it an int. The next being that the values you are reading are not representable as ints. The third being that you are not reading values into the array that you are supposed to be reading into.
1
2
3
4
5
6
7
        int readvalues(FILE* fin, float *x){
	int n;
	n = 0;
	while (fscanf(fin, "%f", &x[n]) !=EOF){
	n++;}
	return n;
}


so like this then ?
Last edited on
so like this then ?

Closer. The type of *x is not compatible with the format specifier %d.
Last edited on
Thank you very much kind sir, I've been staring at this for a while wondering where I put an int instead of an float.
Topic archived. No new replies allowed.