Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,370 people online right now. Registration is fast and FREE... Join Now!




C: finding the min and max of a set of numbers scanned in.

 
Reply to this topicStart new topic

C: finding the min and max of a set of numbers scanned in.

scorpionviolinist
21 May, 2008 - 11:21 AM
Post #1

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

I am trying to find the max and min out of the numbers scanned in by the user. We have not yet covered arrays, so no arrays. I am able to find the min and max with a set amount of numbers.

CODE


#include <stdio.h>

int main(void)
{
   int nmbr = 0;
   int largest = 0;
   int smallest = 10000000;
    
        while (nmbr >= 0)
        {
          printf("Enter a number (enter -5 to end). \n");
          scanf("%lg", &nmbr);

             if (num > largest)
            largest = num;
             if (num < smallest)
             smallest = num;
        }
          printf("smallest value %d \n", smallest);
          printf("largest value %d \n", largest);
}

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 11:27 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
What problem are you having? Help us to help you smile.gif
User is online!Profile CardPM
+Quote Post

scorpionviolinist
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 11:37 AM
Post #3

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

I need to find the smallest and largest number of a set of numbers read in. The way that I have it gives me 1075314688 for the largest and -1074790400 for the smallest when I enter any combination of numbers... like 5, 7, 2, 9, etc...
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 11:50 AM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
OK, I've edited and commented your code as follows:
cpp

#include <stdio.h>

int main(void)
{
int nmbr = 0;
int largest = 0;
int smallest = 100000000;

while (nmbr != -5) // while nmbr isn't -5
{
// perform checks at the beginning, this way "-5" won't
// overwrite the smallest
if (nmbr > largest)
largest = nmbr;
if (nmbr < smallest)
smallest = nmbr;

printf("Enter a number (enter -5 to end). \n");
scanf("%d", &nmbr); // %d is an integer

}
printf("smallest value %d \n", smallest);
printf("largest value %d \n", largest);
}

If you have any more questions, just ask.

Hope this helps smile.gif

This post has been edited by gabehabe: 21 May, 2008 - 11:59 AM
User is online!Profile CardPM
+Quote Post

scorpionviolinist
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 01:04 PM
Post #5

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

Okay... Thank you.
But now it always says that the minimum value is 0. Is that because we already set max = num? IDK.
User is offlineProfile CardPM
+Quote Post

gabehabe
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 01:32 PM
Post #6

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Sorry, my bad. The best way IMO is to do it with an infinite loop, and break if nmbr == -5 like so:
cpp
#include <stdio.h>

int main(void)
{
int nmbr;
int largest;
int smallest;

while (true) // while nmbr isn't -5
{
printf("Enter a number (enter -5 to end). \n");
scanf("%d", &nmbr); // %d is an integer
if (nmbr == -5)
break;
if (nmbr > largest)
largest = nmbr;
if (nmbr < smallest)
smallest = nmbr;

}
printf("smallest value %d \n", smallest);
printf("largest value %d \n", largest);
}

Sorry about that unsure.gif
User is online!Profile CardPM
+Quote Post

scorpionviolinist
RE: C: Finding The Min And Max Of A Set Of Numbers Scanned In.
21 May, 2008 - 04:30 PM
Post #7

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

Thank you for the help ohmy.gif)

Did not mean for that smiley to show... lol smile.gif

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:01AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month