Return to Topic Menu | Computer Science Main Page | MathBits.com  | Terms of Use | Resource CD

Demo Program - Working with Arrays


Task:
 Input 25 grades into an array, print the grades and print the average of the grades.
 

#include <iostream.h>
#include <stdlib.h>
#include <apvector.h>

int main(void)
{
     system ("CLS");

     const int sizeOfArray = 25;
     apvector <int> grades(sizeOfArray);
     int total = 0;

     // Fill the array
     for (int i = 0; i < sizeOfArray; i++)
     {
          cout<<"Please enter grade: ";
          cin>> grades[i];
          total += grades[i];
     }

     // Print the array
     for (i = 0; i < sizeOfArray; i++)
     {
           cout<<grades[i] << endl;;
     }

     // Find the average
     double average = (double) total/sizeOfArray;
    cout<< "\nThe average of the grades is "
           <<average;
     cout<<endl<<endl;

     return 0;
}  

What is the advantage of using a CONSTANT to represent the size of the array?

 

What is the purpose of the counter total?

 

 

What is the purpose of using a cast to (double)?