Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Array Programming

The document discusses arrays in C++. It defines one-dimensional and two-dimensional arrays. One-dimensional arrays have one subscript while two-dimensional arrays are arrays of arrays with at least two dimensions - rows and columns. Examples are provided to demonstrate initializing, declaring, and accessing elements in one-dimensional and two-dimensional arrays using for loops. The examples also show calculating totals and averages stored in arrays.

Uploaded by

intensity
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Array Programming

The document discusses arrays in C++. It defines one-dimensional and two-dimensional arrays. One-dimensional arrays have one subscript while two-dimensional arrays are arrays of arrays with at least two dimensions - rows and columns. Examples are provided to demonstrate initializing, declaring, and accessing elements in one-dimensional and two-dimensional arrays using for loops. The examples also show calculating totals and averages stored in arrays.

Uploaded by

intensity
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CHAPTER 4

ARRAY & STRUCTURES


4.1 ARRAY
Course Learning Outcomes:
CLO1:
Apply program structure and debugging process in C++
programming language accordingly.
CLO2:
Design programs using appropriate control structures,
arrays, structures, functions and pointers.
CLO3:
Solve problems using C++ programming language
environment with proper coding style guidelines and take in
the security issues into consideration.
Introduction
An array is a list of more than one variable with the same
name.
Most applications require the processing of multiple data
items that have common characteristics.
Each array element (individual data item) is referred to by
specifying the array name followed by one or more
subscripts / index (position of elements).
Two type of array:
 One dimensional
 Multi dimensional
One Dimensional
Two Dimensional
Introduction
• Example 1:
char name[6] = “NARUTO”;

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

int age[4] = {19,20,17,22};

19 age [0]
Size
20 age[1] (Only at declaration)
17 age [2]
22 age [3]

Elements Indexs / Subscripts


(Start with 0 until n-1)
One Dimensional Array
• One-dimensional array is an array with one subscript.

• 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]

 int Sem[] = {2, 5, 6, 4, 7, 3};


2 5 6 4 7 3
Sem[0] Sem[1] Sem[2] Sem[3] Sem[4] Sem[5]
One Dimensional Array
Example 1 :

#include<iostream.h>
void main()
{ int quantity [4] = {5,7,12,32};
cout<<"\nInsert quantity:";
cin>>quantity[2];

cout<<"\nElement in index 0:"<<quantity[0];


cout<<"\nElement in index 1:"<<quantity[1];
cout<<"\nElement in index 2:"<<quantity[2];
cout<<"\nElement in index 3:"<<quantity[3];
}
One Dimensional Array
Example 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;

for (r = 0; r < 3; r++)


{
for (c = 0; c < 5; c++)
{
BIL[r][c] = count+=5;
}
}
}
Try this..
• Write the simple program that will show the output as
below:
Answer
#include<iostream.h>

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;

You might also like