Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Simple (stupid cause I don't know it) basic question (http://www.programmingforums.org/showthread.php?t=3266)

massive-war Apr 10th, 2005 10:05 PM

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?

Encryption Apr 10th, 2005 10:17 PM

???
I didn't understand any of that.
How many numbers will be input?
Have you went over string/char management, like arrays?

Benoit Apr 10th, 2005 10:25 PM

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;
            }


massive-war Apr 10th, 2005 10:28 PM

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

Infinite Recursion Apr 10th, 2005 10:36 PM

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

massive-war Apr 10th, 2005 10:47 PM

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

massive-war Apr 10th, 2005 10:47 PM

I'll post my code in a sec...

Infinite Recursion Apr 10th, 2005 10:50 PM

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...

Infinite Recursion Apr 10th, 2005 10:51 PM

that is while ( ; ; )

massive-war Apr 10th, 2005 10:54 PM

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(;;)


All times are GMT -5. The time now is 4:17 PM.

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