Like Tree1Likes

Homework Help, Clarification?

This is a discussion on Homework Help, Clarification? within the C Programming forums, part of the General Programming Boards category; I just wrote the following program which asks the user for an input of how many numbers they'd like to ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    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. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I think range means lowest and highest number. As you've done. Does the program work?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    76
    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????
    Last edited by dolfaniss; 04-18-2011 at 09:06 PM.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Can you copy the exact problem from the book? I don't understand either...

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #6
    Registered User
    Join Date
    May 2010
    Posts
    76
    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. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #8
    Registered User
    Join Date
    May 2010
    Posts
    76
    Thanks, that's what I'll do.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by CommonTater View Post
    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. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Just implement both, one at a time: after all, this is a learning exercise.
    CommonTater likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    76
    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. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clarification
    By WDT in forum C# Programming
    Replies: 1
    Last Post: 01-21-2009, 12:09 AM
  2. Need a clarification here
    By bithub in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 12-27-2004, 12:06 AM
  3. need clarification on something...
    By EvBladeRunnervE in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-08-2004, 08:45 AM
  4. FAQ Clarification
    By Ness in forum C Programming
    Replies: 13
    Last Post: 06-10-2003, 04:37 PM
  5. A little clarification?
    By Dr Nick in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2002, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21