Math2220 Lecture 11

Default Argument

#include <iostream>
using namespace std;

void showVolume(int length, int width=1, int height=1);

void main() {
    showVolume(4,6,2);
    showVolume(4,6); // same as showVolume(4,6,1)
    showVolume(4); // same as showVolume(4,6,1)

    char line[100];
    cin.getline(line,100);
}

void showVolume(int length, int width, int height) {
    cout << "Volume of the box with \n"
         << "lenght = " << length
         << ", width = " << width << endl
         << "and height = " << height
         << " is " << length*width*height
         << endl << endl;
}
Output
Volume of the box with
lenght = 4, width = 6
and height = 2 is 48

Volume of the box with
lenght = 4, width = 6
and height = 1 is 24

Volume of the box with
lenght = 4, width = 1
and height = 1 is 4

Syntax

function declaration Function header
double tax(double income, bool married = false, int dependent = 0);
incorrect
double tax(double income, bool married = false, int dependent = 0) {
// function body
}
The correct version should be
double tax(double income, bool married, int dependent) {
// function body
}
Function body Function call We can rewrite the first example as follows
#include <iostream>
using namespace std;
void showVolume(int length, int width, int height); void showVolume(int length, int width); void showVolume(int length); void main() { showVolume(4,6,2); showVolume(4,6); showVolume(4); char line[100]; cin.getline(line,100); } void showVolume(int length, int width, int height) { cout << "Volume of the box with \n" << "lenght = " << length << ", width = " << width << endl << "and height = " << height << " is " << length*width*height << endl << endl; } void showVolume(int length, int width) { showVolume(length, width, 1); } void showVolume(int length) { showVolume(length, 1, 1); }

Summary

Now you should know:

Array

An array is a sequence of numbers.
#include <iostream>
using namespace std;

// the program asks user to input 5 numbers
// then output the average
void main() {
    // declare an array with 5 numbers
    // sum is the total sum of all the scores
    double score[5], sum = 0;
    cout << "Please input 5 numbers. "
         << "Seperate the numbers by spaces: \n";
   
    int i;
    for(i = 0; i < 5; i++) {
        cin >> score[i]; //ask the user to input the i-th score
        sum+=score[i];
    }

    // magic formula
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "The scores are : \n";
   
    for(i = 0; i < 5; i++) {
        cout << score[i] << " ";
    }

    cout << endl;
    cout << "The average is " << sum/5 <<endl;

    char line[100];
    cin.getline(line,100);
    cin.getline(line,100);
}
Output
Please input 5 numbers. Seperate the numbers by spaces:
92.25 33 45.75 88.8 77.77
The scores are :
92.25 33.00 45.75 88.80 77.77
The average is 67.51

score[0] score[1] score[2] score[3] score[4]
92.25 33 45.75 88.8 77.77
Array consists of 2 parts Syntax
Declare an array
Type arrayName[size];

size should be a number or a const variable. It shouldn't be other type of variables.
Example: Dereference