Average using for loop

This is a discussion on Average using for loop within the C Programming forums, part of the General Programming Boards category; [FONT="Comic Sans MS"][/FONT] Hi.. Pls help me wid solving this program : WAP using a for loop to read in ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Lightbulb Average using for loop

    [FONT="Comic Sans MS"][/FONT]


    Hi..

    Pls help me wid solving this program :

    WAP using a for loop to read in a set of numbers and print out their average.
    The program will start by prompting the user for the number of numbers to be read in and will then prompt for the individual numbers with a prompt such as

    enter number 2 ..to indicate to the uset which data item is currently being entered.Do something special when prompting for the last number.
    Note that there is no need to store all the individual numbers , it is sufficient to maintain a running total.

    Thanks in advance for your help and time
    funs

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Please read "Forum Guidelines" and "Homework policy".
    Thanks in advance
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by funskoolz View Post
    [FONT="Comic Sans MS"][/FONT]
    You lost me there.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Code:
     
    
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    	int n,i,k,sum=0,ave=0;
    	printf("Number of numbers to be read: \n");
    	scanf("%d", &n);
    		for(i=0;i<n;i++)
    		{
    			if(i+1==n)
    			{
    				printf("you are about to enter the last number\n");
    			}
    			printf("enter number %d\n", i+1);
    			scanf("%d", &k);
    			sum=sum+k;
    		}
    		ave=sum/n;
    		printf("ave=%d",ave);
    		
    }

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    u can modify ur code by declaring ave as float and also calculate ave as follows so that it can take float values.
    Code:
    ave=1.0*sum/n;
    printf("ave=%f",ave);

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    1.0 is double - 1.0f is float
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Average score
    By wiz23 in forum C++ Programming
    Replies: 22
    Last Post: 05-05-2005, 05:38 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21