8 Replies - 8897 Views - Last Post: 26 March 2011 - 02:36 PM Rate Topic: -----

#1 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 tricket_7   User is offline

  • D.I.C Head

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

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 12:01 PM

View Postbrds, 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
Was This Post Helpful? 0
  • +
  • -

#4 brds   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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];


Was This Post Helpful? 0
  • +
  • -

#5 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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;
          }


Was This Post Helpful? 0
  • +
  • -

#6 tricket_7   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 brds   User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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


Was This Post Helpful? 1
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

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

Re: finding the lowest/highest value in an array

Posted 26 March 2011 - 02:08 PM

View Postbrds, 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.
Was This Post Helpful? 3
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1