Program output becomes unstable when all negative inputs are introduced?

75 views Asked by At

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 ;
}
0

There are 0 answers