Array Programming
Array Programming
N name [0]
Size
A name [1]
R name [2]
U name [3]
T name [4]
O name [5]
Elements Indexs/
subscripts
Introduction
• Example 2: Elements
19 age [0]
Size
20 age[1] (Only at declaration)
17 age [2]
22 age [3]
• Syntax :
DataType arrayVariable[element-size];
• Example:
char title [7];
float price[100];
One Dimensional Array
Initialization :
• You must assign an initial value to the array elements
before using them.
• example :
float Height[3]={145.5,169.8,179.0};
145.5 169.8 179.0
Height [0] Height [1] Height [2]
#include<iostream.h>
void main()
{ int quantity [4] = {5,7,12,32};
cout<<"\nInsert quantity:";
cin>>quantity[2];
#include<iostream.h>
void main()
{
int num[10];
cout<<"Key in 5 integer number:\n";
for(int x=0; x<5; x++)
{
cin>>num[x];
}
cout<<"\nElements in num array :";
for(int y=0; y<5; y++)
{
cout<<num[y]<<"\t";
}
}
One Dimensional Array
Example 3
#include<iostream.h>
int main()
{
int a;
int number[6];
for(a=0;a<6;a++)
{
number[a]=a+5;
cout<<"number["<<a<<"] is intialized with = “<<number[a]<<endl;
}
return 0;
}
One Dimensional Array
Example 4
#include <iostream.h>
int main ()
{
int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
Two Dimensional Array
• It is actually an array of arrays.
• Sometimes called tables or matrices,
• Having at least two dimensions – rows and columns.
• Syntax :
dataType arrayVariable[rowSize][columnSize];
• Example:
int coordinate [7][4];
float temp[50][100];
Two Dimensional Array
• Array elements are stored row by row.
• Data in the two-dimensional array, always in the same type of
data.
• The number of dimensions corresponds to the dimensions in
the physical world.
• Example:
int a[3][4];
Row
Col
Two Dimensional Array
• Initialization :
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
a[0] 1 2 3 4
a[1] 5 6 7 8
a[2] 9 10 11 12
a[0] a[1] a[2] a[3]
int b[4][3]={{2,5,6},{4,7,3},{1,6,7},{9,8,1}};
[0] 2 5 6
[1] 4 7 3
[2] 1 6 7
[3] 9 8 1
[0] [1] [2]
Two Dimensional Array
• Example 1:
#include<iostream.h>
void main()
{
int bil[3][4]={10,21,33,45,5,65,76,80,90,11,23,4};
for(int row=0;row<3;row++)
{
for(int col=0;col<4;col++)
{
cout<<bil[row][col]<<"\t";
}
cout<<endl;
}
}
Two Dimensional Array
• Example 2:
#include<iostream.h>
void main()
{ int BIL[3][5];
int r, c, count = 10;
void main()
{
int nombor[10]={3,4,5,6,7,8,9,10,11,12};
for(int a=0;a<10;a++)
{
cout<<"Number ["<<a<<"] is initialized with ="<<nombor[a]<<endl;
}
}
Try this
• Write the simple program that will show
the output as below:
Answer
#include<iostream.h>
void main()
{
int a,b;
int array[5][3]={1,0,1,1,2,3,2,4,5,3,6,7,4,8,9};
for(a=0;a<5;a++)
for(b=0;b<3;b++)
{
cout<<"Array["<<a<<"]["<<b<<"]="<<array[a][b]<<endl;
}
}
Write how the looping process
executed
#include<iostream.h> for(i=0;i<10;i++)
{
void main() if(mark[i]>average)
{ bil++;
int mark[10]; }
int i,bil=0, total=0;
double average; cout<<"Average is"<<average<<endl;
cout<<"There are"<<bil<<"marks are higher
for(i=0;i<10;i++) than average"<<endl;
{ }
cout<<"Enter mark:";
cin>>mark[i];
total=total+mark[i];
}
average=total/10;
Run this program
#include<iostream.h> for(i=0;i<4;i++){
for(j=0;j<5;j++){
void main() jum[i]+=nilai[i][j];
{ }}
int nilai[4][5]={5,4,3,2,1,
for(i=0;i<4;i++)
2,3,4,5,2, {
cout<<i<<" "<<jum[i]<<endl;
1,2,4,3,2, }
}
2,3,1,2,4};
int jum[4]={0,0,0,0};
int i, j;