Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction.

Similar presentations


Presentation on theme: "Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction."— Presentation transcript:

1 Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

2 Declaring Array Variables
datatype arrayRefVar[arraySize]; Example: double myList[10]; C++ requires that the array size used to declare an array must be a constant expression. For example, the following code is illegal: int size = 4; double myList[size]; // Wrong But it would be OK, if size is a constant as follow: const int size = 4; double myList[size]; // Correct From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

3 Arbitrary Initial Values
When an array is created, its elements are assigned with arbitrary values. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

4 Indexed Variables The array elements are accessed through the index. Array indices are 0-based; that is, they start from 0 to arraySize-1. In the example in Figure 7.1, myList holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: arrayName[index]; For example, myList[9] represents the last element in the array myList. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

5 Using Indexed Variables
After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1]; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

6 No Bound Checking C++ does not check array’s boundary. So, accessing array elements using subscripts beyond the boundary (e.g., myList[-1] and myList[11]) does not does cause syntax errors, but the operating system might report a memory access violation. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

7 Array Initializers Declaring, creating, initializing in one step:
dataType arrayName[arraySize] = {value0, value1, ..., valuek}; double myList[4] = {1.9, 2.9, 3.4, 3.5}; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

8 Declaring, creating, initializing Using the Shorthand Notation
double myList[4] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

9 CAUTION double myList[4]; myList = {1.9, 2.9, 3.4, 3.5};
Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double myList[4]; myList = {1.9, 2.9, 3.4, 3.5}; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

10 Implicit Size C++ allows you to omit the array size when declaring and creating an array using an initilizer. For example, the following declaration is fine: double myList[] = {1.9, 2.9, 3.4, 3.5}; C++ automatically figures out how many elements are in the array. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

11 Partial Initialization
C++ allows you to initialize a part of the array. For example, the following statement assigns values 1.9, 2.9 to the first two elements of the array. The other two elements will be set to zero. Note that if an array is declared, but not initialized, all its elements will contain “garbage”, like all other local variables. double myList[4] = {1.9, 2.9}; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

12 Initializing arrays with random values
The following loop initializes the array myList with random values between 0 and 99: for (int i = 0; i < ARRAY_SIZE; i++) { myList[i] = rand() % 100; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

13 Printing arrays To print an array, you have to print each element in the array using a loop like the following: for (int i = 0; i < ARRAY_SIZE; i++) { cout << myList[i] << " "; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

14 Copying Arrays Can you copy array using a syntax like this? list = myList; This is not allowed in C++. You have to copy individual elements from one array to the other as follows: for (int i = 0; i < ARRAY_SIZE; i++) { list[i] = myList[i]; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

15 Summing All Elements Use a variable named total to store the sum. Initially total is 0. Add each element in the array to total using a loop like this: double total = 0; for (int i = 0; i < ARRAY_SIZE; i++) { total += myList[i]; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

16 Finding the Largest Element
Use a variable named max to store the largest element. Initially max is myList[0]. To find the largest element in the array myList, compare each element in myList with max, update max if the element is greater than max. double max = myList[0]; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

17 Finding the smallest index of the largest element
double max = myList[0]; int indexOfMax = 0; for (int i = 1; i < ARRAY_SIZE; i++) { if (myList[i] > max) max = myList[i]; indexOfMax = i; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

18 Random Shuffling srand(time(0)); for (int i = 0; i < ARRAY_SIZE; i++) { // Generate an index randomly int index = rand() % ARRAY_SIZE; double temp = myList[i]; myList[i] = myList[index]; myList[index] = temp; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

19 Shifting Elements double temp = myList[0]; // Retain the first element // Shift elements left for (int i = 1; i < ARRAY_SIZE; i++) { myList[i - 1] = myList[i]; } // Move the first element to fill in the last position myList[ARRAY_SIZE - 1] = temp; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

20 Analyze Numbers Read one hundred numbers, compute their average, and find out how many numbers are above the average. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

21 Passing Arrays to Functions
void printArray(int list[], int arraySize); // Function prototype int main() { int numbers[6] = { 1, 4, 3, 6, 8, 9 }; printArray(numbers, 6); // Invoke the function system("pause"); return 0; } void printArray(int list[], int arraySize) for (int i = 0; i < arraySize; i++) cout << list[i] << " "; Passing Arrays to Functions From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

22 Passing Size along with Array
Normally when you pass an array to a function, you should also pass its size in another argument. So the function knows how many elements are in the array. Otherwise, you will have to hard code this into the function or declare it in a global variable. Neither is flexible or robust. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

23 Pass-by-Value void m(int, int[]); int main() {
int x = 1; // x represents an int value int y[10] = { 0 }; // y represents an array of int values m(x, y); // Invoke m with arguments x and y cout << "x is " << x << endl; cout << "y[0] is " << y[0] << endl; system("pause"); return 0; } void m(int number, int numbers[]) number = 1001; // Assign a new value to number numbers[0] = 5555; // Assign a new value to numbers[0] From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

24 const Parameters Passing arrays by reference makes sense for performance reasons. If an array is passed by value, all its elements must be copied into a new array. For large arrays, it could take some time and additional memory space. However, passing arrays by its reference value could lead to errors if your function changes the array accidentally. To prevent it from happening, you can put the const keyword before the array parameter to tell the compiler that the array cannot be changed. The compiler will report errors if the code in the function attempts to modify the array. Compile error From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

25 Returning an Array from a Function
Can you return an array from a function using a similar syntax? For example, you may attempt to declare a function that returns a new array that is a reversal of an array as follows: // Return the reversal of list int[] reverse(const int list[], int size) This is not allowed in C++. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

26 Modifying Arrays in Functions, cont.
However, you can circumvent this restriction by passing two array arguments in the function, as follows: // newList is the reversal of list void reverse(const int list[], int newList[], int size) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

27 Trace the reverse Function
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2, 6); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } list 1 2 3 4 5 6 newList From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

28 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); i = 0 and j = 5 void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } list 1 2 3 4 5 6 newList From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

29 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); i (= 0) is less than 6 void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } list 1 2 3 4 5 6 newList From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

30 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 0 and j = 5 Assign list[0] to result[5] list 1 2 3 4 5 6 newList 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

31 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 1 and j becomes 4 list 1 2 3 4 5 6 newList 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

32 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); i (=1) is less than 6 void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } list 1 2 3 4 5 6 newList 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

