Thread: program to find the range and average

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    program to find the range and average

    hi.
    i need to find the average and range of numbers entered by the user.
    the user can enter as many terms as possible.
    the user should enter a negative number to end.
    i was able to do it with "goto".
    but i need to do the program using if...else..
    here's the code using "goto"

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()
     {
      int count;
      float value,high,low,sum,average,range;
      sum = 0;
      count = 0;
      printf("Enter numbers in a row\n.Enter negative number to end");
      input:
         scanf("%f",&value);
    	 if(value<0) goto output;
    	   count = count + 1;
    	 if(count ==  1)
    	    high = low = value;
    	   else if(value>high)
    	       high = value;
    		   else if(value<low)
    		      low = value;
         sum = sum + value;
    	 goto input;
      output:
         average = sum/count;
    	 range = high - low;
    	 printf("\n\n");
    	 printf("Total values : %d\n",count);
    	 printf("Highest value : %f\nLowest value : %f\n",high,low);
    	 printf("Range    : %f\nAverage  : %f\n",range,average);
    	 return 0;
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Use
    Code:
    while ( scanf("%f",&value) == 1 && value >= 0 ) {
    }
    Now you can remove all you gotos and labels.

    Just put all the code you want to repeat, inside that loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    can you please post the entire code.
    Beacause i have learnt only till "if...else.." statements.
    I haven't yet learnt while loops.
    There in the next chapter in my textbook.
    So can you please post the entire code.

    And is it possible to solve it using "if..else.."?

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by narendrav View Post
    I haven't yet learnt while loops.
    There in the next chapter in my textbook.
    What's stopping your from turning a page and reading about it?
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    ok.I will first learn.and think about this problem.
    actually it's a very long chapter.
    and i am a first timer.So i thought if this problem could be solved using "if..else.."
    so that i can understand.
    any way i will learn and then try to solve it.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by narendrav View Post
    the user can enter as many terms as possible.
    because of this requirement you will HAVE to loop in some way...there is really no
    way to accept unlimited amount of input without having a loop (goto, while etc..)

    IE you would have to have a LIMIT on the amount of input and then have a scanf (hopefully better call then that..) to get EACH value (you could combine them into one scanf if you want the input all on one line...)

    then you can do your calculations with numerous if/else if/else statements (would depend on the finite limit of data, on how many you would need to accomplish this)

    Its really making your life 100 times more difficult NOT using a loop...(by difficult I mean impossible if you want UNLIMITED input)

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    You already have and if / else if chain, so you seem to have that figured out.

    You can't remove all the gotos with just if/else, since you need some kind of loop.

    You could do this, but is it necessarily any better than what you have?
    Code:
    if ( value < 0 ) {
      // all the code following output: label
    } else {
      // all the other code
    }
    goto input;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    yes.i understand.
    thanks @nonpuz.
    its practically impossible to continuosly read data without using loops.
    thanks for the detailed explanation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read n numbers and find their total, min, max, average
    By jackson6612 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2011, 04:04 PM
  2. find & print the average of the odd numbers
    By zozo995 in forum C++ Programming
    Replies: 1
    Last Post: 10-27-2010, 10:31 PM
  3. Why range error & how to fix it in this program?
    By pantera in forum C++ Programming
    Replies: 5
    Last Post: 01-15-2010, 07:49 AM
  4. function to find average
    By nynicue in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 05:30 PM
  5. How can I find the average in the ARRAYS?
    By Nate2430 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2001, 11:21 AM

Tags for this Thread