Finding Average of floats

This is a discussion on Finding Average of floats within the C++ Programming forums, part of the General Programming Boards category; can some one tell me whats wrong with this program. i'm trying to find average of 10 floats and how ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Angry Finding Average of floats

    can some one tell me whats wrong with this program. i'm trying to find average of 10 floats and how do i use code tags?
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    
    void main()
    {
    	// declare all variables
    	float a[10];
    	
    	// ask user for numbers
    	cout << "What are the floating point numbers?" << endl;
    	cin  >> a[10];
    	
    	float average;
    	average = ((a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]+a[10])/10);
    	cout << "The average is " << average << endl;
    	
    
    }

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I added the code tags for you, but here's how you use them:

    put [code] before your code, then [/code] after your code and voila...check the link in my signture for more...

    (looks like this when typing)

    [code]

    cout << "hi";

    [/code]
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    well, first you need to actually get the floats from the user

    Code:
    for(int i = 0; i < 10; ++i)
    {
       cout<<"Enter number: ";
       cin>>a[i];
    }
    code tags are

    [ code ]
    and
    [ /code ]

    without the spaces.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    54
    thank you very much for adding code tags. i haven't used the board since a while and forgot. And thanks for that program help.
    Thank you.

  5. #5
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Code:
    #include <iostream.h>
    
    int main()
    {
    	float a[10];
    
    	cout << "What are the floating point numbers?" << endl;
    
    	for(int i = 0; i < 10; i ++)
    	{
    		cin >> a[i];
    	}
    
    	float average;
    	average = ((a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9])/10);
    	cout << "The average is " << average << endl;
    
    	return 0;
    }
    Remember the base of an array is 0
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    54

    Another Question

    Now how would i make the program to display which numbers have exceeded the average?

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Red face

    You can use a conditional statement (like an if...)
    Mr. C: Author and Instructor

  8. #8
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    that would be pretty simple..

    for each score
    if score > average
    print score is higher than average

  9. #9
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    I can't resist...

    Code:
    cout << "Numbers that exceed average: ";
    
    for(int j = 0; j < 10; j ++)
    {
    	if(a[j] > average)
    	{
    		cout << a[j] << " ";
    	}
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in finding average of a class of numbers
    By hastefan2001 in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2009, 01:11 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. problems finding the average of an array column
    By mrgeoff in forum C Programming
    Replies: 4
    Last Post: 04-18-2005, 11:49 PM
  5. Replies: 9
    Last Post: 11-20-2003, 07:55 PM

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