A probably simple problem.

This is a discussion on A probably simple problem. within the C++ Programming forums, part of the General Programming Boards category; Here it is. I am trying to get an output that shows the highest number in an array and what ...

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

    A probably simple problem.

    Here it is. I am trying to get an output that shows the highest number in an array and what number this number is in the array. When you run it it shows the correct output forthe second two arrays but it is off on the first one. Even when I switched it to find the lowest it came out correct I don't know why it doesn't work for the first array. If you can determine why it would be greatly appreciated. Here is the code.

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int FindIndexHighest(double [],const unsigned 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);

    int index = FindIndexHighest (x, sizeof (x) / sizeof (x[0]));
    cout << "Array x: element index = " << index
    << " element contents = " << x[index] << '\n';
    index = FindIndexHighest (y, sizeof (y) / sizeof (y[0]));
    cout << "Array y: element index = " << index
    << " element contents = " << y[index] << '\n';
    cout << "Array z: element index = "
    << FindIndexHighest (z, sizeof (z) / sizeof (z[0]))
    << " element contents = "
    << z[FindIndexHighest (z, sizeof (z) / sizeof (z[0])) ] << '\n';
    return 0;
    }
    int FindIndexHighest( double num[], const unsigned int SZ )
    {

    int a = 0;
    double highest = num[ 0 ];

    for( int i = 1; i < SZ; i++ )

    {
    if( highest < num[ i ])
    a = i;

    }



    return a;
    }

    Thanks
    Nate2430

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    You must put 'highest = num[i]' in the comparing if-statement.
    // Gliptic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-17-2010, 12:00 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 08:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 06:33 PM

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