Average Function


Program Listing

AverageFunc.cpp

/************************************************************************
*                                                                       *
*   Program:    AverageFunc                                             *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototype

float Average(int a, int b);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
************************************************************************/

int main() {

//  Get two integers

    cout << "Enter two test scores:  " << flush;
    int x;
    int y;
    cin >> x >> y;

//  Find their average

    float AvgScore = Average(x, y);

//  Print the average

    cout << "The average is " << AvgScore << endl;
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   Average                                                 *
*                                                                       *
************************************************************************/

float Average(int a, int b) {
    float Avg = (a + b)/2.0;
    return Avg;
}

Sample Run

Enter two test scores:  85 90
The average is 87.5



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:36 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/AverageFunc.html

Return to Coms 261 home page