0

As the title suggests, my program which is designed to find the largest element within a user defined integer array becomes unstable when all negative numbers are used for the user input. Additionally the output becomes unstable when all zero's are used for the user input (excluding the input for total # of elements).

The program works just fine when all positive numbers are used. This is quite perplexing to me and I'm sure there's a valid explanation but I was under the impression that in C++, the int & float data types were automatically signed and would be able to handle negative numbers for it's data range. So why would the program not return a valid output if all negative numbers are used for the user array input(s)?

Bad Output:

Please enter a total number of elements you'll be using: 5

Please enter each variable one by one.


Enter number 1: -10

Enter number 2: -5

Enter number 3: -20

Enter number 4: -2

Enter number 5: -7


The largest element within specified realNum[5] array is element number: 6 with a value of: 5.88501e-039

Good Output:

Please enter a total number of elements you'll be using: 5

Please enter each variable one by one.


Enter number 1: 10

Enter number 2: 5

Enter number 3: 20

Enter number 4: 2

Enter number 5: 7


The largest element within specififed realNum[5] array is element number: 3 with a value of: 20

Program:

//10.2 Largest Element Finder of an Array
//Mandatory header
#include <iostream>

//Use namespace std ;
using namespace std ;

//Mandatory main method
int main ()
{
    //Declare and initlize variables
    int i, total, realNum = 0, temp = 1 ;

    //Ask user to input a number of total elements
    cout << endl << endl
    << "Please enter a total number of elements you'll be using: " ;

    //Wait for user input
    cin >> total ;

    //Declare array set
    float setNum [total] ;

    //Ask user to input each varaible
    cout << endl
    << "Please enter each variable one by one." << endl << endl ; 

    for ( i = 0 ; i < total ; i ++ )
    {
        cout << endl << "Enter number " << (i + 1) << ": " ;
        cin >> setNum [i] ;
    }

    //Find the largest element within the array
    for ( i = 0 ; i < total ; i ++ )
    {
        if ( setNum [i] <= setNum [temp] ) //Discard current i if less than the next element - Means temp is HIGHER and should be saved
        {
            if ( setNum [temp] >= setNum[realNum] )
                realNum = temp ; //Temp can now be changed for iteration purposes as realNum is saving the highest element's positon
        }

        else if ( setNum [i] >= setNum [temp] ) //Discard current i if more than the next element and use the remainder to compete against the realNum
        {
            if ( setNum [i] >= setNum [realNum] )
                realNum = i ;
        }

        i ++ ;
        temp += 2 ;
    }

    //Display calculations
    cout << endl << endl
    << "The largest element within specififed realNum[" << total << "] array is element number: " << (realNum + 1) << " with a value of: " << setNum[realNum] << endl ;

    //Mandatory return statement
    return 0 ;
}
Mr SnowGlobe
  • 57
  • 1
  • 8
  • 1
    `if ( setNum [i] <= setNum [temp] )` Keep an eye on `temp`. Looks like it can get larger than (or equal to) the array size. P.S. There's no need for an array here that I can see. – 001 Apr 07 '20 at 18:37
  • 2
    That's a really convoluted algorithm to find the max element in an array. Something like `int max = 0; for (int i = 1; i < total; ++i) if (setNum[i] > setNum[max]) max = i;` would be simpler _and_ not access the array out of bounds. – Miles Budnek Apr 07 '20 at 18:58
  • @MilesBudnek Awesome, I see my error now! I was going out of bounds on the array whenever an odd number of elements was being used; since whenever an even number was inputted the output stabilized. This disparity is a result of just bad programming by over-complicating the testing procedure, trying to test 2 number of elements for each iteration when it should've just been a test of 1 element at a time. Not sure where I got the idea from, but it was quite detrimental in the end. I have fixed the program and learned a little more as I practice C++ using these trivial programs, thanks! – Mr SnowGlobe Apr 07 '20 at 19:55
  • @Johnny Mopp Yes, I can see that now, as I was going out of bounds. And this program was simply a practice run for using arrays and running through their data values. I'm a beginner at C++ atm. – Mr SnowGlobe Apr 07 '20 at 19:57

0 Answers0