33 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 1 and j = 4 Assign list[1] to result[4] list 1 2 3 4 5 6 newList 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

34 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 2 and j becomes 3 list 1 2 3 4 5 6 newList 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

35 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i (=2) is still less than 6 list 1 2 3 4 5 6 newList 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

36 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 2 and j = 3 Assign list[i] to result[j] list 1 2 3 4 5 6 newList 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

37 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 3 and j becomes 2 list 1 2 3 4 5 6 newList 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

38 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i (=3) is still less than 6 list 1 2 3 4 5 6 newList 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

39 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 3 and j = 2 Assign list[i] to result[j] list 1 2 3 4 5 6 newList 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

40 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 4 and j becomes 1 list 1 2 3 4 5 6 newList 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

41 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i (=4) is still less than 6 list 1 2 3 4 5 6 newList 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

42 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 4 and j = 1 Assign list[i] to result[j] list 1 2 3 4 5 6 newList 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

43 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 5 and j becomes 0 list 1 2 3 4 5 6 newList 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

44 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i (=5) is still less than 6 list 1 2 3 4 5 6 newList 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

45 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i = 5 and j = 0 Assign list[i] to result[j] list 1 2 3 4 5 6 newList 6 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

46 Trace the reverse Function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } After this, i becomes 6 and j becomes -1 list 1 2 3 4 5 6 newList 6 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

47 Trace the reverse function, cont.
int list1[] = {1, 2, 3, 4, 5, 6}; reverse(list1, list2); void reverse(const int list[], int newList[], int size) { for (int i = 0, j = size - 1; i < size; i++, j--) newList[j] = list[i]; } i (=6) < 6 is false. So exit the loop. list 1 2 3 4 5 6 newList 6 5 4 3 2 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

48 Problem: Counting Occurrence of Each Letter
Generate 100 lowercase letters randomly and assign to an array of characters. Count the occurrence of each letter in the array. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

49 Searching Arrays Searching is the process of looking for a specific element in an array; for example, discovering whether a certain score is included in a list of scores. Searching is a common task in computer programming. There are many algorithms and data structures devoted to searching. In this section, two commonly used approaches are discussed, linear search and binary search. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

