Anda di halaman 1dari 31

BITG 1113:

Array (Part 1)

LECTURE 8

1
Objectives :
 To understand basic concepts of array.

 To understand how to use arrays in the


program.
 To know how to input values into array
and output values from array.
 To know how to use array as function
parameter.
2
Concepts

• An array is a sequenced collection of elements of the


same name and the same type.

• It serve as an example of structured data types - they are


effectively just lists of variables all of the same data type
("int", "char" or whatever).

3
Example : An array of Numbers

4
Concepts

• When referring to which element in array, we use the


subscript concept
– E.g : numbers1 – number[1]
– This is also known as indexing the elements

• Loops can be used to read and write, also it can be


used to add, subtract, multiply and divide the
elements in arrays

• The subscript starts with 0


E.g : number[0] for the first element

5
Using arrays in C++

6
Read or Print
using index

Example of an array
flowchart
7
Declaring and defining arrays
• The general syntax for an array declaration is:
– <component type> < identifier name>[<size>];
– Declaration and definition tells the compiler the name of
the array, type of each element and size or number of
element in the array

• E.g : suppose we are writing a program to


manipulate data concerning the number of hours a
group of 6 employees have worked in a particular
week.

• We might start the program with the array


declaration:
int hours[6];
8
Declaring and defining arrays

9
Initialization

10
Inputting Values

• Is another way to fill the array

• Can be done using a loop – the most


appropriate is for loop

• E.g :
int scores[10];
for ( i = 0; i < 10; i++)
cin >> scores [i];

11
Outputting values

• Usually done by using a for loop

E.g :

for (i=0; i<10; i++)


cout << scores[i];

12
Assigning values

• Individual elements can be assigned values


using the assignment operator
– E.g : scores [4] = 23;

• We can copy an array to another array with the


same size and type but it is wrong to assign
one array to another :
– E.g : int num1[5]={1,2,3,4,5},num2;
num2=num1; //error

13
Assigning values
• Example : to copy elements of num1 into num2.

int num1[5] = {1,2,3,4,5}, num2[5];

for(int i =0 ; i<5;i++)
{
num2[i]= num1[i]; //correct
}

14
Example : Input and Output of an array
#include <iostream>
#include <iomanip>
using namespace std;
 
void main()
{
int i, ary1[5]={2,4,6,8,10},ary2[5],ary3[5];

cout<<“Enter 5 numbers:\n”;

for(i=0; i<5; i++)


cin>>ary2[i];

cout<<setw(15)<<“ary1”<<setw(10)<<“ary2”<<setw(10)
<<“ary3”<<endl;

for(i=0;i<5;i++)
{
ary3[i]= ary1[i]+ ary2[i];
cout<<setw(15)<<ary1[i]<<setw(10)<<ary2[i]
<<setw(10)<<ary3[i]<<endl;
} 15
}
Output :
Enter 5 numbers:
1
2
3
4
5
ary1 ary2 ary3
2 1 3
4 2 6
6 3 9
8 4 12
10 5 15

16
Example : Determine the minimum value
#include <iostream>
using namespace std;

 
void main()
{
int i, score[5], min; Output :
Enter 5 scores:
cout<<“Enter 5 scores:\n”;
85

for(i=0; i<5; i++) 60


cin>>score[i]; 55
78
min = score[0];
95
for(i=1;i<5;i++) The minimum score is 55
{
if(score[i]< min )
min = score[i];
}
cout<<“The lowest score is “ << min; 17
}
Exchanging values – the wrong way

18
Exchanging values – using temporary variable

19
Example : Exchanging values
#include <iostream>
using namespace std;
void main()
{
int temp;
int numbers[5] = {3,7,12,24,45};
 
cout << "Before exchange the value :"<< endl;
for(int i=0; i<5; i++)
cout << numbers[i] << ' ';

temp = numbers[3];
numbers[3] = numbers[1];
numbers[1] = temp;
 
cout<<"\nAfter exchange the value :"<<endl;
for(int i=0; i<5; i++)
cout << numbers[i] << ' ';
 
cout << endl;
20
}
Array Applications

• Common statistical application using array


– Frequency distributions and histograms

• Frequency array
– Shows number of identical element in a series
of number

• Histograms
– A pictorial representation of a frequency array

21
Frequency array

22
Frequency histogram

23
Two dimensional array
• An array that consists of rows and columns
– In C++ it means an array of two-dimensional array

24
25
Two dimensional array
• Two dimensional array declaration
int table [5][4];

• First dimension specifies the number of rows in the array


• Second dimension specifies the number of columns in each row

• Initialization
int table [5][4] =
{0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,
31,40,41,42,43};

int table [5][4] = {{0,1,2,3},{10,11,12,13},


{20,21,22,23},
{30,31,32,31},{40,41,42,43}};

int table [5][4] = {{0,1},{10},{20,21,22},{30},


{40,41,42,43}};

26
Inputting Values
• for(r=0;r<5;r++)
for(c=0;c<4;c++)
cin>>table[r][c];

Outputting Values

 for(r=0;r<5;r++){
for(c=0;c<4;c++)
cout<<table[r][c]<<“ “;
cout<< endl;
}
27
Accessing Values

• To access the element of a two dimensional array, the index of row


and column are used.
E.g : To print the element in the second row and the third column of
table.
int table [5][4] = {{0,1,2,3},{10,11,12,13},{20,21,22,23},
{30,31,32,31},{40,41,42,43}};

cout<<table[1][2];// print 12

• Individual elements can be initialized using the assignment


operator.
table[2][0] = 23;

28
Accessing Values
• Let us assume that we want to initialize our 5 x 4 array as shown below.

0 1 2 3
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43

• The following code will perform the initialization :

- int table[5][4];
for(r=0;r<5;r++)
for(c=0;c<4;c++)
table[r][c]= r*10 + c ;

29
Example: Two Dimensional array
#include <iostream>
using namespace std;
 
void main()
{
int i,j,ary1[4][3];

for(i=0; i<4; i++){


cout << “Enter 3 numbers for row :"<<i+1;
for(j=0; j<3; j++)
cin>>ary1[i][j];
cout<<endl;
}

cout<<“Elements of ary1 is : ”<<endl;

for(i=0; i<4; i++){


for(j=0; j<3; j++)
cout<<ary1[i][j]<<setw(5);
cout<<endl;
30
}
Output :
Enter 3 numbers for row 1 : 1 2 3
Enter 3 numbers for row 2 : 2 4 6
Enter 3 numbers for row 3 : 3 6 9
Enter 3 numbers for row 4 : 4 8 12

Elements of ary1 is :
1 2 3
2 4 6
3 6 9
4 8 12

31

Anda mungkin juga menyukai