How to find largest number using array



I'm having hard time to fill the homework that my teacher assigned to us this morning, I missed a day of class and missed a lot of lectures.

Please help! I have no idea how to find the largest and the lowest number, and please don't explain it by words I prefer example, so I could see what's happening. My teacher sent me this code to fill. I'm sitting with no one to ask for help, found this web, register, then this. I hope you guys can help me ASAP. THANKS!



// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.

// PLACE YOUR NAME HERE

#include <iostream>

using namespace std;

const int ARRAY_SIZE=100;

float findAverage (int array[], int size); // finds average of all grades
int findHighest (int array[], int size); // finds highest of all grades
int findLowest (int array[], int size); // finds lowest of all grades

int main()
{


int grades[ARRAY_SIZE]; // the array holding the grades.

int temp; // variable for the input number

int numberOfGrades; // the number of grades read.

int pos; // index to the array.

float avgOfGrades; // contains the average of the grades.

int highestGrade; // contains the highest grade.

int lowestGrade; // contains the lowest grade.



cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;

cin >> temp;

if(temp!=-99 )
{
while(temp > 100 || temp < 0)
{
cout<<"Enter grade, grade > 0 and grade < 101"<<endl;
cin>>temp;
}
}
else
return false;

pos=0;
while(pos < ARRAY_SIZE && temp != -99)
{
// Fill in the code to assign temp to the next element in intArray
grades[pos] = temp;

// Fill in the code to read the next number
grades[pos];
++pos;
}// end for



numberOfGrades=ARRAY_SIZE; // Fill blank with appropriate identifier


avgOfGrades = findAverage(grades, numberOfGrades);

// Fill in the call to the function that calculates highest grade
highestGrade= findHighest(grades, numberOfGrades);



// Fill in the call to the function that calculates lowest grade



cout << endl << "The average of all the grades is " << avgOfGrades << endl;

cout << endl << "The highest grade is " << highestGrade << endl;

// Fill in code to write the lowest to the screen




return 0;
} // end main


//****************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers, size
// data returned: avarage of the numbers in the array
//
//****************************************************************************

float findAverage (int array[], int size)
{
float sum = 0; // holds the sum of all the numbers

if( size <=0 )
return 0;

for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];

return (sum / size); //returns the average

}

//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers, size
// data returned: highest value of the numbers in the array
//
//***************************************************************************
int findHighest (int array[], int size)
{

int largest;

while(largest < size)
largest=array[largest];
return largest;

// Fill in the code for this function



} // end findHighest


//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers, size
// data returned: lowest value of the numbers in the array
//
//****************************************************************************

int findLowest (int array[], int size)
{

// Fill in the code for this function



} // end findLowest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>
#include <utility>
int main()
{
	const int SIZE = 6;
	int arr[] = { 4, 66, 29, 1003, 482, 67 };

	auto element = std::minmax_element(arr, arr + SIZE);

	std::cout << "The smallist number is " << *element.first << std::endl;
	std::cout << "The biggest number is " << *element.second << std::endl;


	std::cin.ignore();
	return 0;
}
Last edited on
lmptitfy

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
98
// This program will read in a group of test scores( positive integers from 1 to 100)
// from the keyboard and then calculates and outputs the average score
// as well as the highest and lowest score. There will be a maximum of 100 scores.

// PLACE YOUR NAME HERE

#include <iostream>

using namespace std;

const int ARRAY_SIZE=100;

float findAverage (int array[], int size); // finds average of all grades
int findHighest (int array[], int size); // finds highest of all grades
int findLowest (int array[], int size); // finds lowest of all grades
#include <iostream>

using namespace std;

const int ARRAY_SIZE=100;

float findAverage (int array[], int size); // finds average of all grades
int findHighest (int array[], int size); // finds highest of all grades
int findLowest (int array[], int size); // finds lowest of all grades

int main()
{


int grades[ARRAY_SIZE];	// the array holding the grades.

int temp; // variable for the input number

int numberOfGrades; // the number of grades read.

int pos;	// index to the array.

float avgOfGrades; // contains the average of the grades.

int highestGrade; // contains the highest grade.

int lowestGrade; // contains the lowest grade.



cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;

cin >> temp;

if(temp!=-99 )
{
while(temp > 100 || temp < 0)
{
cout<<"Enter grade, grade > 0 and grade < 101"<<endl;
cin>>temp;
}
}
else
return false;

pos=0;
while(pos < ARRAY_SIZE && temp != -99)
{
// Fill in the code to assign temp to the next element in intArray
grades[pos] = temp;

// Fill in the code to read the next number
grades[pos];
++pos;
}// end for



numberOfGrades=ARRAY_SIZE; // Fill blank with appropriate identifier


avgOfGrades = findAverage(grades, numberOfGrades);

// Fill in the call to the function that calculates highest grade
highestGrade= findHighest(grades, numberOfGrades);



// Fill in the call to the function that calculates lowest grade



cout << endl << "The average of all the grades is " << avgOfGrades << endl;

cout << endl << "The highest grade is " << highestGrade << endl;

// Fill in code to write the lowest to the screen




return 0;
} // end main 


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
//****************************************************************************
// findAverage
//
// task: This function receives an array of integers and its size.
// It finds and returns the average of the numbers in the array
// data in: array of floating point numbers, size
// data returned: avarage of the numbers in the array
//
//****************************************************************************

float findAverage (int array[], int size)
{
float sum = 0; // holds the sum of all the numbers

if( size <=0 )
return 0;

for (int pos = 0; pos < size; pos++)
sum = sum + array[pos];

return (sum / size); //returns the average

}

//****************************************************************************
// findHighest
//
// task: This function receives an array of integers and its size.
// It finds and returns the highest value of the numbers in
// the array
// data in: array of floating point numbers, size
// data returned: highest value of the numbers in the array
//
//***************************************************************************
int findHighest (int array[], int size)
{

int largest;

while(largest < size)
largest=array[largest];
return largest;

// Fill in the code for this function



} // end findHighest


//****************************************************************************
// findLowest
//
// task: This function receives an array of integers and its size.
// It finds and returns the lowest value of the numbers in
// the array
// data in: array of floating point numbers, size
// data returned: lowest value of the numbers in the array
//
//****************************************************************************

int findLowest (int array[], int size)
{

// Fill in the code for this function



} // end findLowest 


You might find the bubblesort in a C++ book a useful example
Thank you sir !
Topic archived. No new replies allowed.