I need to code a program for my C++ class that finds the smallest and largest number that is put into the program. I am brand new to the language so any help is greatly appreciated.
Any normal class provides an introduction into the language with some sort of course guide. At least when I took comp sci classes that was the format. We'd get a lecture on a subject, look through our course guide that provides sample code, examples, library references, and other info, and then finally get onto a computer and work through a problem.
There aren't many people who will simply give you code for classwork, that is what your head is for.
Look through any documentation your class provides, and if you don't understand it or need help with it, ask here. The majority of us will explain how code works rather than just give you code you won't understand.
Plus, you'll learn the language better that way. :buddy:
I have a basic understanding of if else statements and some basic control structures. I have an idea of how to find it using if and statements(that's what they are right?) but i'm not exactly sure how to execute it.
The most straightforward approach would be to have a variable that holds the largest number seen "so far", initially set to zero or something. As you read numbers (using a loop), compare each against the "largest so far" variable, and if the new number is larger, store it into the variable (replacing the old value).
[QUOTE=Wyzard;17361993]The most straightforward approach would be to have a variable that holds the largest number seen "so far", initially set to zero or something. As you read numbers (using a loop), compare each against the "largest so far" variable, and if the new number is larger, store it into the variable (replacing the old value).[/QUOTE]
Here is what it would look like.
[code]
for(int i=0; i<totalnumbers; i++)
{
if(numberArray[i] > largestNumber)
largestNumber = numberArray[i];
if(numberArray[i] < smallestNumber)
smallestNumber = numberArray[i];
}[/code]
You could also use std::max and std::min for shorter and cleaner code.
[cpp]
#include <algorithm>
for(int i=0; i<totalnumbers; i++)
{
largestNumber = std::max(numberArray[i], largestNumber);
smallestNumber = std::min(numberArray[i], smallestNumber);
}
[/cpp]
And include another header just for those two functions that do nothing but the same as above? I'd rather just clean it up by using the ternary operator.
[cpp]
for(int i=0; i<totalnumbers; i++)
{
largestNumber = (numberArray[i] > largestNumber) ? numberArray[i] : largestNumber;
smallestNumber = (numberArray[i] < smallestNumber) ? numberArray[i] : smallestNumber;
}
[/cpp]
Precompiled Headers.
There's nothing wrong with including a header to use a couple functions from it.
[QUOTE=Spoco;17365774]And include another header just for those two functions that do nothing but the same as above? I'd rather just clean it up by using the ternary operator.[/QUOTE]
Yes, precisely that. There's nothing bad about having shorter, cleaner code. Including the header is mostly irrelevant.
Might as well just sort it while you are at it and take the first and last numbers as the highest. Odds are you'll need to sort the data anyway.
Otherwise just use std::min and max as suggested above.
Alternatively, you could just have it guess the highest and lowest numbers.
[code]
Enter a number (f to finish): 10
Enter a number (f to finish): -220
Enter a number (f to finish): -98
Enter a number (f to finish): 46
Enter a number (f to finish): 25
Enter a number (f to finish): f
uhhh... how about... 30 was the highest.... and... 67 was the lowest?
[/code]
People are talking about using arrays containing all the numbers, but if you examine each number right when you receive it, there's no need to store whole list in memory.
Sorry, you need to Log In to post a reply to this thread.