All pastes #2108320 Raw Edit

Someone

public text v1 · immutable
#2108320 ·published 2012-01-31 22:25 UTC
rendered paste body
/********************************************************************
 *
 * Lab 3 : ScoreTable.cpp
 *
 * Author: Benjamin McComb
 *         bam125@zips.uakron.edu
 *
 * Purpose: Demonstration of C++ library headers
 *
 * This program will display (on the console) a table
 * containing 4 usernames along with a score for each.
 *
 ********************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main() {

    // four scores for four students
    int score1 = 23;
    int score2 = 54;
    int score3 = 7;
    int score4 = 211;

    /*
    // user inputs all four scores
    cout << "Please type four (integer) scores\n>";
    cin >> score1 >> score2 >> score3 >> score4;
    cin.ignore(20000, '\n');
    */

    // calculate the average score
    double average = static_cast<double>(score1 + score2 + score3 + score4) / 4;

    // output a table of scores
    cout << left << "PERSON:"  << right << "SCORE" << endl;
    cout << left << "Larry:"    << right << score1 << endl;
    cout << left << "Curly:"    << right << score2 << endl;
    cout << left << "Moe:"      << right << score3 << endl;
    cout << left << "Mega Man:" << right << score4 << endl;
    cout << left << "AVERAGE:" << right << average << endl;

    return 0;
}





Part 6: Demonstrating math functions using abs() ¶

It turns out your boss wants a third column in this table, containing the absolute numeric difference between each person's score and the global average (regardless of whether it's greater or smaller). Bosses want strange things sometimes. But we can do this!

The abs function accepts a numeric value, and returns the absolute value. When used from the cmath library, the return value of abs() will be of type double, even if an integer is entered into it. The parameter passed into the function can be in the form of an entire expression. The expression will be evaluated first, and the result will be used as the input value. Here is example syntax using variables a, b, and c:

abs( a * (b - c) )

Procedure: ¶

    * In your source file, at the end of each table row, add terms to your cout statements to add a new (properly aligned) column to the output table, containing the absolute values of the difference between each person's score and the overall average. 

    Save/compile/run. Make sure the columns line up, then commit with the message:

        Add column for absolute value

See the textbook for a partial list of more math functions.