Can't find the lowest number in array?

This is a discussion on Can't find the lowest number in array? within the C++ Programming forums, part of the General Programming Boards category; With the following code I found the highest number in each array but it only returns the first number in ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Can't find the lowest number in array?

    With the following code I found the highest number in each array but it only returns the first number in each array for the lowest and not the lowest number. Could someone please tell me why?#include <iostream>
    #include <iomanip>
    using namespace std;


    double FindHighest( double [], const int );
    double FindLowest( double [], const int );

    int main()
    {
    double x[] = { 11.11, 66.66, 88.88, 33.33, 55.55 };
    double y[] = { 9, 6, 5, 8, 3, 4, 7, 4, 6, 3, 8, 5, 7, 2 };
    double z[] = { 123, 400, 765, 102, 345, 678, 234, 789 };

    cout << fixed << setprecision(2);

    double high1 = FindHighest (x, sizeof (x) / sizeof (x[0]));
    double high2 = FindHighest (y, sizeof (y) / sizeof (y[0]));
    double low1 = FindLowest (x, sizeof (x) / sizeof (x[0]));
    double low2 = FindLowest (y, sizeof (y) / sizeof (y[0]));

    cout << "Array x: high = " << high1 << " low = " << low1 << '\n';
    cout << "Array y: high = " << high2 << " low = " << low2 << '\n';
    cout << "Array z: high = " << FindHighest (z, sizeof (z) / sizeof (z[0]))
    << " low = " << FindLowest (z, sizeof (z) / sizeof (z[0])) << '\n';
    return 0;
    }

    double FindHighest( double num[], const int SZ )
    {
    int highest = num[ 0 ];

    for( int i = 1; i < SZ; i++ )
    {
    if( highest < num[ i ])
    highest = num[ i ];
    }
    return highest;
    }

    double FindLowest( double num[], const int SA )
    {

    int highest = 0;

    double lowest = num[ highest ];


    for( int i = highest; i > SA; i++ )
    {
    if( lowest > num[ i ])
    lowest = num[ i ];

    }
    return lowest;
    }

    Your help is greatly appreciated,
    Nate2430

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    in the following line change the > to <.

    for( int i = highest; i > SA; i++ )


    As is the loop never runs because i is never greater than SA. THerefore you always get what you started out with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. finding the element number in an array
    By tommy69 in forum C Programming
    Replies: 7
    Last Post: 04-02-2004, 04:26 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21