beginners program; can't get min value

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 8
Reputation: tj is an unknown quantity at this point 
Solved Threads: 0
tj's Avatar
tj tj is offline Offline
Newbie Poster

beginners program; can't get min value

 
1
  #1
Sep 23rd, 2004
Hello all....I can't seem to figure out why the min value won't display correctly. Could someone please critique my program with out actually giving me the code.

I am so happy that I finally got my loop to work! I've been working on this all evening. It is a great satisfaction when you get something to actually work!

Here is what I came up with:

# 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=0; //highest vaule
int min=0; //lowest value

//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++ ) {

cout << "Enter a number: "; //prompt for input
cin >> b; //read number from user

if (b <= max)
min = b;

if (b > max)
max = b;
}

cout << "The maximum value is: " << max << " ";
cout << "The minimum value is: " << min << " " <<endl;

return 0; //indicates that program ended successfully

} //end function main
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: beginners program; can't get min value

 
0
  #2
Sep 23rd, 2004
      if (b <= max)
          min = b;
Perhaps you should compare the current min.
  1. int max=0; //highest vaule
  2. int min=0; //lowest value
You would probably want to initialize both to the first element in the list or else set the max to the lowest int value (INT_MIN) and the min to the highest int value (INT_MAX).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8
Reputation: tj is an unknown quantity at this point 
Solved Threads: 0
tj's Avatar
tj tj is offline Offline
Newbie Poster

Re: beginners program; can't get min value

 
0
  #3
Sep 24th, 2004
That makes more sense than what I had down. Thank you!

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.

Here is the prog again with the corrections

# 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

//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++ ) {

cout << "Enter a number: "; //prompt for imput
cin >> b; //read number from user

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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 1
Reputation: kevin85 is an unknown quantity at this point 
Solved Threads: 0
kevin85 kevin85 is offline Offline
Newbie Poster

Re: beginners program; can't get min value

 
1
  #4
Sep 24th, 2004
Why don't you try out this instead?
# 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

//processing phase
//get input from user
cout << "Enter the number of values in the set: "; //prompt for input
cin >> a; //read number from user

// this number is used as the starting variable considered as max value
cout << "Enter a number: "; //prompt for imput
cin >> b; //read number from user

max=b;
min=b;

int d; // create a new variable

for ( c=1; c < a; c++ ) {

cout << "Enter another number: ";
cin >> d;

if(d > max) // if the num > max, then num is bigger
max = d;

else if (d < min) // if the second num < min, then num is smaller
min = d;
}

cout << "The maximum value is: " << max << "\n";
cout << "The minimum value is: " << min << " " <<endl;


return 0; //indicates that program ended successfully

} //end function main[/QUOTE]
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: beginners program; can't get min value

 
0
  #5
Sep 24th, 2004
Originally Posted by mtjuarez
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.
  1. # include <iostream>
  2. using std::cout; //program uses cout
  3. using std::cin; //program uses cin
  4. using std::endl; //program uses endl
  5.  
  6. //function main begins program execution
  7. int main ( )
  8. {
  9. int a; //number of values in set inputed by user
  10. int b; //number inputed by user
  11. int c; //counter
  12. int max; //highest vaule
  13. 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.
  1. //initialization phase
  2. max = b;
  3. min = b;
  1. //processing phase
  2. //get input from user
  3. cout << "Enter the number of values in the set: "; //prompt for input
  4. cin >> a; //read number from user
  5.  
  6. for ( c=1; c <= a; c++ ) {
Now you finally get a value for b.
  1. cout << "Enter a number: "; //prompt for imput
  2. 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.
  1. if (b > max)
  2. max = b;
  3.  
  4. if (b < min)
  5. min = b;
  1. }
  2. cout << "The maximum value is: " << max << "\n";
  3. cout << "The minimum value is: " << min << " " <<endl;
  4.  
  5.  
  6. return 0; //indicates that program ended successfully
  7.  
  8. } //end function main
Here is another way to do this.
  1. #include <climits> // for INT_MIN, INT_MAX
  2. #include <iostream>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. int main()
  8. {
  9. int size; // number of values in set inputed by user
  10. int value; // number inputed by user
  11. int i; // counter
  12. int high = INT_MIN; //highest value
  13. int low = INT_MAX; //lowest value
  14.  
  15. cout << "Enter the number of values in the set: ";
  16. cin >> size;
  17.  
  18. for ( i = 0; i < size; i++ )
  19. {
  20. cout << "Enter a number: ";
  21. cin >> value;
  22.  
  23. if(value > high)
  24. {
  25. high = value;
  26. }
  27. if(value < low)
  28. {
  29. low = value;
  30. }
  31. }
  32. cout << "The maximum value is: " << high << "\n";
  33. cout << "The minimum value is: " << low << endl;
  34. return 0;
  35. }
  36.  
  37. /* my output
  38.   Enter the number of values in the set: 5
  39.   Enter a number: -4
  40.   Enter a number: 9
  41.   Enter a number: 15
  42.   Enter a number: 2
  43.   Enter a number: -1
  44.   The maximum value is: 15
  45.   The minimum value is: -4
  46.   */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8
Reputation: tj is an unknown quantity at this point 
Solved Threads: 0
tj's Avatar
tj tj is offline Offline
Newbie Poster

Re: beginners program; can't get min value

 
0
  #6
Sep 24th, 2004
<climits> is a new header file for me. we have only used <iostream> so far. my instructor refers to a std library occasionally; is this part of my software (i'm using microsoft visual c++ 6.0 intro edition)? Is that where I pull up headers to use?

I am going to keep playing with statements and headers and see what kind of monster I can create....lol

Thanks for your help guys!
PS
Out of curiosity...how long did it take for yall to learn C++? Did you learn it from a class or on your own?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: beginners program; can't get min value

 
0
  #7
Sep 24th, 2004
Originally Posted by mtjuarez
<climits> is a new header file for me. we have only used <iostream> so far. my instructor refers to a std library occasionally; is this part of my software
Like <iostream>, <climits> is part of the standard library. Others are listed here (click on the paws if you get the "Limited Access Notice" page).

Originally Posted by mtjuarez
(i'm using microsoft visual c++ 6.0 intro edition)
My condolences. :p

Originally Posted by mtjuarez
Out 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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: chound is an unknown quantity at this point 
Solved Threads: 1
chound chound is offline Offline
Junior Poster
 
0
  #8
Sep 25th, 2004
How do you delete posts
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC