Week2 Lecture Summary for CSC 309

These notes are not intended to be complete. They serve as an outline to the class and as a supplement to the text.

Reference Variables

Example 1
int count = 67;
int& refCount = count;  // creating and initializing a reference variable
Example 2
int count = 67;
int refCount = count;

Note the difference(huge) between example 1 and example 2.

In Example 1, the memory location (box) that contains the value 67 has two names, count and refCount. We can use refCount to read or modify the value stored in the box at that location.

In Example 2, their are two different memory locations(2 boxes) with the value 67.

Example 3
int x;
int& y = x;

x = 100;
cout << y << endl;  // output 100

++y;
cout << x << endl; // output 101


Functions

Example
#include ....

using namespace std;

// function declaration or prototype
int pow(int base, int exponent);

int main(){
    pow(2,7);
      :
}

// function definition
int pow(int base, int exponent){
     :
     :
    return ..
}

Variable Scope

Example

void getIncome(){
    double income;
    cin >> income;
    return;
}
double computeTax(){
    return  income * .30;
}

Example

double income;

double computeTax(){
    return  income * .30;
}

void getIncome(){    
    cin >> income;
    return;
}

Parameter Passing

Function Overloading

Example1

The max function is overloaded, therefore this code will compile.

int max(int first, int second);

int max(double first, double second);

int max(double first, double second, double third);
Example2

The max function is NOT overloaded, therefore this code will NOT compile.

int max(int first, int second);

int max(int second, int first);

int max(int f, int s);

Default Parameters

See linebreak.cpp


Monthly Payment

Write a program to compute the monthly payment on a loan using the formula.

In the formula P is the amount borrowed, i is the monthly rate and n is the number of payments.

The amount borrowed, the rate and number of years will be gotten form the user.

We will break the program up using the following functions.

void getInput(double& principal, double& rate, int& years);

double computePayment(double principal, double rate, int years);

void showMonthlyPayment(double principal,double rate, int years);

Statistics Program

Uses functions to compute the mean and median of values stored in a vector. Here are the function prototypes.

double median(vector<double> temperatures);

double mean(const vector<double>& temperatures);

Iterators

Review Previous Class and Chapter 2