To find the max/min/average of 5 numbers (input from the user), must there be use of loop statements? Can it be only "if" statements or would that be ridiculous and long?
Reason I am asking is because our teacher assigned us this project, but we are between the "if" chapter and the "loop" chapter, only he hasn't discussed the "loop" chapter yet so I don't know if I am supposed to assume he means for us to read it and do the project or if he meant for us to use "if" statements.
You only need a loop if you're generalizing to n (i.e. some unknown amount of) elements.
Most good programers tend to generalize as much as possible though (assuming it avoids unnecessary overhead.) It's just good habit, as you never know when you'll want to change your program to accommodate new conditions, or when you'll want to reuse your code for something slightly different.
In this case you have the right feeling. A loop makes more "sense" (in the way described above.) If you were to change the program from 5 inputs to 10 inputs you would only need to change a few (possibly only 1 or 2) things using a loop; but, if you use if statements, you'll have to change/add a lot!
If you find yourself often copy and pasting large chunks of code, then there might be (and most likely is) a better way to do it.
Most programmers would go for the loop option, I think. What if you had 100 or 1000 numbers to deal with?
Here's an out line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
constunsigned LIMIT = 5;
//variable for max
//variable for min
//variable for sum
//variable average - needs to be double
for (unsigned count = 0;count < LIMIT ; count++) {
//get input
//add to sum variable - use += operator
//decide if it max
//decide if it is min
}
//calc average - use the count variable, but should to cast it to double.
// I like to always do explicit casts
//print results
Notice how I have written out the methodology with comments? This is a good way to do basic design for your code. It helps organise your thoughts logically, and identify which code might be in functions. Leave the comments there as they form documentation.
This can be done incrementally - start with very general ideas , then go back and fill in more detail (as many times as you need), then write the code.
Also the layout of the file should be like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//includes
//using statements
using std::cout;
using std::cin;
using std::endl;
//similar for any other std thing
// otherwise put std:: before each std thing in the code - strings, vectors, lists etc
//function declarations
int main() {
return 0;
}
//function definitions
Finally I like to point people to the reference section on this site (and the articles), go to the top left of this page. Some how most people seem to be blissfully unaware that it is there. Another thing people seem to be unaware of - Google and wiki are your best friends
Hope this helps - I look forward to helping out in the future - GOOD LUCK & have fun !!!