50 Linear Search The linear search approach compares the key element, key, sequentially with each element in the array list. The Function continues to do so until the key matches an element in the list or the list is exhausted without a match being found. If a match is made, the linear search returns the index of the element in the array that matches the key. If no match is found, the search returns -1. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

51 Linear Search Animation
Key List 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8 3 6 4 1 9 7 3 2 8

52 From Idea to Solution Trace the function
int[] list = {1, 4, 4, 2, 5, -3, 6, 2}; int i = linearSearch(list, 4); // returns 1 int j = linearSearch(list, -4); // returns -1 int k = linearSearch(list, -3); // returns 5 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

53 Initializing Character Arrays
char city[] = {'D', 'a', 'l', 'l', 'a', 's'}; char city[] = "Dallas"; This statement is equivalent to the preceding statement, except that C++ adds the character '\0', called the null terminator, to indicate the end of the string. Recall that a character that begins with the back slash symbol (\) is an escape character. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

54 cout << "string " << str.length() << endl;
char c[] = "gaza"; cout << "c[ ] " << strlen(c) << " : " << c << endl; char c1[5] = "gaza"; cout << "c[5] " << strlen(c1) << " : " << c1 << endl; char c2[6] = "gaza"; cout << "c[6] " << strlen(c2) << " : " << c2 << endl; char c3[] = { 'g','a','z','a' }; cout << "c[ ] " << strlen(c3) << " : " << c3 << endl; char c4[] = { 'g','a','z','a', '\0' }; cout << "c[ ] " << strlen(c4) << " : " << c4 << endl; char c5[5] = { 'g','a','z','a' }; cout << "c[5] " << strlen(c5) << " : " << c5 << endl; char c6[6] = { 'g','a','z','a','\0' }; cout << "c[6] " << strlen(c6) << " : " << c6 << endl; string str = "gaza"; cout << "string " << str.length() << endl; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

55 Reading C-Strings You can read a string from the keyboard using the cin object. For example, see the following code: char city[10]; cout << "Enter a city: "; cin >> city; // read to array city cout << "You entered " << city << endl; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

56 Printing Character Array
For a character array, it can be printed using one print statement. For example, the following code displays Dallas: char city[] = "Dallas"; cout << city; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

57 Reading C-Strings Using getline
C++ provides the cin.getline function in the iostream header file, which reads a string into an array. The syntax of the function is: cin.getline(char array[], int size, char delimitChar) The function stops reading characters when the delimiter character is encountered or when the size - 1 number of characters are read. The last character in the array is reserved for the null terminator ('\0'). If the delimiter is encountered, it is read, but not stored in the array. The third argument delimitChar has a default value ('\n'). From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

58 C-String Functions From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

59 Converting Numbers to Strings
int x = 15; double y = 1.32; long long int z = 10935; string s = "Three numbers: " + to_string(x) + ", " + to_string(y) + ", and " + to_string(z); cout << s << endl; C++11: the to_string function is defined in C++11 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

60 Two-dimensional Arrays
// Declare array ref var elementType arrayName[rowSize][columnSize]; int matrix[5][5]; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

61 Two-dimensional Array Illustration
From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

62 Declaring, Creating, and Initializing Using Shorthand Notations
You can also use an array initializer to declare, create and initialize a two-dimensional array. For example, From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

63 Initializing Arrays with Random Values
The following loop initializes the array with random values between 0 and 99: for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) matrix[row][column] = rand() % 100; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

64 Printing Arrays To print a two-dimensional array, you have to print each element in the array using a loop like the following: for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) cout << matrix[row][column] << " "; } cout << endl; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

