Thread: Array problem

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Question Array problem

    i have created the following to display the numbers entered by a user in an array can anybody tell me how i can find out the average of the ten integer numbers entered and display the total. i know ill have to change the variables to floats to accomodate decimal points. im a begginer
    Code:
    #include <iostream.h>
    #define MAX 10
    main()
    {
        int number[MAX];
        int index;
        for (index = 0; index < MAX; index++)
        { 
            
            cout <<"Please enter a number  ";
            cin >> number[index];
        }
        for (index = 0; index < MAX; index++)
        {
            cout <<endl;
            cout << "The contents of the array is as follows";
            cout << (index + 1) << " is ";
            cout << number[index];
        }
        cout << endl;
       
        return(0);
    }
    [edit]Code Tags added by Hammer.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Simple!

    Iterate over all elements, adding it to a temporary variable. Then divide by the number of elements.
    Code:
    int Sum = 0;
    for(int i=0; i<MAX; i++)
    {
       Sum += Number[i];
    }
    Sum = Sum / MAX;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM