if (b <= max)
min = b;
Perhaps you should compare the currentmin.
int max=0; //highest vaule
int min=0; //lowest value
You would probably want to initialize both to the first element in the list or else set themax to the lowest int value (INT_MIN) and the min to the highest int value (INT_MAX).
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
I tried followed the program (after corrections) via paper and it seems to work but, when I run the program on the computer it is still not comming up with the correct results.
Let's try this "on paper".
You declare 5 integer variables, but none are initialized to any value -- so their values are completely random.
# include <iostream>
using std::cout; //program uses cout
using std::cin; //program uses cin
using std::endl; //program uses endl
//function main begins program execution
int main ( )
{
int a; //number of values in set inputed by user
int b; //number inputed by user
int c; //counter
int max; //highest vaule
int min; //lowest value
You initialize max and min to the unitialized value of b. So your max and min values are still random values, but both are the same random value that b had when the program started.
//initialization phase
max = b;
min = b;
//processing phase
//get input from user
cout << "Enter the number of values in the set: "; //prompt for input
cin >> a; //read number from user
for ( c=1; c <= a; c++ ) {
Now you finally get a value for b.
cout << "Enter a number: "; //prompt for imput
cin >> b; //read number from user
Then you are comparing the value entered for b with whatever random values min and max were when the program started. Maybe this will work, maybe not.
if (b > max)
max = b;
if (b < min)
min = b;
}
cout << "The maximum value is: " << max << "\n";
cout << "The minimum value is: " << min << " " <<endl;
return 0; //indicates that program ended successfully
} //end function main
Here is another way to do this.
#include <climits> // for INT_MIN, INT_MAX
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int size; // number of values in set inputed by user
int value; // number inputed by user
int i; // counter
int high = INT_MIN; //highest value
int low = INT_MAX; //lowest value
cout << "Enter the number of values in the set: ";
cin >> size;
for ( i = 0; i < size; i++ )
{
cout << "Enter a number: ";
cin >> value;
if(value > high)
{
high = value;
}
if(value < low)
{
low = value;
}
}
cout << "The maximum value is: " << high << "\n";
cout << "The minimum value is: " << low << endl;
return 0;
}
/* my output
Enter the number of values in the set: 5
Enter a number: -4
Enter a number: 9
Enter a number: 15
Enter a number: 2
Enter a number: -1
The maximum value is: 15
The minimum value is: -4
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
is a new header file for me. we have only used so far. my instructor refers to a std library occasionally; is this part of my software
Like, is part of the standard library. Others are listed here (click on the paws if you get the "Limited Access Notice" page).
(i'm using microsoft visual c++ 6.0 intro edition)My condolences. :pOut of curiosity...how long did it take for yall to learn C++? Did you learn it from a class or on your own?I'm still trying to learn on my own.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314