Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2005, 10:05 PM   #1
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
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
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 10:17 PM   #2
Encryption
Programmer
 
Encryption's Avatar
 
Join Date: Feb 2005
Posts: 42
Rep Power: 0 Encryption is on a distinguished road
Send a message via AIM to Encryption Send a message via MSN to Encryption Send a message via Yahoo to Encryption
???
I didn't understand any of that.
How many numbers will be input?
Have you went over string/char management, like arrays?
__________________
Quote:
Originally Posted by Bjarne Stroustrup
* "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do succeed, you will blow away your whole leg."

* "I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone."
Encryption is offline   Reply With Quote
Old Apr 10th, 2005, 10:25 PM   #3
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 548
Rep Power: 4 Benoit is on a distinguished road
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
Benoit is offline   Reply With Quote
Old Apr 10th, 2005, 10:28 PM   #4
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
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
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 10:36 PM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2005, 10:47 PM   #6
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
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
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 10:47 PM   #7
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
I'll post my code in a sec...
__________________
Support Our Troops
massive-war is offline   Reply With Quote
Old Apr 10th, 2005, 10:50 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2005, 10:51 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
that is while ( ; ; )
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2005, 10:54 PM   #10
massive-war
Hobbyist Programmer
 
massive-war's Avatar
 
Join Date: Mar 2005
Location: Illinois--> My room
Posts: 117
Rep Power: 0 massive-war is an unknown quantity at this point
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
massive-war is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC