finding the lowest/highest value in an array

Page 1 of 1

8 Replies - 8897 Views - Last Post: 26 March 2011 - 02:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=224433&amp;s=b8bcfd8c5fe115b17d6ffc71870b499a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 tricket_7

Reputation: 0
• Posts: 104
• Joined: 09-May 09

finding the lowest/highest value in an array

Posted 26 March 2011 - 11:54 AM

I haven't yet put in the code to find the lowest value but my code to find the highest value in the array is not returning the correct number.
```#include <iostream>

using namespace std;

int main()
{
const int VALUES = 10;         //number of values
int num[VALUES];               //each value
int count, highest, lowest;                     //loop counter

//Input values
for (count = 0; count < VALUES; count++)
{
cout << "Enter value "
<< (count + 1) << ": ";
cin >> num[VALUES];
}

//Find the highest and smallest value in the array
highest = num[0];

for(int count = 0; count < VALUES; count++)
{
if(num[count] > highest)
highest = num[count];
}

//Display the largest and smallest value of the array
cout << highest << endl;

system("pause");
return 0;
}

```

This post has been edited by tricket_7: 26 March 2011 - 11:58 AM

Is This A Good Question/Topic? 0

Replies To: finding the lowest/highest value in an array

#2 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:00 PM

Yes, you have code to show calculate the highest, but none to calculate the lowest. I suggest you read your code again.

This post has been edited by brds: 26 March 2011 - 12:02 PM

#3 tricket_7

Reputation: 0
• Posts: 104
• Joined: 09-May 09

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:01 PM

brds, on 26 March 2011 - 01:00 PM, said:

Yes, you have code to show the highest, but none to calculate the lowest. I suggest you read your code again.

I realized I messed that up and fixed it, still having the problem though

#4 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:05 PM

Yeah, sorry.
```cin >> num[VALUES]; // You are only populating the last position of the array.

//Try
cin >> num[count];

```

#5 tricket_7

Reputation: 0
• Posts: 104
• Joined: 09-May 09

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:17 PM

when I add the code to also give the lowest value it does not give me the right values.
```#include <iostream>

using namespace std;

int main()
{
const int VALUES = 10;         //number of values
int num[VALUES];               //each value
int count;                     //loop counter

//Input values
for (count = 0; count < VALUES; count++)
{
cout << "Enter value "
<< (count + 1) << ": ";
cin >> num[count];
}

//Find the highest and smallest value in the array

int highest = num[0];
int lowest = num[0];

for(int count = 0; count < VALUES; count++)
{
if(num[VALUES] > highest)
highest = num[VALUES];

if(num[VALUES] < lowest)
lowest = num[VALUES];
}

//Display the largest and smallest value of the array
cout << "The highest value in the array is: " << highest << "\nThe lowest value in the array is: " << lowest << endl;

system("pause");
return 0;
}

```

#6 tricket_7

Reputation: 0
• Posts: 104
• Joined: 09-May 09

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:36 PM

I got it working, thanks for the help

#7 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:38 PM

Go back to your first post in this thread, notice the use of count during the calculation of highest.

Why are you using VALUES(a static number), where count should be used?

```for(count = 0; count < VALUES; count++)
cout << "VALUES :" << VALUES << " count:" << count << endl;

Output
------
VALUES :10 count:0
....
VALUES :10 count:9

```

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 02:08 PM

brds, on 26 March 2011 - 03:05 PM, said:

Yeah, sorry.
```cin >> num[VALUES]; // You are only populating the last position of the array.

//Try
cin >> num[count];

```

Actually, in this case he's populating the memory one BEYOND the end of the array, which is of course undefined behavior.

#9 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 02:36 PM

Even though this is just a trivial "toy" program, a quick note about efficiency might be worthwhile.

This code
```        for(int count = 0; count < VALUES; count++)
{
if(num[VALUES] > highest)
highest = num[VALUES];

if(num[VALUES] < lowest)
lowest = num[VALUES];
}

```

isn't "incorrect", but whenever it is true that (num[VALUES] > highest), then it is a complete waste of processing time to test for (num[VALUES] < lowest).

Adding one tiny word else to that loop, as in
```        for(int count = 0; count < VALUES; count++)
{
if(num[VALUES] > highest)
highest = num[VALUES];

else if(num[VALUES] < lowest)
lowest = num[VALUES];
}

```

eliminates all of that unnecessary testing.