Coursework for to while

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Coursework for to while
# 1  
Old 04-06-2011
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:
Code:
/*      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);
  
}


3. The attempts at a solution (include all code and scripts):
Code:
/*      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);
  
}



4. 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).

Moderator's Comments:
Mod Comment edit by bakunin: please use CODE-Tags when posting code, terminal output or something such. Thank you.

Last edited by bakunin; 04-06-2011 at 03:43 PM..
# 2  
Old 04-06-2011
That is no for-loop at all, but a if-then-else. In standard C

Code:
<condition> ? <command1> : <command2>

is just an abbreviated way of writing

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

It will do absolutely the same.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 04-06-2011
I think you dropped a comma.
Code:
printf("Average of %u values is %f.\n",n , (n == 0) ? 0.0 : (double) sum / n);

# 4  
Old 04-07-2011
Error average never show

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
# 5  
Old 04-08-2011
Quote:
Originally Posted by mgyeah
Code:
                // n++;
                //sum += next;

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

bakunin
This User Gave Thanks to bakunin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

About Rules for Homework & Coursework Questions Forum

Hi, in my case, I have a question for topics that are slightly dealt with in class, which I am investigating on my own but which are not directly related to the lessons. Do I have to put the name of the school and others? (I can't put the name of the professor because is against the school rules... (2 Replies)
Discussion started by: Naky
2 Replies

2. Homework & Coursework Questions

Can anybody help me with coursework

This is the coursework, in Technical University Sofia, Bulgaria prof. Koshmarov, sorry but the link to the course is broken. And this is the condition: 1. If the value of x is 5, and you have been set a new value as such x="expr $x + 10", What would be the value of x? What... (1 Reply)
Discussion started by: astralito
1 Replies

3. Homework & Coursework Questions

Community Spirit and Ethos on Homework & Coursework

Helping students is in line with the "community spirit" ethos of The UNIX and Linux Forums which aims to give free help and advice in order to support the personal and professional development of others. We encourage students to think carefully about what they do, and do not, understand about... (0 Replies)
Discussion started by: Neo
0 Replies

4. Homework & Coursework Questions

Rules for Homework & Coursework Questions Forum

Homework Help: On Posting Questions: Any and all high school and undergraduate homework assignments or textbook style exercises for which you are seeking assistance are to be posted only in our Homework & Coursework Questions area--not in blogs, visitor messages, PMs, or the main technical... (0 Replies)
Discussion started by: Neo
0 Replies
Login or Register to Ask a Question