65 Summing All Elements To print a two-dimensional array, you have to print each element in the array using a loop like the following: int total =0; for (int row = 0; row < rowSize; row++) { for (int column = 0; column < columnSize; column++) total += matrix[row][column]; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

66 Summing Elements by Column
For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this: for (int column = 0; column < columnSize; column++) { int total = 0; for (int row = 0; row < rowSize; row++) total += matrix[row][column]; cout << "Sum for column " << column << " is " << total << endl; } From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

67 Which row has the largest sum?
Use variables maxRow and indexOfMaxRow to track the largest sum and index of the row. For each row, compute its sum and update maxRow and indexOfMaxRow if the new sum is greater. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

68 Passing Two-Dimensional Arrays to Functions
You can pass a two-dimensional array to a function; however, C++ requires that the column size to be specified in the function declaration. Listing 8.1 gives an example with a function that returns the sum of all the elements in a matrix. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

69 const int COLUMN_SIZE = 4;
int sum(const int a[][COLUMN_SIZE], int rowSize) { int total = 0; for (int row = 0; row < rowSize; row++) for (int column = 0; column < COLUMN_SIZE; column++) total += a[row][column]; } return total; int main() const int ROW_SIZE = 3; int m[ROW_SIZE][COLUMN_SIZE]; cout << "Enter " << ROW_SIZE << " rows and “ << COLUMN_SIZE << " columns: " << endl; for (int i = 0; i < ROW_SIZE; i++) for (int j = 0; j < COLUMN_SIZE; j++) cin >> m[i][j]; cout << "\nSum of all elements is " << sum(m, ROW_SIZE) << endl; system("pause"); return 0; From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

70 Example: Grading Multiple-Choice Test
Objective: write a program that grades multiple-choice test. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

71 Recursion Functions From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

72 Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1);
From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

73 // Return the factorial for a specified index
long long factorial(int); int main() { // Prompt the user to enter an integer cout << "Please enter a non-negative integer: "; int n; cin >> n; // Display factorial cout << "Factorial of " << n << " is " << factorial(n); return 0; } long long factorial(int n) if (n == 0) // Base case return 1; else return n * factorial(n - 1); // Recursive call From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

74 Computing Factorial factorial(4) factorial(0) = 1;
factorial(n) = n*factorial(n-1); factorial(4) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

75 Computing Factorial factorial(4) = 4 * factorial(3) factorial(0) = 1;
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

76 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

77 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

78 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

79 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

80 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

81 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) = 4 * 3 * 2 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

82 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) = 4 * 3 * 2 = 4 * 6 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

83 Computing Factorial factorial(4) = 4 * factorial(3)
factorial(n) = n*factorial(n-1); factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) = 4 * 3 * 2 = 4 * 6 = 24 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

84 Trace Recursive factorial
Executes factorial(4) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

85 Trace Recursive factorial
Executes factorial(3) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

86 Trace Recursive factorial
Executes factorial(2) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

87 Trace Recursive factorial
Executes factorial(1) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

88 Trace Recursive factorial
Executes factorial(0) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

89 Trace Recursive factorial
returns 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

90 Trace Recursive factorial
returns factorial(0) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

91 Trace Recursive factorial
returns factorial(1) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

92 Trace Recursive factorial
returns factorial(2) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

93 Trace Recursive factorial
returns factorial(3) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

94 Trace Recursive factorial
returns factorial(4) From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

95 Pointers From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

96 What is a Pointer? Pointer variables, simply called pointers, are designed to hold memory addresses as their values. Normally, a variable contains a specific value, e.g., an integer, a floating-point value, and a character. However, a pointer contains the memory address of a variable that in turn contains a specific value. From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

97 What is a Pointer? From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

98 Declare a Pointer Like any other variables, pointers must be declared before they can be used. To declare a pointer, use the following syntax: dataType* pVarName; Each variable being declared as a pointer must be preceded by an asterisk (*). For example, the following statement declares a pointer variable named pCount that can point to an int varaible. int* pCount; TestPointer Run From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

99 Dereferencing Referencing a value through a pointer is called indirection. The syntax for referencing a value from a pointer is *pointer For example, you can increase count using count++; // direct reference, increment the value in count by 1 or (*pCount)++; // indirect reference, the value in the memory pointed by pCount is incremented by 1 From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

100 Pointer Type A pointer variable is declared with a type such as int, double, etc. You have to assign the address of the variable of the same type. It is a syntax error if the type of the variable does not match the type of the pointer. For example, the following code is wrong. int area = 1; double* pArea = &area; // Wrong From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014

101 Effect of Assignment = From resourses of Y. Daniel Liang, “Introduction to Programming with C++”, 3rd edn, Pearson, 2014


Download ppt "Introducing Arrays Array is a data structure that represents a collection of the same types of data. From resourses of Y. Daniel Liang, “Introduction."

Similar presentations


Ads by Google