![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
Simple (stupid cause I don't know it) basic question
I don't want to have to write 50 if statements for a prog...
how do you find the largest of the numbers that have been input? The thing is, I picked up this book, and they tell you to only use the stuff you've learned so far. But I've only learned if, if...else... and while statements (I haven't JUST learned them, but that's as far as I've gotten in the book. It's an accelerated c++ book, but it goes over the basics in the beginning, with harder problems.) Why I have to read the largest number isn't the point, but the point is I don't know the shortcut of getting the largest number. Can anyone help? simple function call? or what?
__________________
Support Our Troops |
|
|
|
|
|
#2 | |
|
Programmer
|
???
I didn't understand any of that. How many numbers will be input? Have you went over string/char management, like arrays?
__________________
Quote:
|
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4
![]() |
Use a bubble sort algorithm
Have all the numbers put into an array, in this case a[10] Have a varible to temporarily hold the value, and make the switch for (x=0; x < 10; x++)
for (y=0; y < 10; y++)
if (a[y] > a[y+1])
{
t=a[y];
a[y]=a[y+1];
a[y+1]=t;
}
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
thanks benoit. I was thinking of that, but for my class he won't let me use anything other than the stuff WE already learned, like as a class. We haven't covered arrays yet, but screw it, I'll use it anyway
thanks
__________________
Support Our Troops |
|
|
|
|
|
#5 |
|
Programming Guru
![]() ![]() ![]() |
I was bored... here is an example of getting the max number... i believe math.h has a max function you could use as an alternative... i'm sure some more complex data algorithms also have this available... but if all you had to use was the if else, etc... this will work.
#include <iostream>
#define EVER ;;
using namespace std;
int CheckMax (int x);
int GetNums (void);
int MAX = 0;
int main (void)
{
for (EVER)
{
GetNums();
}
}
int GetNums (void)
{
int myNum = 0;
cout << "Enter number (-1 to quit): " << endl;
cin >> myNum;
if (myNum != -1)
CheckMax(myNum);
else
{
cout << "The maximum value entered: " << MAX << endl;
exit(0);
}
return 0;
}
int CheckMax (int x)
{
if (x > MAX)
MAX = x;
return 0;
}Enter number (-1 to quit): 4 Enter number (-1 to quit): 19 Enter number (-1 to quit): 432 Enter number (-1 to quit): 51 Enter number (-1 to quit): 5326 Enter number (-1 to quit): 13526523 Enter number (-1 to quit): 234234 Enter number (-1 to quit): -1 The maximum value entered: 13526523
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
thanks infinite. That's excellent, I would have never thought of that lol...
anyways, I was supposed to use a while loop, but what I'll probably do is make a garbage call, like: ......... int x = 0; while (x = 0) ...... do the max number thing..... after all the max stuff is set up.... x = -1; so it quits the loop get what I'm saying, just make a stupid call to get the while statement in, which is a waste of memory and time, but he said he wants a while statement and I can't think of one anyway... Thanks though
__________________
Support Our Troops |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
I'll post my code in a sec...
__________________
Support Our Troops |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
lol... change my for"ever" loop into a while
while (; ![]() { GetNums(): } Normally, I don't like doing homework assignments... because the student doesn't learn that way... Hopefully, this thread was helpful and you have learned some new ways to do stuff...
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#9 |
|
Programming Guru
![]() ![]() ![]() |
that is while ( ; ; )
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0
![]() |
no way, I learn something new everyday... while(;
Now I've heard everything...I still think my garbage value function thing was a good idea. lol Thanks infinite, your awesome Note: BTW, I'm using your while(; ![]()
__________________
Support Our Troops |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|