Menu

Help please!

Andrew
2007-09-29
2012-09-26
  • Andrew

    Andrew - 2007-09-29

    Hello! I am using Dev C++ at school in my basic programming class. I have to create a program that will ask the user to input number until they hit the 0 button. When they do that the program will calculate the average of the numbers inputted and tell the user what it is. This is what I have so far and I am completely stuck on how to go about my issue. All help will be appreciated!

    include<stdio.h>

    include<stdlib.h>

    int main()
    {
    int input; // this telles the computer to read what the user typed
    int one; // vareable for first scanf statement
    float average;
    int counts;
    printf("Please enter a number, when finished, type 0:\n");
    scanf("%d",&input);
    one += input;

    while(input != 0)
    {
    printf("next:");
    scanf("%d", &input);
    one += input;
    }
    printf("%d %d\n",one, counts);
    average = one / counts;
    printf("Average= %.2f\n", average);

    system("pause");
    return 0;

    }

     
    • Anonymous

      Anonymous - 2007-09-29

      ... but what is your question!?

      You have written some code, what is wrong with it? In what way does it not work or is incomplete?

      We could possibly work that out for ourselves by attempting to compile and run your code, or carefully analysing it. Or, since you presumably already know, you could just tell us!

      Incidentally, what if the series of values you wished to average included zero!?

      Clifford

       
    • Andrew

      Andrew - 2007-09-30

      I am trying to calculate the average of the numbers inputted by the user. When I compile and run the program it does not give me the average and it always gives me a bunch or random numbers at the bottom of the screen.

       
    • Kurgusov

      Kurgusov - 2007-09-30

      You have not initialised int one so it contains a garbage value, then you '+=' so you add to the garbage value.
      Thats why its always a safe option to initialise your variables.

       
    • Kurgusov

      Kurgusov - 2007-09-30

      Also you hav'nt assigned a value to 'counts' ?

       
    • Andrew

      Andrew - 2007-10-01

      Okay, I will try that. One more question. Is there a way for the program to count the amount of numbers the user put in? Ex. the user types: 4 5 6 7 3 that would be 5 times. Just wondering.

       
    • Anonymous

      Anonymous - 2007-10-01

      Consider the following fragment:


      int count = 0 ;
      int numbers[MAX_NUMBERS] ;
      char buffer[BUFFER_LEN] ;
      int check ;
      int i ;

      printf( "Enter number list (up to %d):", MAX_NUMBERS ) ;
      fgets( buffer, BUFFER_LEN, stdin ) ;

      do
      {
      check = sscanf( buffer, "%d", &numbers[count] ) ;

      } while( count < MAX_NUMBERS && check != 0 )

      for( i = 0; i < count; i++ )
      {
      printf( "%d: %d\n", i, numbers[i] ) ;
      }


      There are of course many other ways.

      Clifford

       
      • Soma

        Soma - 2007-10-01

        O_o

        "There are of course many other ways."

        There had better be. Your code is wrong. Your code is an infinite loop.

        Soma

         
    • Kurgusov

      Kurgusov - 2007-10-01

      I think that was the point though,i.e. prompting OP to learn how to iterate count?

      Or Clifford needs another coffee?

       
    • Andrew

      Andrew - 2007-10-01

      Is there a way for the program to count the amount of numbers the user put in and how would i incorporate it with my code?

       
      • Soma

        Soma - 2007-10-01

        Clifford's code shows you a valid approach.

        Soma

         
    • Andrew

      Andrew - 2007-10-01

      Also, I am kinda confused on the counts thing. I am not sure how to tell the computer how to count the amount of times someone types in a number.

       
    • Kurgusov

      Kurgusov - 2007-10-01

      If you want to count how many people walk into your shop,you count them as they walk through the door.
      If you want to count how many time a person enters a number,you count as they....?

      and dont say "walk through the door".

       
    • Andrew

      Andrew - 2007-10-01

      Type?

       
    • BiT

      BiT - 2007-10-01

      Depends if these numbers are entered one at a time ie I press 4 then hit enter or I press 4 then space bar then 2 ect.....you need to think about it Andrew!

      do you have to read each character as it is entered or do you need to read the entire line.......you need to think about your approach to what it is you are trying to accomplish maybe jot down some notes so you can tackle each problem one at a time, ie How do I read the character or line that was entered. How do I increment it, how do I ect......

      and if you need to read something(not the best resource but you seem to be a little clueless here)
      http://newdata.box.sk/bx/c/

       
    • Anonymous

      Anonymous - 2007-10-02

      Ok, the code was typed in directly untested, it should give you the gist, but does contain errors/typos. My appologies. And it is only a "fragment" as I mentioned; you have to wrap it into a valid function and provide your own MAX_NUMBERS and BUFFER_LEN.

      Here are the corrections to the errors I spotted, I still have not compiled or executed it - sometimes you have to do some thinking for yourself!


      int count = 0 ;
      int numbers[MAX_NUMBERS] ;
      char buffer[BUFFER_LEN] ;
      int check ;
      int i ;

      printf( "Enter number list (up to %d):", MAX_NUMBERS ) ;
      fgets( buffer, BUFFER_LEN, stdin ) ;

      do
      {

      check = sscanf( buffer, "%d", &numbers[count] ) ;
      count++ ;

      } while( count < MAX_NUMBERS && check != 0 ) ;

      for( i = 0; i < count; i++ )
      {
      printf( "%d: %d\n", i, numbers[i] ) ;
      }


       
    • Anonymous

      Anonymous - 2007-10-02

      One post:
      >> Is there a way for the program to count the amount of numbers the user put in?

      Another post:
      >> Is there a way for the program to count the amount of numbers the user
      >> put in and how would i incorporate it with my code?

      And another:
      >> I am not sure how to tell the computer how to count the amount
      >> of times someone types in a number.

      Now forgive me, but did you not just ask the same question three times while ignoring the answers provided!?

      Clifford

       
    • Andrew

      Andrew - 2007-10-02

      Thank you for the help, I have gotten it to work.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.