Coursework for to while

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    The problem i have would be printf("Average of %u values is %f.\n",n (n == 0) ? 0.0 : (double) sum / n); in the code on #3 because i have no idea what this means and apparently it is the problem for compiling. I have ask a few experience programmers and they say its another way to write a for statement is that true?
    I am changing the codes for statement to while just for a heads up.

  2. Relevant commands, code, scripts, algorithms:

/*      File : avg.c            *
 *      By   :                  *
 *      login:                  *
 *      team :                  *
 *      Date :                  */

/*
 * Compute average of its input values.
 */

#include <stdio.h>

int main()
{
  int          next;               /* next input value */
  long         sum;                /* running total */
  unsigned int n;                  /* number of input values */
  int          result;             /* did we read another value? */
  double       avg;                /* average of input values */

        for (sum = n = 0;
             (result = scanf("%d", &next)) == 1;
             n++)
                sum += next;   
        if (result != EOF)
                printf("Warning: bad input after reading %u values\n", n);
        printf("Average of %u values is %f.\n",
               n, (n == 0) ? 0.0 : (double) sum / n);
  
}
  1. The attempts at a solution (include all code and scripts):
/*      File : avg.c            *
 *       *      By   :Matthew Yee                  *
 *        *      login: mgy                 *
 *         *      team : Yun                 *
 *          *      Date : 04/05/2011                 */

/*
 *  * Compute average of its input values.
 *   */

#include <stdio.h>

int main()
{
  int          next;               /* next input value */
  long         sum;                /* running total */
  unsigned int n;                  /* number of input values */
  int          result;             /* did we read another value? */
  double       avg;                /* average of input values */
  sum = n = 0;
       

     while(result = scanf("%d", &next) == 1){
            n++; 
            sum += next; } 
            
                // n++;
                //sum += next;   
        if (result != EOF)
                printf("Warning: bad input after reading %u values\n", n);
        printf("Average of %u values is %f.\n",n (n == 0) ? 0.0 : (double) sum / n);
  
}
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Hawaii at Manoa, Honollulu (HI), Oahu(Hawaii), Tep Dobry, EE160 EE 160: Lab 10

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

That is no for-loop at all, but a if-then-else. In standard C

<condition> ? <command1> : <command2>

is just an abbreviated way of writing

if( <condition> ) {
     <command1> ;
} else {
      <command2> ;
}

It will do absolutely the same.

I hope this helps.

bakunin

1 Like

I think you dropped a comma.

printf("Average of %u values is %f.\n",n , (n == 0) ? 0.0 : (double) sum / n);

the program works now but the average show 0 every time i put an input. i think that it is my variable sum because the n variable went through other wise the math would be wrong. the sum kept seeing it as 0 and n sees it as how many variables there are going to be type in input. for example if i type 3 values, enter, type the values like 1 2 3 the average came to be zero

You have commented the only place where "sum" is incremented - small wonder it remains 0 all the time. ;-))

bakunin

1 Like