Programming Fundamentals
(CS-102)
Instructor: Dr. Junaid Rashid
Department of Computer Science
Air University Islamabad
A&AC-Kamra
Contents
•Recap
• Array Basics
• Array Definition
• Initialization of Array elements
• Referring To Individual Elements Of Array
• Bound Checking
Recap
Arrays in C++
Declare Variables -What We Have So for
• Declare 10 integers and print on screen.
• Read 100 numbers from user and find their sum.
void main {
int var1, var2, var3, var4, var5, var6, var7, var8, var9,var10;
• Issue with this type of declaration and usage ?
• Solution?
Arrays- Introduction
• In computer languages we also need to group together data items of
the same type. The most basic mechanism that accomplishes this in
C++ is the array
• We have used variables to store values (single value at a time) in
memory for later reuse. We now explore a means to store multiple
values together as one unit, the array (one name of many values).
• Arrays exist in almost every computer language. Arrays in C++ are
similar to those in other languages, and identical to those in C.
• In Standard C++ the array is not the only way to group elements of
the same type. (vector is another way)
Arrays- Introduction
• Arrays are fixed-size collections consist of the data items of the same
type.
• An array stored sequentially in memory (contiguous memory location)
• Therefore, an integer array holds some number of integers, a character
array holds some number of characters.
• Array has same properties as for variable in C++ (type, name, size)
Datatype ArryName [Int_expression] Datatype varName
Array declaration Variable declaration
Array- Example
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main (){ int main (){
const int SIZE=5; const int SIZE=5;
int arrayVar1[SIZE]; int arrayVar1[5];
arrayVar1[0]=10; for(int i=0, j=10; i<SIZE;i++, j=j+10){
arrayVar1[1]=20; arrayVar1[i]=j;
arrayVar1[2]=30; }
arrayVar1[3]=40;
arrayVar1[4]=50; for(int i=0; i<SIZE;i++){
cout<<"\n array element "<<i+1<<" ["<<i<<"] :
cout<<"\n array 1st element [0] : "<<arrayVar1[0]; "<<arrayVar1[i];
cout<<"\n array 2nd element [1] : "<<arrayVar1[1]; }
cout<<"\n array 3rd element [2] : "<<arrayVar1[2]; return 0;
cout<<"\n array 4th element [3] : "<<arrayVar1[3]; }
cout<<"\n array 5th element [4] : "<<arrayVar1[4];
return 0;
}
Array – Example Array subscripting operator
int arrayVar1[5];
Type
int, float, double, char
Size/dimension
Name Must be unsigned integer value
Same rules as for variables
Arrays – Memory View
arrayVar1
Name
memory
arrayVar1[0] 10 First element of Array
arrayVar1[1] 20
arrayVar1[2] 30
arrayVar1[3] 40
arrayVar1[4] 50
Index/subscript
Start from zero to size-1
Declaration of Arrays
Datatype ArryName [size]
• Array must be declared before its first use (as for variables)
int arrayVar1 [ 50 ] ; char myChar[20]
• More than one array can be declared on a line
int student[10] , Faculty[30] , Player[20] ;
• Mix declaration of variables with declaration of arrays
int var1 , var2 , marks [8] ;
float fArray[size];
Initialization of Arrays Elements
• Can be initialized in declaration
double A[3]= {3.45, 6.80,5.30}
• If not enough initializers, rightmost elements become 0
int arr1[ 5 ] = { 0 } ; int arr2[ 5] = { } ;
• If too many a syntax error is produced
char arr5[3]={'a', 'b', 'c', 'd'};
• If size omitted, initializers determine it the size of array
int n[ ] = { 1, 2, 3, 4, 5 };
Referring To Individual Elements Of Array
• The elements of an array can be accessed by using an index/subscript
• Arrays in C++ are zero-indexed, so the first element has an index of 0
int a=4, b=8
arrayVar1[ 0 ] = 10; arrayVar1[a] = 20;
cout<<arrayVar1[0]; arrayVar1[b]= arrayVar1[a]
int a=2, b=1 cout<<arrayVar1[a];
arrayVar1[a] = 15; cout<<arrayVar1[b];
arrayVar1[a+b]=22
cout<<arrayVar1[0]; if (arrayVar1[ 0 ] == 10);
cout<<arrayVar1[0]+10; cout<<“Array can be used here too“;
int i=65;
while (i<=90){ Array[i]=i++; }
Bound Checking
• C++ provides no automatic array bound checking to prevent an
element that does not exist
• It’s important to ensure that every subscript you use to access an
array element is within the array’s bounds (0 to size-1)
• Referring to an element outside the array bounds is an
execution-time logic error. It isn’t a syntax error
• Out-of-bounds problem can create buffer overflow, (Security flaw)
Bound Checking
• If we have the statements:
int numArray[5];
int i;
• The component numArray[i] is valid if i = 0, 1, 2, 3, 4
• The index of an array is in bounds if the index >=0 and the index <=
ARRAY_SIZE-1
• Otherwise, we say the index is out of bounds
• In C++, there is no guard against indices that are out of bounds
Advantage of array
• It is used to represent multiple data items of same type by using only
single name.
• It can be used to implement other data structures like linked lists,
stacks, queues, trees, graphs etc.
• 2D arrays are used to represent matrices.
16
Arrays Exercise - code
int Marks[20];
for (int counter = 0; counter < 20; counter++ ){
cin>>Marks[counter];
}
int sum = 0;
for (int counter = 0; counter < 20; counter++ ){
sum += Marks[counter];
}
cout << “Average is “ << sum/20;
Homework
Problem#1
Write a program that take 10 characters from user and print it in reverse order of the input.
Problem#2:
Write a program that take 10 integer from user and find the maximum and minimum
using arrays;.
Also find the index of maximum element.
Problem#1
Write a program take 20 characters from user, 10 each in two character arrays.
And the find similar character in both arrays.