Largest and smallest in array

Hello,
I need help displaying the Largest and Smallest numbers in my array. I don't know how to code it correctly. I was able to set up the array and get a sum and average to work. The problem section is labeled Largest and Smallest.

please help me output the largest and smallest numbers in the array. thank you in advance for your help.

==============================================================================

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
#include<iostream>
#include <cstdlib>

using namespace std;

const int ARRAY_SIZE = 10;

int main()
{
	double avg;
	int number[ARRAY_SIZE];
	int lcv,
		seed,
		sum=0;

	// *********************************************** Random Number Generator ****************************************************************



	cout << "Enter seed for random number generator ==> ";
	cin >> seed;
	srand(seed);
	

	for(lcv = 0; lcv < ARRAY_SIZE; lcv++)
	{
		number[lcv] = rand();
	}

	cout << "**********************************************" << endl << endl;       





	// ************************************************* Display the Numbers in Array ***************************************************************



	cout << "these are the numbers that are in the array " << endl;
	
	for(lcv = 0; lcv < 10; lcv++)
	{
		cout << "                                 " << number[lcv] << endl;
	}

	// ************************************************************* Calculate the Sum **************************************************************

	cout << "                                _______" << endl << endl;         // visual seperators

	for(lcv = 0; lcv < 10; lcv++)
	{
		sum += number[lcv];
	}
	cout << "                      The sum is " << sum << endl << endl;


	// *********************************************************** Calculate the Average ****************************************************************



	for(lcv = 0; lcv < 10; lcv++)
	{
		double denom = 10.00;
		avg = ( sum / denom );
	}
	cout << "                  The average is " << avg << endl;


	cout << "**********************************************" << endl << endl;         // visual seperators



	
	// ********************************************************** Largest and Smallest*****************************************************************
 


	for(lcv = 0; lcv < 10; lcv++)
	{
		int max;

		if(lcv < number[lcv])
		{
			lcv = number[lcv];
			max = lcv;
		}
		if(max > lcv)
			cout << max << endl;
	}

	return 0;
}

@kaseyahumada
Here's one way. Declare ints max and low with the other declarations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// **********************************************************
//              Largest and Smallest 
// **********************************************************
 
	max = low = number[0]; // Sets values 

	for(lcv = 0; lcv < ARRAY_SIZE; lcv++)
	{
		if(low > number[lcv])   // Check if low is higher, and if it is..
			low = number[lcv];    //  Set low to number[lcv]
		if(max < number[lcv])   //   Check if  max is lower, and if it is.. 
			max = number[lcv];   //  Set max to number[lcv]
	}
	cout << "Low = " << low << "\tMax = " << max << endl;
Last edited on
@whitenite1
thank you for you help with this problem of mine. I will now study your code and lean from it. thanks again for you help. have a blessed day.
Topic archived. No new replies allowed.