Professional Documents
Culture Documents
Introduction To Programming: Engr. Rashid Farid Chishti
Introduction To Programming: Engr. Rashid Farid Chishti
Programming
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
Chapter 07: Arrays and Strings
International Islamic University H-10, Islamabad, Pakistan
http://www.iiu.edu.pk
Arrays
In everyday life we commonly group similar
objects into units. We buy eggs by the carton.
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.
Arrays can hold a few data items or tens of
thousands. The data items grouped in an array
can be simple types such as int or float, or
they can be user-defined types such as
structures and objects.
An array groups items of the same type. The
items in a in an array are accessed by an
index number. Using an index number to specify
an item allows easy access to a large number
of items.
Defining, Reading and Writing Array
// gets four ages from user, displays them
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int age[4], j; //array 'age' of 4 ints
for(j=0; j<4; j++){ //get 4 ages
cout << "Enter an age: ";
cin >> age[j]; //access array element
}
for(j=0; j<4; j++){ //display 4 ages
cout << "age[" << j << "] = " << age[j] << endl;
cout <<"Address " << &age[j] << " = " << age[j] <<
endl;
}
system("PAUSE"); return 0;
}
Calculating Average
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
double avg, sum = 0 ;
int i ; int marks[10] ; /* array declaration */
for ( i = 0 ; i <= 9 ; i++ ){
cout << "\nEnter marks ";
cin >> marks[i]; /* store data in array */
}
for ( i = 0 ; i <= 9 ; i++ )
sum = sum + marks[i] ; /* read data from array*/
avg = sum / 10 ;
cout << "\n Average marks = " << avg <<endl;
system("PAUSE"); return 0;
}
Using Direct Access on an Array
// Using Direct Access on Array
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
int mem[]={50,60,70} ; // Initialize the array
cout << "mem[0] = " << mem[0] << endl;
cout << "mem[1] = " << mem[1] << endl;
cout << "mem[2] = " << mem[2] << endl;
mem[0] = 80; mem[1] = 90; mem[2] = 100;
cout << endl;
cout << "mem[0] = " << mem[0] << endl;
cout << "mem[1] = " << mem[1] << endl;
cout << "mem[2] = " << mem[2] << endl;
system("PAUSE"); return 0;
}
Printing in Reverse Order
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
const int SIZE=5; // defines the size N for 5 elements
double a[SIZE]; // declares the array’s elements as
type double
cout << "Enter " << SIZE << " numbers:\t";
for (int i=0; i<SIZE; i++)
cin >> a[i];
cout << "In reverse order: ";
for (int i=SIZE-1; i>=0; i--)
cout << " " << a[i];
system("PAUSE"); return 0;
}
Out of Bounds
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
float a[] = { 22.2,44.4, 66.6 };
float x=11.1;
cout << "I m going to Crash " << endl;
cin >> x;
a[3333] = 88.8; // ERROR: index is out of bounds!
system("PAUSE"); return 0;
}
Passing Array to Function
#include <iostream>
#include <stdlib.h>
using namespace std;
int sum(int[],int); // declaration
int main(){
int a[] = { 11,33, 55,77 };
int size = sizeof(a)/sizeof(int);
cout << "sum(a,size) = " << sum(a,size) << endl;
cout << endl << a[0] << endl;
system("PAUSE"); return 0;
}
int sum(int a[],int n){
int sum=0;
for (int i=0; i<n; i++)
sum += a[i];
a[0] = 100; return sum;
}
n Dimensional Arrays
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=2, col=3; int i,j;
int ary[row][col] = {
{11,12,13},
{21,22,23}
};
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){ cout << ary[i][j] << " ";}
cout << endl;
}
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << &ary[i][j] << "="<<ary[i][j]<<"\t";}
cout << endl;}
system("PAUSE"); return 0;
}
n Dimensional Arrays
ary[0][0]= 0x22ff40 = 11
ary[0][1]= 0x22ff44 = 12
11 12 13
21 22 23
ary[0][2]= 0x22ff48 = 13
ary[1][0]= 0x22ff4C = 21
ary[1][1]= 0x22ff50 = 22
ary[1][2]= 0x22ff54 = 23
2-Dimensional Arrays
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=3, col=3; int i,j;
int ary[row][col] = {
{11,12,13},
{21,22,23},
{31,32,33} };
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){ cout << ary[i][j] << " ";}
cout << endl;
}
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << &ary[i][j] << "="<<ary[i][j]<<"\t";}
cout << endl;}
system("PAUSE"); return 0; }
3-Dimensional Arrays ary[0][0]= 0x22ff30 = 11
ary[0][1]= 0x22ff34 = 12
ary[0][2]= 0x22ff38 = 13
ary[1][0]= 0x22ff3C = 21
ary[2][0]= 0x22ff48 = 31
ary[2][2]= 0x22ff50 = 33
Calculating Square of a Matrix
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ const int row=3, col=3; int i,j;
int A[row][col];
cout << "Square of a " <<row <<"x"<<col<<"Matrices"<<endl;
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++){
cout << "A[" << i+1 << "][" << j+1 << "]= ";
cin >> A[i][j];
}
}
for(i=0 ; i< row ; i++)
for(j=0 ; j<col; j++)
A[i][j] = A[i][j]*A[i][j];
for(i=0 ; i< row ; i++){
for(j=0 ; j<col; j++)
cout << A[i][j] << "\t";
cout << endl;
}
system("PAUSE"); return 0;
}
Passing 2D Array to Function
#include <iostream>
#include <stdlib.h>
using namespace std;
void get_data(float a[][3],int row, int col){
int i,j;
for (i=0; i<row; i++)
for (j=0; j<col; j++){
cout << "A["<<i+1<<"]["<<j+1<<"]:";
cin >> a[i][j];}
}
void show_data(float a[][3],int row, int col){
int i,j;
for (i=0; i<row; i++){
for (j=0; j<col; j++)
{cout << a[i][j] << "\t";}
cout << endl; }
}
int main(){
float matrix[4][3]; // 4 rows and 3 columns
get_data(matrix,4,3);
show_data(matrix,4,3);
system("PAUSE"); return 0;}
Passing 3D Array to Function (1/2)
#include <iostream>
#include <stdlib.h>
using namespace std;
void get_data(float a[][3][2],int row, int col,int page){
int i,j,k;
for (k=0; k<page; k++){
for (i=0; i<row; i++){
for (j=0; j<col; j++){
cout <<"A["<< i <<"]["<< j <<"]["<< k <<"]:";
cin >> a[i][j][k];
} // end of for (j=0
} // end of for (i=0
} // end of for (k=0
}
Passing 3D Array to Function (2/2)
void show_data(float a[][3][2],int row, int col, int page){
int i,j,k;
for (k=0; k<page; k++){
for (i=0; i<row; i++){
for (j=0; j<col; j++){
cout << a[i][j][k] << "\t";
}
cout << endl;
}
cout << endl;
}
}
int main(){
float matrix[4][3][2]; // 4 rows, 3 columns, 2 pages
get_data(matrix,4,3,2);
show_data(matrix,4,3,2);
system("PAUSE");
return 0;
}
Sorting Data Using Bubble Sort Algo
#include <iostream>
#include <stdlib.h>
using namespace std;
void print( float[], int );
void sort ( float[], int );
int main(){
int i; float data[10];
cout << "Enter 10 Numbers for Sorting \n";
for( i=0 ; i<10 ; i++ ){
cout << "Enter No." <<i+1<< ":" ;
cin >> data[i];
}
sort(data,10);
print(data,10);
system("PAUSE");
return 0;
}
Sorting Data Using Bubble Sort Algo
void sort( float a[], int n ){ // bubble sort:
for (int i=1; i<n; i++) // bubble up max{a[0..n-i]}:
for (int j=0; j<n-i; j++)
if (a[j] > a[j+1]){
float temp = a[j];
a[j]=a[j+1];
a[j+1] = temp;
}
}