#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;
}
|
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 |
double tax(double income, bool married = false, int
dependent = 0, bool resident = true);.double tax(double income,
bool& married = false);double calculateGPA(char grade = 'A', int unit =
4);double calculateInterest(double balance, double
interestRate = 0.05);void showVolume(int length = 1, int width, int
height);double double tax(double income, bool married = false,
int dependent, bool resident = true)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
double tax(double income, bool married = false, int
dependent = 0, bool resident = true);| Function call | Meaning |
tax(100000,true,2,false) |
tax(50000,true,2,false) |
tax(200000,true,3) |
tax(200000,true,3,true) |
tax(75555,false,2) |
tax(75555,false,2,true) |
tax(22333.75,true) |
tax(22333.75,true,0,true) |
tax(35678.25) |
tax(35678.25,false,0,true) |
resident, you must provide the
value for married and dependent.
#include <iostream> |
#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);
}
|
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 |
| Declare an array Type arrayName[size]; size should be a number or a const variable. It shouldn't be other type of variables. |
int year[50];int is called the base type of the array,
50 is the size. year is the name.double taxRate, income[99];The order doesn't matter.
#include <iostream>
using namespace std;
// the program asks user to input 5 numbers
// then output the average
void main() {
const int NUM_OF_STUDENTS = 5;
// declare an array with NUM_OF_STUDENTS numbers
// sum is the total sum of all the scores
double score[NUM_OF_STUDENTS], sum = 0;
cout << "Please input "<< NUM_OF_STUDENTS << " numbers. "
<< "Seperate the numbers by spaces: \n";
int i;
for(i = 0; i < NUM_OF_STUDENTS; 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 < NUM_OF_STUDENTS; i++) {
cout << score[i] << " ";
}
cout << endl;
cout << "The average is " << sum/NUM_OF_STUDENTS <<endl;
char line[100];
cin.getline(line,100);
cin.getline(line,100);
}
|
#include <iostream>
using namespace std;
void main() {
int numOfStudents;
cout << "Please enter the number of students: ";
cin >> numOfStudents;
double score[numOfStudents], sum = 0; // Incorrect!
char line[100];
cin.getline(line,100);
}
|
score[0],score[1],score[2],score[3],score[4]
represent the first, second, third, forth and fifth element in
the array scorescore[i] as variables. i.e. you can
use it in assignments, arithmetic expression etc etcscore[i], or income[n+1].