Finding the minimum value of an array, C++

I'm trying to find the average, minimum, and maximum value of a list of data. The data is entered into an array, and then we find those values.

Here's the program

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <fstream>
#include <cctype>
#include <iomanip>

using namespace std;

float average(int grades[]);

int main()
{
	ifstream sourcefile;
	sourcefile.open("a5.txt");

	if(!sourcefile)
	{
		cout << "imput failed" << endl;
		return 0;
	}

	int grades[25];

	int bill;
	int max = 0;

	for(bill = 0 ; bill < 25 ; bill++)
	{	
		sourcefile >> grades[bill];

		if (grades[bill] > max)
		{
			max = grades[bill];
		}
		cout << grades[bill] << endl;
	}

	

	for(bill = 0 ; bill < 25 ; bill++)
	{
		cout << grades[bill] << endl;
	}

	int min = 100;

	for(bill = 0 ; sourcefile ; bill++)
	{
		min = grades[bill];
		
	}
	

	cout << "The top grade is " << max << endl;
	cout << "The lowest grade is " << min << endl;

// it says the min is 100 for some reason... I don't think I'm using the right 
// technique
	


// In the section below the program completely crashes.

cout << "this is a printout of the array, to make sure it's not being modifed" << endl;

	ofstream output;
	output.open("output.txt");

	
	for(bill = 0 ; bill < 25 ; bill++)
	{
		output << grades[bill] << endl;
	}

		



	return 0;
}

float average(int grades[])
	{
		int total = 0;
		int bill = 0;
		float average;
		for (bill ; bill < 25 ; bill++)
		{
			total = total + grades[bill];
		}

		cout << "total of all grades = " << total << endl;

		average = float(total) / 25;

		cout << "Average of all grades = " << average << endl;
		return average;
	}


I found the maximum, the minimum, and the average just fine, but I think there's something wrong with how I find the minimum.

How would you guys reccomend finding the minimum value of an array? I tried to use a for loop and do the reverse of what I did to find the maximum value.
Last edited on
You didn't actually do the reverse; Re-check the inside of the max loop and compare it to what you have inside the min loop.
Oh, I put the wrong thing up, I messed it up trying to find my mistake and I forgot to put up a working model

it works now, but something really strange kept happening

it kept glitching at my for loop end condition

When I had the end condition be the end of the file (sourcefile), it worked fine, but when I set it to when my count variable is less than 25 (bill < 25) I got a WINDOWS error message, not a MVC++ error like usual


Do you think that's vista screwing up on me? The condition "bill < 25" works great now =\

Topic archived. No new replies allowed.