8 Lecture FoP Arrays
8 Lecture FoP Arrays
Arrays
Dr Ayesha Zeb
Email: [email protected]
Lecture Contents
• Introduction to arrays
• Multidimensional arrays
• C strings
• Passing arrays to functions
Visualizing Variables and Arrays
Variable X
X= 5
Array A
• Example:
float arr[ 20 ];
• The last array element is one less than the size of the
array.
– If array size is 20, ie. arr[20] array subscripts would be from
arr[0] to arr[19]
Referring to Individual Elements in an Array
Integer Type
Array
Size of Array = 12
3 2 6 0 0
2 7 4
Entering Data into an Array
sum = 0;
for ( i = 0; i < 5; i++ )
sum += marks [i];
cout<<"Average is "<<sum/5<<endl;
Summing the Elements of an Array
int main()
{
const int Size = 10; //variable indicating
size of array
int a[Size] = {87,68,94,100,83,78,85,91,76,87};
int total = 0;
// sum contents of array a
for ( int i = 0; i < Size; i++ )
total += a[ i ];
return 0;
}
Checking Size of an Array
int x[5]={1,2,3,4,5};
cout << sizeof(x);
• Would return 20
Multi Dimensional Arrays (Matrices)
• For example,
arr[i][j]
2-D Array (Matrix)
Images are multidimensional arrays
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Initializing a 2D Array
59 78 14 17
(0,0) (0,1) (0,2) (0,3)
68 28 32 45
(1,0) (1,1) (1,2) (1,3)
(row = 2, col
5 14 12 15
= 0)
(2,0) (2,1) (2,2) (2,3)
6 2 22 1
(3,0) (3,1) (3,2) (3,3)
4x4
Matrix
Reading a 2D Array
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
Example
12 12
54 34
16 3
12 6
43 23
1 2
Class Activity!!
for(int i=0;i<5;i++)
{
for(int j=0;j<2;j++)
cout<<matrix[i][j]<<"\t";
cout<<endl;
}
}
C Strings
• Another Example,
Terminating
zero or “null”
character ‘\0’
cin.get(str_name,str_size);
For example,
int main()
{
const int MAX = 20;// max characters in
string
char str[MAX];
cout<< "Enter a string :" ;
cin.get(str,MAX);
cout<< "You entered: " << str;
}
Multi-Dimensional Strings (Array of Strings)
2D array of characters
String Library
• Function call
somefunct(elem, size)
• Function Header
void somefunct( int array[], int
size_of_elem)
Passing an Array to a Function
#include <iostream>
using namespace std; • When passing an
void displayArray(int arr[], array to a function:
int arrSize) – Pass the size of the
{cout << endl; array
for (int i = 0; i < arrSize; – Any changes you
i++) make to the array are
cout << arr[i] << " "; reflected in your
code.
cout << endl;}
int main()
{int arr[10] = { 5,6,10 };
displayArray(arr, 10);
return 0; }
Passing an Array to a Function
#include <iostream> {
for (int i = 0; i < arrSize;
using namespace std; i++)
arr1[i] += i;
void displayArray(int arr[], }
int arrSize) int main()
{ {
cout << endl; const int arrSize = 10;
for (int i = 0; i < arrSize; int arr[arrSize] = { 5,6,10
i++) };
cout << arr[i] << " "; displayArray(arr, arrSize);
cout << endl; modifyArray(arr, arrSize);
} displayArray(arr, 10);
void modifyArray(int arr1[], return 0;
int arrSize) }
Activity!!
int main()
{
float no, ans;
int select;
cout <<" enter number for calc of sq or cube
=";
cin >> no;
cout <<"\nenter 2 for sq 3 for cube =";
cin >> select;
ans=sq_cube (no,select);
if (select == 2)
cout<<"Sq of " <<no<<" is
="<<ans<<endl;
else
cout<<"Cube of "<<no<<" is
="<<ans<<"\n\n";
cout <<"\n\n";
}
float sq_cube(float x, int y)
{
if (y == 2)
return x*x;
else return x*x*x;
}
Passing Strings to Functions
int main()
{
char str[SIZEOFSTRING];
cin>>str;
printABC(str );
return 0;
}