PDA

View Full Version : A bit of help with an assignment :\



Runescapian321
01-06-2008, 11:49 PM
I need to make a C program that finds the lowest and max of an undefined amount of numbers. Yeah, I know, that's kind of confusing, once I show you the program you'll get it, think it's easy, and then it's confusing all over again (that's what I went through anyways :p). So here is what I have so far
#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"

int main()
{
int num, num1;

printf("Please enter a list of integers.\n");
printf("Signal the end of your list with 0.\n");
printf("Enter the first number: ");
num = GetInteger();
num1 = 0;
while(num!=0)
{
printf("Enter the next number: ");
num = GetInteger();
if (num > num1)
num1 = num;
if (num1 >= num)
num1 = num1;
}
printf("The biggest number is %d\n", num1);
}
(made it in Scar tags so it would be easier to read, just disregard the green)

I got the biggest number, but I can't for the life of me figure out how to get the lowest one...It seems like it should be simple, but I've been tearing my hair out trying to figure it out for almost an hour...Any help anyone?

BenLand100
01-07-2008, 03:57 AM
#include "stdafx.h"
#include "simpio.h"
#include "genlib.h"

int main()
{
int num, min, max;

printf("Please enter a list of integers.\n");
printf("Signal the end of your list with 0.\n");
printf("Enter the first number: ");
num = GetInteger();
min = num;
max = num;
while(num!=0)
{
printf("Enter the next number: ");
num = GetInteger();
if (num > max)
max = num;
if (num < min)
min = num;
}
printf("The biggest number is %d\n", max);
printf("The smallest number is %d\n", min);
}That should work assuming I didn't get a syntax error from writing in the reply box :rolleyes:
Edit: assuming you disregard the zero as being a number in the list... otherwise (unless you count negatives) it will always be the least...

Runescapian321
01-07-2008, 04:14 AM
-.- Now that I look at that it seems so simple, I ended up doing it a longer, more complicated way that works, but the way you did it could come in handy later. Thanks :)

Wizzup?
01-07-2008, 12:41 PM
Instead of

if (num > max)
max = num;
if (num < min)
min = num;

You could use this:


Max = (Max > Num ? Max : Num);

BenLand100
01-07-2008, 08:07 PM
Instead of

if (num > max)
max = num;
if (num < min)
min = num;You could use this:


Max = (Max > Num ? Max : Num);
Although it is neater code, its not faster than a comparison and assigning since the tertiary operator will always assign a new value, and a comparison only assigns a new value when the condition is met.

Yakman
01-07-2008, 10:09 PM
Runescapian321 didn't specify if he wanted a super-fast application to out-process any competition.
if he wanted speed, he wouldn't read from standard IN, but get command line arguments or something.

Runescapian321, you could use [code] tags instead of [scar] tags, so you don't get the green

BenLand100
01-09-2008, 03:42 AM
Runescapian321 didn't specify if he wanted a super-fast application to out-process any competition.
if he wanted speed, he wouldn't read from standard IN, but get command line arguments or something.

Runescapian321, you could use [code] tags instead of [scar] tags, so you don't get the green
I'll remember that when I describe the difference between two methods to keep the differences strictly based on the program at hand... :rolleyes: Because everyone knows you should use the tertiary operator whenever possible *sarcasm* :)