PIC10A Lecture 11

Default Argument

#include <iostream>
using namespace std;

void showVolume(int length, int width=1, int height=1); int main() { showVolume(4,6,2);
showVolume(4,6); // same as showVolume(4,6,1)
showVolume(4); // same as showVolume(4,6,1)

return 0;
}

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

Volume of a box with
Lenght = 4, width = 6
and Height = 1 is 24

Volume of a box with
Lenght = 4, width = 1
and Height = 1 is 4

Syntax

function declaration Function header
  • exactly same as the function headers of ordinary functions
  • you should not assign the default value in the function header because the assignments are already done in the function declaration. e.g.
    Suppose the function declaration is
    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); int main() { showVolume(4,6,2); showVolume(4,6); showVolume(4); return 0; } void showVolume(int length, int width, int height) { cout << "Volume of a 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
    int 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;

    return 0;

    }
    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