finding highest value in array

Aug 15, 2012 at 7:07am
i am having a little trouble with getting the highest value in this array. can someone please tell me whats wrong?

here is the code.

#include <iostream>

using namespace std;


int main()
{
const int SIZE=5;
int numArray[SIZE];
int numbers=0;
int highest;
int lowest;
int count=0;

cout<<"Enter ten numbers and i will tell you the largest and the smallest. "<<endl;

for (count=0; count < SIZE; count++)
{
cout<<"Number "<<count+1<<":";
cin>>numArray[numbers];
highest=numArray[0];
if(numArray[count] > highest)
{
highest = numArray[count];
}
}



cout<<"The highest number is "<<highest<<endl;




system ("pause");
return 0;
}

it is printing the last number entered instead of the highest value.
Aug 15, 2012 at 7:20am
your code with code tags:
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
#include <iostream>

using namespace std;


int main()
{
const int SIZE=5;
int numArray[SIZE];
int numbers=0;
int highest;
int lowest;
int count=0;

cout<<"Enter ten numbers and i will tell you the largest and the smallest. "<<endl;

for (count=0; count < SIZE; count++)
{
cout<<"Number "<<count+1<<":";
cin>>numArray[numbers];
highest=numArray[0];
if(numArray[count] > highest)
{
highest = numArray[count];
}
}



cout<<"The highest number is "<<highest<<endl;




system ("pause");
return 0;
}


in the line:
cin>>numArray[numbers];

you are setting the index numbers which is 0, and does not change so the only inputted number being stored is the last, thus you see the last number.

change to:
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
#include <iostream>
using namespace std;

int main()
{
	const int SIZE=10;		//should be 10 not 5
	int numArray[SIZE];
	int highest;
	int lowest;				//count need not be declared here
	cout<<"Enter ten numbers and i will tell you the largest and the smallest. "<<endl;

	for (int count=0; count < SIZE; count++) {		//gets input
		cout<<"Number "<<count+1<<":";
		cin>>numArray[count];
	}
	highest=numArray[0];				//sets highest and lowest
	lowest=numArray[0];
	for(int count=0; count<SIZE; count++) {		//find highest and lowest
		if(numArray[count] > highest)
		{
			highest = numArray[count];
		}
		if(numArray[count]<lowest) {
			lowest=numArray[count];
		}
	}
	cout<<"The highest number is "<<highest<<endl;
	cout<<"The lowest number is "<<lowest<<endl;
	return(0);
}


the above code is not tested, yet it works!


your code could be reduced to:

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
#include <iostream>
using namespace std;

int main()
{
	int lowest,highest,temp;
	cout<<"Enter ten numbers and i will tell you the largest and the smallest. "<<endl;

	cout<<"Number 1:";
	cin>>temp;
	highest=temp;
	lowest=temp;
	
	for (int count=1; count<10; count++) {
		cout<<"Number "<<count+1<<":";
		cin>>temp;
		if(temp>highest)
		{
			highest=temp;
		}
		if(temp<lowest) {
			lowest=temp;
		}
	}
	cout<<"The highest number is "<<highest<<endl;
	cout<<"The lowest number is "<<lowest<<endl;
	return(0);
}
Last edited on Aug 15, 2012 at 7:26am
Aug 15, 2012 at 8:54am
You are asking to enter ten numbers but entering only five numbers

1
2
3
4
const int SIZE=5;
int numArray[SIZE];
....
cout<<"Enter ten numbers and i will tell you the largest and the smallest. "<<endl;


I would rewrite this snip of code the following way

1
2
3
4
const int SIZE = 10;
int numArray[SIZE];
...
cout<<"Enter " << SIZE << " numbers and i will tell you the largest and the smallest. "<<endl;


In the loop instead

1
2
3
4
5
6
cin>>numArray[numbers];
highest=numArray[0];
 if(numArray[count] > highest)
 {
 highest = numArray[count];
 }


the following shall be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cin>>numArray[coiunt];
if ( count == 0 )
{
   highest = numArray[count];
   lowest  = numArray[count];
}
else if ( highest < numArray[count] )
{
   highest = numArray[count];
}
else if ( numArray[count] < lowest )
{
   lowest = numArray[count];
}

Last edited on Aug 15, 2012 at 10:26am
Aug 15, 2012 at 11:09am
Just to be sure, you're aware that C++ has the max_element() function, and this is just an exercise for loops, right?
Aug 15, 2012 at 12:31pm

@Cubbi

Just to be sure, you're aware that C++ has the max_element() function, and this is just an exercise for loops, right?


Just to be sure, you're aware that C++ has the minmax_element() function, and this is just an exercise for loops, right? :)

By the way neither max_element nor minmax_element can be applied because in fact there is an input iterator.:)
Aug 15, 2012 at 12:33pm
I am very much aware of the minmax* functions, vlad.

The original post was "i am having a little trouble with getting the highest value in this array".
Aug 15, 2012 at 12:36pm
In fact he deal with an input iterator not an array. He would deal with an array if he at first would enter values for the array and only after that start to search the highest and the lowest elements.
Aug 19, 2012 at 8:06am
i meant to come back and tell you all thank you for your help!
Topic archived. No new replies allowed.