Array program

I have been working on this program as homework and cannot figure out why it will not execute. Any help would be much appreciated.

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
 


#include <iostream>
#include <iomanip>

using namespace std;



int main()
{

	//change the variable size to modify the amount of grades desired
	const int size = 5;
	int array[size];
	int lowest = 101;
	int total = 0;
	double average;
	int i = 0;

	//loop through the array
	for (i = 0; i < size; i++)
	{
		//get inputs
		cout << "\nPlease enter grade " << i + 1 << ": ";
		cin >> array[i];
		
		

		//check that value entered is between 0 and 100
		while (array[i] < 0 || array[i] >100)
		{
			cout << "Wrong input. Please enter a value between 0 and 100 only." << endl;
			cout << "\nPlease Reenter grade " << i + 1 << " : ";
			cin >> array[i];
		}
	}
	
	//pass the array to function getLowestGrade() instead, get the value as a return
	lowest = getLowestGrade(array);
	
	//loop through the array
	for (i = 0; i < size; i++)
	{
		//if the value of the array element isn't the lowest then we'll add it to the total(because you want to drop the lowest value)
		if (array[i] != lowest)
		{
			total = total + array[i];
		}
	}


	//then take the total and divide it by the total sample (size minus one because we ditched the lowest grade)
	average = (total / (size - 1));

	//print the average 
	cout << average;

	//pause 
	system("pause");

}


int getLowestGrade(int array[])
{
	int lowest = 101;

	//loop through the array
	for (i = 0; i < size; i++)
	{
		while (array[i] < lowest)
		{
			lowest = array[i];
		}
	}
	
	return lowest;
}
No function prototype for getLowestGrade

Prototype:

int getLowestGrade(int[], int);

getLowestGrade has a variable size that doesn't exist for it and i isn't initialized either. Also while loop?

Corrected version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int getLowestGrade(int array[], int size)
{
	int lowest = array[0];

	//loop through the array
	for (int i = 1; i < size; i++)
	{
		if(array[i] < lowest)
		{
			lowest = array[i];
		}
	}

	return lowest;
}


main needs to return 0;

After that you will have to make some minor corrections in your main()
Last edited on
Topic archived. No new replies allowed.