Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2012 Pearson Education, Inc. 16/4/1435 h Sunday Lecture 3 1.Using a Loop to Step Through an array 2.Implicit Array Sizing 3.No Bounds Checking.

Similar presentations


Presentation on theme: "Copyright © 2012 Pearson Education, Inc. 16/4/1435 h Sunday Lecture 3 1.Using a Loop to Step Through an array 2.Implicit Array Sizing 3.No Bounds Checking."— Presentation transcript:

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

2 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 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++)

3 Copyright © 2012 Pearson Education, Inc.. cout<<N[i]; system(“pause”); return 0; }

4 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;

5 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}; 12171511

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

7 Copyright © 2012 Pearson 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.

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

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

10 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.

11 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;


Download ppt "Copyright © 2012 Pearson Education, Inc. 16/4/1435 h Sunday Lecture 3 1.Using a Loop to Step Through an array 2.Implicit Array Sizing 3.No Bounds Checking."

Similar presentations


Ads by Google