1. ## Homework Help, Clarification?

I just wrote the following program which asks the user for an input of how many numbers they'd like to enter in, adds them up, gets the average and displays the highest and lowest.

Now the book is asking me to modify the program such that is will display the range of values. What does that mean? Go easy, I've been out of school a long time. Here is the other program, which is fine, as a reference.

Code:
```#include <stdio.h>

int main (void)

{
int loop_count, lowest_num, highest_num, loop, initial, number, sum, actual_sum;
double average;

sum = 0;

printf ("Please enter the amount of numbers you would like to use>> ");
scanf ("%d", &loop_count);

while (loop_count < 2)
{
printf ("This program requires at least 2 inputs.\nPlease try again>> ");
scanf ("%d", &loop_count);
}
printf ("Please enter the first number>> ");
scanf ("%d", &initial);

lowest_num = initial;
highest_num = initial;

for (loop = 1; loop < loop_count; loop = loop +1)
{
printf ("Please enter the next number>> ");
scanf ("%d", &number);

if (number < lowest_num)
{
lowest_num = number;
}

else if (number > highest_num)
{
highest_num = number;
}
else
{
number = number;
}

sum = sum + number;

}
printf ("\nThe lowest number in the set is: %d\n\n", lowest_num);
printf ("The highest number in the set is: %d\n\n", highest_num);
actual_sum = sum + initial;
printf ("The sum of the numbers is: %d\n\n", actual_sum);
average = (double)actual_sum / (double)loop_count;
printf ("The average of the numbers is: %.2lf\n\n", average);
return 0;

}```

2. I think range means lowest and highest number. As you've done. Does the program work?

3. Yes, it works fine. Why is it lousy? LOL.

Yeah I was thinking they want the amount of numbers between the highest and lowest.

eg. lowest 25, highest 100 range 75????

4. Can you copy the exact problem from the book? I don't understand either...

5. No it's fine. I just don't have a ready compiler here to find any latent syntax errors, if that's what you're looking for. Your indentation could be more consistent but other than that it's OK.

6. The book says to create a program that will find the smallest, largest and average values in a collection of N numbers. Get the value of N before scanning each value in the collection of N numbers. I've accomplished this.

Then it says to modify the program to display a range of values. I'm thinking distance from lowest to highest?

7. Oh it says get smallest, largest, AND range. My mistake. But it could be sufficient to say "Range is <low> to <high>" which it already does.
In that case I have trouble understanding the question as well. By all means display <high> - <low> or better still <high> - <low> + 1.

8. Thanks, that's what I'll do.

9. I think the point is to see if you can modify the program without screwing it up

I would interpret "range" to mean the difference between the lowest and highest values.

10. Originally Posted by CommonTater
I think the point is to see if you can modify the program without screwing it up

I would interpret "range" to mean the difference between the lowest and highest values.
Listen to CommonTater...In every math class I've taken Range means difference in the highest and lowest value. Example: 1, 2, 7, 9 Highest = 9 Lowest = 1 Range = 8

11. I would interpret range to be highest - lowest + 1. If the limits are 13,14, then the "range" spans 2 possible numbers. 14 - 13 + 1.

12. Just implement both, one at a time: after all, this is a learning exercise.

13. Thanks guys the modified version is done, but I have another question about this one and another one I'm going to write. In class right now we are doing loops, I always seem to have to do one calculation outside of the loop and am wondering if I'm cheating. In this program here (I will re code tag it) is there anyway to get the highest and lowest number from the user's input of N numbers without getting the first input outside of the loop? I couldn't think of a way. Remember we are only at loops so no fancy library functions, or array or string parsing as we aren't that far along yet.

Code:
```#include <stdio.h>

int main (void)

{
int loop_count, lowest_num, highest_num, loop, initial, number, sum, actual_sum;
double average;

sum = 0;

printf ("Please enter the amount of numbers you would like to use>> ");
scanf ("%d", &loop_count);

while (loop_count < 2)
{
printf ("This program requires at least 2 inputs.\nPlease try again>> ");
scanf ("%d", &loop_count);
}
printf ("Please enter the first number>> ");
scanf ("%d", &initial);

lowest_num = initial;
highest_num = initial;

for (loop = 1; loop < loop_count; loop = loop +1)
{
printf ("Please enter the next number>> ");
scanf ("%d", &number);

if (number < lowest_num)
{
lowest_num = number;
}

else if (number > highest_num)
{
highest_num = number;
}
else
{
number = number;
}

sum = sum + number;

}
printf ("\nThe lowest number in the set is: %d\n\n", lowest_num);
printf ("The highest number in the set is: %d\n\n", highest_num);
actual_sum = sum + initial;
printf ("The sum of the numbers is: %d\n\n", actual_sum);
average = (double)actual_sum / (double)loop_count;
printf ("The average of the numbers is: %.2lf\n\n", average);
return 0;

}```

14. Assuming you mean this...

Code:
```printf ("Please enter the first number>> ");
scanf ("%d", &initial);
lowest_num = initial;
highest_num = initial;

for (loop = 1; loop < loop_count; loop = loop +1)
{```
You can remove the first scanf() and initialize highest and lowest to impossible values...
Code:
```lowest_num = 2000000000;
highest_num = -1000000000;```
... then start your loop at 0.

Since lowest is ridiculously high, it will be changed by the first number entered.
Similarly since highest is ridiculously low the first number entered will catch it as well.
From there normality will be restored.

There are values INT_MAX and INT_MIN in limits.h you can use, but you said you didn't want library functions...

15. Yup. Just make the loop go for the number of expected times. for (loop = 0; loop < loop_count
lowest_num must be initialized to some huge number INT_MAX or avoid the test if it's the first time.
highest_num must be initialized to some small number INT_MIN or avoid the test of it's the first time.