Week 08
Week 08
COMPUTER PROGRAMMING
Muzammil Ahmad Khan
[email protected]
2
Array index out of bounds
• An array is a series of elements, each accessed by an index of
that array.
If I say
• int arr[5] = { 20, 30, 40, 50, 60 };
• arr has 5 elements in it, accessed through arr[0] to arr[4].
• The first element, 20, is access through arr[0].
• The second element, 30, is accessed through arr[1].
• The third element, 40, is accessed through arr[2].
• The fourth element, 50, is accessed through arr[3].
• The last (5th) element, 60, is accessed through arr[4].
3
Array index out of bounds
• If an index is "out of bounds", it means that it would be attempting to access an
array element that doesn't exist.
For example,
int arr[5] = { 20, 30, 40, 50, 60 }; // declare an array of length 5.
arr[5] = 70; // 5 here is out of bounds, because valid array indices only go from 0 to 4
arr[-1] = 42; // -1 here is out of bounds, because arrays start at index 0.
6
Arrays in memory
7
Arrays in memory
8
Range-based for loop statement
9
Range-based for loop statement
18
Program initializing an array with a declaration
output
#include <iostream> while ( grades[idx] > MAX_GRADE )
using namespace std; {
#define MAX_GRADE 100 cout<< "\nThe highest grade possible is “
#define STUDENTS 10 << MAX_GRADE<<endl ;
int grades[STUDENTS], idx; cout<< "\nEnter correct grade: " ;
cin>>grades[idx] ;
int total = 0; //used for average
}
main()
total += grades[idx];
{
}
for( idx=0;idx< STUDENTS;idx++)
cout<< "\n\nThe average score is "<<(total /
{ STUDENTS);
cout<<"Enter Student "<<idx return 0;
+1<<"\'s grade: "; }
cin>>grades[idx] ; 19
Program initializing an array with a declaration
Output Enter Student 1's grade: 122
Enter Student 1's grade: 77 The highest grade possible is 100
Enter Student 2's grade: 88
Enter Student 3's grade: 90
Enter correct grade:
Enter Student 4's grade: 66
Enter Student 5's grade: 43
Enter Student 6's grade: 65
Enter Student 7's grade: 87
Enter Student 8's grade: 23
Enter Student 9's grade: 65
Enter Student 10's grade: 76
The average score is 68 20
Multidimensional arrays
• A multidimensional array has more than one subscript.
• A two-dimensional array has 2 subscripts; a three-dimensional array has 3
subscripts, and so on.
E.g., int table[5][3];
// 5 rows and 3 cols and store integer
table[0][0] table[0][1] table[0][2]
table[1][0] table[1][1] table[1][2]
table[2][0] table[2][1] table[2][2]
table[3][0] table[3][1] table[3][2]
table[4][0] table[4][1] table[4][2]
21
Multidimensional arrays
E.g.,
int n[5][3] = { { 20,43,45 }, { 65,4,34 },
{ 23,56,67 }, { 45,56,78 }, { 66,76,90 } };
22
Multidimensional arrays
E.g.,
int n[5][3] = { { 20,43,45 }, { 65,4,34 },
{ 23,56,67 }, { 45,56,78 }, { 66,76,90 } };
23
Multidimensional arrays
24
Multidimensional arrays
#include <iostream> for ( int j = 0; j < 2; j++ )
using namespace std; {
int main () { cout << "a[" << i << "][" << j << "]:
// an array with 5 rows and 2 columns. ";
int a[5][2] = { {0,0}, {1,2}, {2,4}, cout << a[i][j]<< endl;
{3,6},{4,8}}; }
25
Multidimensional arrays
When the above code is compiled and executed, it produces the following
result −
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
26
Multidimensional arrays
#include<iostream> cout<<"Printing a 2D Array:\n";
using namespace std; for(i=0;i<4;i++)
main( ) {
{ for(j=0;j<2;j++)
int arr[4][2] = { {
{ 10, 11 }, cout<<"\t"<<arr[i][j];
{ 20, 21 }, }
{ 30, 31 }, cout<<endl;
{ 40, 41 } }
}; }
int i,j;
27
Multidimensional arrays
28
Multi Dimensional Arrays
▪ The array int arr1 [2] [4] can store total (2*4) = 8 elements.
▪ In C++ int data type takes 4 bytes and we have 8 elements in the
array ‘arr1’ of the int type.
30
Two Dimensional Arrays
31
Two Dimensional Arrays
Syntax of 2D array:
data_Type array_name[n][m];
Where,
n: Number of rows.
m: Number of columns.
32
Two Dimensional Arrays
// c++ program to illustrate the two dimensional array // Printing the element of 2D array
#include <iostream> for (int i = 0; i < 3; i++) {
using namespace std; for (int j = 0; j < 4; j++) {
int main() cout << array1[i][j] << " ";
{ }
int count = 1; cout << endl;
int array1[3][4]; }
// Initialize 2D array using loop
for (int i = 0; i < 3; i++) { return 0;
for (int j = 0; j < 4; j++) { }
array1[i][j] = count;
count++;
}
} 33
Two Dimensional Arrays
// C Program to print the elements of a Two-Dimensional array
#include <stdio.h>
int main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// output each array element's value
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("Element at x[%i][%i]: ", i, j);
printf("%d\n", x[i][j]);
}
}
return (0);
} 34