Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,431 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,905 people online right now. Registration is fast and FREE... Join Now!




Finding the Average

 
Reply to this topicStart new topic

Finding the Average

smklem
17 Jan, 2008 - 07:47 PM
Post #1

New D.I.C Head
*

Joined: 17 Jan, 2008
Posts: 3

hello everyone. i am trying to do a program where i find enter the height's of students. crazy.gif it should end like this
The program should read in all the numbers and make a table like this:


height number of pupils
160 2
164 1
165 2
167 1
... ...
... ...
188 1
average height 174.0

i get it to read the numbers and sort them. this is what i have:
CODE
#include <iostream>
    using namespace std;
    
    void fillArray (int a[], int size, int& numberUsed);
    void sort(int a[], int numberUsed);
    void swapValues(int& v1, int& v2);
    int indexOfSmallest(const int a[], int startIndex, int numberUsed);
    
    double computeAverage(int a[], int numberUsed);
void showDifference(int a[], int numberUsed);
        
    int main()
{
    cout << "This program finds the average of pupil's heights. \n";
    int sampleArray[20], numberUsed;
    
fillArray (sampleArray, 20, numberUsed);
    sort(sampleArray, numberUsed);
    
    cout << "Sorted: \n";
    for (int index=0; index < numberUsed; index++)
        cout << sampleArray[index] << " ";
        cout << endl;
    system ("pause");
    return 0;
}
void sort (int a[], int numberUsed)
{
     int indexOfNextSmallest;
     for (int index = 0; index < numberUsed -1; index++)
     {
         indexOfNextSmallest =
         indexOfSmallest(a, index, numberUsed);
         swapValues(a[index], a[indexOfNextSmallest]);
     }
        
}
void swapValues (int& v1, int& v2)
{
      int temp;
      temp = v1;
      v1 = v2;
      v2 =  temp;
  }
int indexOfSmallest(const int a[], int startIndex, int numberUsed)
{
    int min = a[startIndex],
            indexOfMin = startIndex;
    for (int index = startIndex +1; index < numberUsed; index++)
         if (a[index] < min)
         {
                      min = a[index];
                      indexOfMin = index;
         }
    return indexOfMin;
}
void fillArray (int a[], int size, int& numberUsed)
{
     cout << "Enter the pupil's height's. \n"
     << "Mark the end of list with negative number \n";
          int next, index = 0;
          cin >> next;
          while ((next >= 0) && (index < size))
          {
                a[index] = next;
                index++;
                cin >> next;
          }
          numberUsed  = index;
    
}
    void showDifference(int a[], int numberUsed)
    
    {
         double average = computeAverage(a, numberUsed);
         cout <<"Average of pupil's height " << numberUsed << endl;
         for (int index =0; index < numberUsed; index++)
         cout << a[index] << (a[index] - average) << endl;
    
}
double computeAverage(int a[], int numberUsed)
{
                    }


please help i am lost!!!!!
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Finding The Average
17 Jan, 2008 - 08:08 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
I assume you're not sure how to find an average.

There are actually 3 different kinds of average. There is the mean, the mode, and the median. I assume you are trying to find the mean.

To do that, simply add up all the heights, then divide that by the number of entries.

i.e. (sum of heights / number of heights)
User is offlineProfile CardPM
+Quote Post

smklem
RE: Finding The Average
17 Jan, 2008 - 10:12 PM
Post #3

New D.I.C Head
*

Joined: 17 Jan, 2008
Posts: 3

i know how to find an average by math, but to put that into c++ code is what im having problems with. can anyone point me in the right direction? thanks
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Finding The Average
17 Jan, 2008 - 10:52 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, well I can't write the code for you, especially because this is an academic assignment.

All I can do is give you some pointers, and you have to work it out yourself.

This is EXACTLY what you need to do (but I've written it in psuedo-code)

1. Sum As Integer = 0
2. For each item in array
2. a) Sum = Sum + item

3. MeanAverage = Sum / numberUsed


Perhaps, you can't put that into code yourself, so you need to learn. Here's a simple example of adding a number:
CODE

int a = 3; // make variable, a, set it to 3
int b = 5; // ditto

b += 3; // add 3 to b
b += a; // add a to b

cout << b; // send b to console

b will start as 5. Then it will change to 8 (5+3). Then it will change to 11 (8+3). Then it will be pushed to the console (i.e. printed/displayed)

If you want to do it multiple time, you must use a for loop. This loop below will count from 0 to 4 (i.e. the code inside will be run 5 times)

CODE

int n = 0; // create integer, n, set it to 0
for(int i = 0; i < 5; i++) {
  n+=2;
}
cout << n;


This time 10 will be pushed to the console. Because n starts at 0, and adding 2 to it 5 times makes it ten.


If you need to access an array index:
CODE

int arr[5]; // this creates an array with 5 elements (from 0 to 4)

int i = 3; // create an int, set it's value to 3.

arr[i] = 34; // access array element 3 (the 4th element) and set it to 34

cout << arr[3]; // send "34" to console.


If you read this post, and understand it, you have more than enough information to write your mean calculation routine.

All you need to do is make a for loop. Access the array using i, add each value in the array to sum, divide sum by the number of values. All code examples I gave you have one of the elements that you need.

This post has been edited by Nayana: 17 Jan, 2008 - 11:04 PM
User is offlineProfile CardPM
+Quote Post

smklem
RE: Finding The Average
18 Jan, 2008 - 06:52 AM
Post #5

New D.I.C Head
*

Joined: 17 Jan, 2008
Posts: 3

i think i can figure it out from there! thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month