| | |
beginners program; can't get min value
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
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
if (b <= max) min = b;
C++ Syntax (Toggle Plain Text)
int max=0; //highest vaule int min=0; //lowest value
"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
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
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
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Solved Threads: 0
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]
# 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]
•
•
•
•
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.
You declare 5 integer variables, but none are initialized to any value -- so their values are completely random.
C++ Syntax (Toggle Plain Text)
# 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
C++ Syntax (Toggle Plain Text)
//initialization phase max = b; min = b;
C++ Syntax (Toggle Plain Text)
//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++ ) {
C++ Syntax (Toggle Plain Text)
cout << "Enter a number: "; //prompt for imput cin >> b; //read number from user
C++ Syntax (Toggle Plain Text)
if (b > max) max = b; if (b < min) min = b;
C++ Syntax (Toggle Plain Text)
} cout << "The maximum value is: " << max << "\n"; cout << "The minimum value is: " << min << " " <<endl; return 0; //indicates that program ended successfully } //end function main
C++ Syntax (Toggle Plain Text)
#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 */
"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
<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?
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?
•
•
•
•
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
•
•
•
•
Originally Posted by mtjuarez
(i'm using microsoft visual c++ 6.0 intro edition)
•
•
•
•
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?
"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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: A little help please?
- Next Thread: Problem getting SDL to work with Dev-C++
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






