Lecture01-Introduction1

# Lecture01-Introduction1 - Introduction/Revision Part I...

This preview shows pages 1–8. Sign up to view the full content.

Computer Programming II 1 Introduction/Revision Part I Lecture 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computer Programming II 2 Learning Objectives Ø To understand how to use one-dimensional array Ø To understand how to use two-dimensional array Ø To understand when to use references Ø To understand how to use functions Ø To understand how to use function overloading Ø To understand how to use default arguments Ø To understand the differences between structures and classes
Computer Programming II 3 Review of Arrays: One -Dimensional Arrays int main() { const int SIZE = 5; int item [SIZE] ; // array of 5 integers cout << "Enter 5 numbers.“ << endl; int sum = 0; for (int i = 0; i < SIZE; i++) { cin >> item[i] ; sum += item[i]; } cout << "The sum of numbers is: " << sum << endl; cout << "The numbers in reverse order:\n"; for (int i = SIZE-1; i >= 0; i--) cout << item[i] << " "; cout << endl; } Output: Enter 5 numbers. 2 3 5 7 9 The sum of numbers is: 26 The numbers in reverse order: 9 7 5 3 2 Q: Program to read 5 numbers , find their sum , and print the numbers in reverse order .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computer Programming II 4 Review of Arrays: Two -Dimensional Arrays int main() { const int NUMROWS = 3; const int NUMCOLS = 4; int val [NUMROWS][NUMCOLS] = {8,16,9,52, 3,15,27,6, 14,25,2,10}; cout << "Multiplied elements"; for (int i = 0; i < NUMROWS; i++) { cout << endl; for (int j = 0; j < NUMCOLS; j++) { val[i][j] *= 10; cout << setw(5) << val[i][j] ; } } cout << endl; } Output: Multiplied elements 80 160 90 520 30 150 270 60 140 250 20 100 Q: Program in which each element is multiplied by 10 and displayed .
Computer Programming II 5 int x = 5; int y = x; // Ampersand sign (&). // Read y is a reference to x . int z = x; // z is NOT a reference to x y++; // Changing y affects x, but not z. Why? cout << "x = " << x << endl << "y = " << y << endl << "z = " << z << endl; Output: x = 6 y = 6 z = 5 References Reference is an alias, another name or nickname of a source variable, hence changing it affects the source variable/object too References must be initialized (null references are not permitted). int x = 5; int y; // Compile error , uninitialized void myFunction(myObj obj) { . .. } References are most commonly found in function parameters , so that the changes made to the parameters/argumentd are retained at the end of function call. .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Computer Programming II 6 double TotalPrice (double ItemPrice, double TaxRate) { // Begin function body return ItemPrice + ItemPrice * TaxRate / 100; } // End function body int main() { double ItemPrice, TaxRate; cout << "Enter the price: "; cin >> ItemPrice; cout << "Enter the tax rate: "; cin >> TaxRate; cout << "The final price is: " << TotalPrice (ItemPrice, TaxRate) ; } Output: Enter the price: 20 Enter the tax rate: 5 The final price is: 21 Review of Functions Function header Function body Return statement Function call Return type Function name Function parameter
Computer Programming II 7 void Swap(int &, int &); // prototype int main() { int x = 6, y = 9;

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

Lecture01-Introduction1 - Introduction/Revision Part I...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online