Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
-
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. -
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);
}
- 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);
}
- 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).