Level 4 Programming 2 CS 2301 Unit 4


![Q: Design an array - Memory Layout for the following array int tests[5]; allocates Q: Design an array - Memory Layout for the following array int tests[5]; allocates](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-3.jpg)

![. Q: Calculate the size of these arrays? 1. int tests[5] is an array . Q: Calculate the size of these arrays? 1. int tests[5] is an array](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-5.jpg)


![. cout<<N[j]; system(“pause”); return 0; } Copyright © 2012 Pearson Education, Inc. . cout<<N[j]; system(“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-8.jpg)














![. // Get the hours worked by each employee cin>>hours[0]; cin>>hours[1]; cin>>hours[2]; cin>>hours[3]; cin>>hours[4]; . // Get the hours worked by each employee cin>>hours[0]; cin>>hours[1]; cin>>hours[2]; cin>>hours[3]; cin>>hours[4];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-23.jpg)
![. cout<< “ cout<< “ return 0; } “ <<hours[0]; “ <<hours[1]; “ <<hours[2]; . cout<< “ cout<< “ return 0; } “ <<hours[0]; “ <<hours[1]; “ <<hours[2];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-24.jpg)









![for (i = 0; i < SIZE; i++) { if (numbers[i] > highest) highest for (i = 0; i < SIZE; i++) { if (numbers[i] > highest) highest](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-34.jpg)

![for - (i = 0; i < SIZE; i++) { if (numbers[count] < lowest) for - (i = 0; i < SIZE; i++) { if (numbers[count] < lowest)](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-36.jpg)






![Two different format for the combined statement total += numbers[i][j]; Total = total+ numbers[row][col]; Two different format for the combined statement total += numbers[i][j]; Total = total+ numbers[row][col];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-43.jpg)
- Slides: 43

Level 4 Programming (2) CS 2301 Unit 4 Arrays Presented by: Maria Abdalla Bashir Copyright © 2012 Copyright Pearson © Education, 2012 Pearson Inc. Education, Inc.

Q: Define an array? An array is a variable that can store multiple values of the same type Q: Write a format for declaring a one dimensional array? EX: int tests[5]; <Data type> < Array Name > [ Array Size declarator] ; Copyright © 2012 Pearson Education, Inc.
![Q Design an array Memory Layout for the following array int tests5 allocates Q: Design an array - Memory Layout for the following array int tests[5]; allocates](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-3.jpg)
Q: Design an array - Memory Layout for the following array int tests[5]; allocates the following memory: Subscripts 0 first element 1 second element Copyright © 2012 Pearson Education, Inc. 2 third element 3 fourth element 4 fifth element

Array Terminologies Q: What is the difference between the array size declarator and the array size ? The array size declarator shows the number of elements in the array. The size of an array is the total number of bytes allocated for it. It is = (number of elements) * (number of bytes for each element) Copyright © 2012 Pearson Education, Inc.
![Q Calculate the size of these arrays 1 int tests5 is an array . Q: Calculate the size of these arrays? 1. int tests[5] is an array](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-5.jpg)
. Q: Calculate the size of these arrays? 1. int tests[5] is an array of 20 bytes, assuming 4 bytes for an int 2. long double measures[10] is an array of 80 bytes, assuming 8 bytes for a long double Copyright © 2012 Pearson Education, Inc.

1. Using a Loop to Step Through an array 2. Implicit Array Sizing 3. No Bounds Checking in arrays in C++ Copyright © 2012 Pearson Education, Inc.

Program 1 Using loop concept write a c++ Program to enter 5 numbers and print them back in an array form #include<iostream> using namespace std; int main( ) { int N[5]; for(int i = 0; i<5; i++) cin>>N[i]; for(int j = 0; j<5; j++) Copyright © 2012 Pearson Education, Inc.
![coutNj systempause return 0 Copyright 2012 Pearson Education Inc . cout<<N[j]; system(“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-8.jpg)
. cout<<N[j]; system(“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.

Program 2 1. Write a code that defines an array, numbers, and assigns 99 to each element using for loop const int SIZE = 5; int numbers[SIZE]; for (int i= 0; i< SIZE; i++) numbers[i] = 99; Q: Use cin>> instead of assignment cin>> numbers[i]; Copyright © 2012 Pearson Education, Inc.

2. Implicit Array Sizing Q: What do we mean by implicit array sizing? It means that we can determine array size by the size of the initialization list: int quizzes[]={12, 17, 15, 11}; 12 17 Copyright © 2012 Pearson Education, Inc. 15 11

. 3. No Bounds Checking in arrays in C++ Copyright © 2012 Copyright Pearson © Education, 2012 Pearson Inc. Education, Inc.

Q: What do we mean by No Bounds Checking for arrays in C++? 1. When you use a value as an array subscript, C++ does not check it to make sure it is a valid subscript. 2. In other words, you can use subscripts that are beyond the bounds of the array. Copyright © 2012 Pearson Education, Inc.

Q: Write a C++ Code that explains the concept of No Bound Checking, Then show these values are set in a memory The following code defines a three-elements array, and then writes five values to it!` Copyright © 2012 Pearson Education, Inc.

What the Code Does Copyright © 2012 Pearson Education, Inc.

Q: Mention the affect of No Bounds Checking in C++ ? It can : 1. corrupt other memory locations. 2. crash program. 3. lock up computer. Copyright © 2012 Pearson Education, Inc.

Q: When does an Off-By-One Errors occurs in arrays? 1. It happens when you use array subscripts that are off by one. 2. This can happen when you start subscripts at 1 rather than 0. Example: // This code has an off-by-one error. const int SIZE = 100; int numbers[SIZE]; for (int count = 1; count <= SIZE; count++) numbers[count] = 0; Copyright © 2012 Pearson Education, Inc.

. Accessing Array’s Elements Copyright © 2012 Pearson Education, Inc.

Named Constants • Q: Mention the benefits of named constants when used as an array size declarator, give an example? • This eases program maintenance when the size of the array needs to be changed. • Ex: const int SIZE = 5; int tests[SIZE]; Copyright © 2012 Pearson Education, Inc.

Q: Define array’s subscripts? Q: Define an array subscript 1. Each element in an array is assigned a unique subscript. Subscripts start at 0 and end with n-1 2. The last element’s subscript is n-1 where n is the number of elements in the array. subscripts: 0 1 Copyright © 2012 Pearson Education, Inc. 2 3 4

Q: How can we access array’s elements ? Q: How can we access array’s elements? By using : 1. A constant or literal subscript: cout << tests[3] << endl; EX : 2. Integer expression as subscript: EX int i = 5; cout << tests[i] << endl; Copyright © 2012 Pearson Education, Inc.

Q : Write a general Format for accessing array’s Elements cout<< or cin >> Array Name [Array Subscript]; OR Array Name [integer Number]; Copyright © 2012 Pearson Education, Inc.

Q: Write a c++ program that asks for the number of hours worked by 6 employee and stores the values in an array[Only to show the concept of the loop] #include<iostream> using namespace std; int main( ) { const int num_emp = 6; int hours[num_emp]; cout<<“enter the hours worked by 6 employees”; Copyright © 2012 Pearson Education, Inc.
![Get the hours worked by each employee cinhours0 cinhours1 cinhours2 cinhours3 cinhours4 . // Get the hours worked by each employee cin>>hours[0]; cin>>hours[1]; cin>>hours[2]; cin>>hours[3]; cin>>hours[4];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-23.jpg)
. // Get the hours worked by each employee cin>>hours[0]; cin>>hours[1]; cin>>hours[2]; cin>>hours[3]; cin>>hours[4]; cin>>hours[5]; //Display the values in the array Cout<<“the values you entered are: ” Copyright © 2012 Pearson Education, Inc.
![cout cout return 0 hours0 hours1 hours2 . cout<< “ cout<< “ return 0; } “ <<hours[0]; “ <<hours[1]; “ <<hours[2];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-24.jpg)
. cout<< “ cout<< “ return 0; } “ <<hours[0]; “ <<hours[1]; “ <<hours[2]; “ <<hours[3]; “ <<hours[4]; “ <<hours[5]<<endl; Copyright © 2012 Pearson Education, Inc.

Program output with example of input Here are the contents of the hours array, with the values entered by the user in the example output: Copyright © 2012 Pearson Education, Inc.

. 1. Array Initialization 2. Default Array Initialization 3. Partial Array Initialization Copyright © 2012 Pearson Education, Inc.

1. Array Initialization Q: How can array being initialized? Arrays can be initialized with an initialization list within the code. The initialization list cannot exceed the array size. Example: const int SIZE = 5; int tests[SIZE] = {79, 82, 91, 77, 84}; 7 -27 Copyright © 2012 Pearson Education, Inc.

2. Default Initialization Q: How can global and local arrays being initialized by default? 1. Global array all elements initialized to zero by default 2. Local array all elements uninitialized by default Copyright © 2012 Pearson Education, Inc.

3. Partial Array Initialization Q: What do we mean by partial array initialization? If an array is initialized with fewer initial values than the size declarator, the remaining elements will be set to 0: Q: Draw a memory layout for this array int numbers[7] = { 1, 2, 4, 8}; Copyright © 2012 Pearson Education, Inc.

Program 3 Q: Write a C++ program that shows each month and the number of days in that month using initialization list #include<iostream> using namespace std; int main( ) { const int MONTH=12; int days[MONTH]={31, 28, 31, 30, 31, 30. 31}; Copyright © 2012 Pearson Education, Inc.

. for( int i =0; i<MONTH; i ++) { cout<< “month “ <<(count +1)<<“has”; cout<< days[i] << “ days. n”; } system (“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.

. Copyright © 2012 Pearson Education, Inc.

Program 4 Q: Write a c++ program that finds the Highest Value in an Array #include<iostream> using namespace std; int main( ) { const int size= 4; int numbers[size] ={70, 80, 67, 34}; int count; int highest; highest = numbers[0]; Copyright © 2012 Pearson Education, Inc.
![for i 0 i SIZE i if numbersi highest highest for (i = 0; i < SIZE; i++) { if (numbers[i] > highest) highest](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-34.jpg)
for (i = 0; i < SIZE; i++) { if (numbers[i] > highest) highest = numbers[i]; } cout<< highest<<endl; system (“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.

Q: no Write a c++ program that finds the Lowest Value in an Array #include<iostream> Using namespace std; int main( ) { const int size=4; Int numbers[size]={10, 45, 56, 90}; int count; int lowest; lowest = numbers[0]; Copyright © 2012 Pearson Education, Inc.
![for i 0 i SIZE i if numberscount lowest for - (i = 0; i < SIZE; i++) { if (numbers[count] < lowest)](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-36.jpg)
for - (i = 0; i < SIZE; i++) { if (numbers[count] < lowest) lowest = numbers[count]; } cout<< lowest; system (“pause”); return 0; } Copyright © 2012 Pearson Education, Inc.

Program 6 Summing All the Elements in a Two-Dimensional Array 4 Q: Given the following definitions of an array: Find the summing of all its elements using c++ program Copyright © 2012 Pearson Education, Inc.

const int R = 5; // Number of rows const int C = 5; // Number of columns int total = 0; // Accumulator int numbers[R][C] = {{2, 7, 9, 6, 4}, {6, 1, 8, 9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, 2, 7, 4, 1}}; Copyright © 2012 Pearson Education, Inc.

. #include<iostream> using namespace std; int main() { const int R = 5; // Number of rows const int C = 5; // Number of columns int total = 0; // Accumulator int numbers[R][C] = {{2, 7, 9, 6, 4}, {6, 1, 8, 9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, Copyright 2, 7, © 4, 1}}; Education, Inc. 2012 Pearson

const int R= 5; // Number of rows const int C = 5; // Number of columns int total = 0; // Accumulator int numbers[R][C] = {{2, 7, 9, 6, 4}, {6, 1, 8, 9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, 2, 7, 4, 1}}; Copyright © 2012 Pearson Education, Inc.

#include<iostream> using namespace std; { const int R = 5; // Number of rows const int C = 5; // Number of columns int total = 0; // Accumulator int numbers[R][C] = {{2, 7, 9, 6, 4}, {6, 1, 8, 9, 4}, {4, 3, 7, 2, 9}, {9, 9, 0, 3, 1}, {6, 2, 7, 4, 1}}; Copyright © 2012 Pearson Education, Inc.

// Sum the array elements. for (int i= 0; i < R; i++) { for (int j = 0; j < C; j++) total += numbers[i][j]; } // Display the sum. cout<< "The total is " <<total<< endl; System(“pause”); Return 0; } Copyright © 2012 Pearson Education, Inc.
![Two different format for the combined statement total numbersij Total total numbersrowcol Two different format for the combined statement total += numbers[i][j]; Total = total+ numbers[row][col];](https://slidetodoc.com/presentation_image_h2/7ee14bb6e87efd1cb904a31e66ce019c/image-43.jpg)
Two different format for the combined statement total += numbers[i][j]; Total = total+ numbers[row][col]; Copyright © 2012 Pearson Education, Inc.