You are on page 1of 6

ARRAYS

OBJECTIVE (S):
At the end of this laboratory activities the students will:
1. Show the advantage of using arrays.
2. Familiar in accessing stored inputs in an array.
3. Use the different types of array in programming.

EQUIPMENT/MATERIALS
PC with keyboard and mouse
DOS-based C++ application

DISCUSSION
An array is a number of fixed-size variables that is referenced by a common name. The variables are referenced
by an index. In C++, the starting index is zero (0). An array name is just like any variable, which can store different
values without having to use different variable names. The general syntax for array declaration is:

data_type array_name [array_size];

Arrays usually have a size that is constant.
int const num = 9;
int score[num];
Array elements can also be initialized using any of the following formats:
data_type array_name[array_size]={value1,value2, , valueN};

data_type array_name[]={value1,value2, , valueN};

Thus, the example above can be initialized as:
int const num= 9;
int score[num]={70,75,80,85,90,95,100,80,90};

When array elements are initialized, the size of the array can be omitted. If this is the case, the computer will
allot the number of the initializes as the size of the array. When the initializing list is less than that of the declared
array size, the values of the elements that are not declared will be initially set to zero, 0.

int score[]= {70,75,80,85,90,95,100,80,90};

This array can be viewed in the computer as:


score[0]

score[1]

score[2]

Score[3]

Score[4]

score[5]

score[6]

score[7]

score[8]

70

75

80

85

90

95

100

80

90

Arrays are also useful in forming tables or matrices. That can be implemented using a two-dimensional array.
As the name suggests, it has two dimensions: a row size and a column size.

data_type array_name [row_size] [column_size];

Elements of a two-dimensional array can be initialized using any of the following methods:

data_type array_name [row_size][column_size]={value1, value2, , valueN };

data_type array_name [row size][column_size]={{elements of row1},
{elements of row2},
{elements of rowN}};

As with single-dimensional arrays, row and column sizes of a two-dimensional array need to be declared as
constants. Also, when initializing the elements of the two-dimensional array, the row size is optional but the column
size must be defined so that the compiler may know the limit of the array.
As an example, the succeeding initializations are valid and are all the same:
int matrix [ 2 ] [ 3 ] = { 8, 3, 1, 0, 5, 18 };
int matrix [ ] [ 3 ] = {8, 3, 1, 0, 5, 18 };
int matrix [ 2 ] [ 3 ] = { { 8, 3, 1 }, { 0, 5, 18 } };
int matrix [ ] [ 3 ] = { { 8, 3, 1 }, { 0, 5, 18 } };

That particular array can be viewed as:
col 1 col 2 col 3
row 1 8 3 1
row 2 0 5 18

In accessing individual array elements, the index or the respective row and column positions should be
identified. Any valid expression that leads to an integer can be used as a proper index of any array element. But
since the compiler does not perform bounds checking, it is the responsibility of the programmer to assure that any
element being accessed is within the range of the array size.






Sample Output:



















Sample Output:

Sample Program 6.1
// a program that displays the highest element of an array
#include <iostream.h>
#include <conio.h>

int main( )
{ clrscr( );
const int NUM=5;
int val[NUM], x, y, max;
cout<<"Enter the array elements: ";

for ( x=0; x<NUM; x++ )
cin>>val[x];

max=val[0];

for ( y=1; y<NUM; y++ )
if (max<val[y])
max=val[y];

cout<<endl <<The maximum element is "
<<max <<endl;
getch( );
return 0;
}
Sample Program 6.2
// a program that will accept five scores and then
// computes and displays the average
#include<iostream.h>
#include<conio.h>
int main( )
{ clrscr( );
int score[9], j, sum=0;
float ave;
cout<<"Enter the scores: ";
for ( j=0; j<5; j++ )
{ cin>>score[j];
sum = sum + score[j];
}
ave = sum / 5.0;
cout<<endl <<"The average is " <<ave;
getch( );
return 0;
}

Enter the array elements: 10
20
30
40
50

The maximum element is 50

Enter the scores: 75
76
73
72
79

The average is 75
APPLICATION PROGRAM
Direction: Save your program as EXP6A.CPP, EXP6B.CPP, and EXP6C.CPP in your local disk.

1. Using one-dimensional array, make and run a program that will allow a user to input 15 numbers and sort
them from highest to lowest.
Sample Output:






2. Make and run a program that will let the user to input two numbers. Display a multiplication table using the
entered numbers as its size. Set maximum size to 20.
Sample Output:






3. Make and run a program that will allow the 2 users that will act as a player to participate in XOX game
Sample Output:
COL
/
ROW
1 2 3
1 X O O
2 O X X
3 O X X
Player 1 (X) 3 3
Player 2 (O) 3 1
Player 1 with X symbol wins!!!
What are the 20 numbers?
5 9 6 1 0 2 11 15 16 78 52 3 4 7 10 8 12 0 13 14
The highest to lowest order will be:
0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 78 52

I will create a multiplication table for you!!
How many row and column do you want? 2 3
1 2 3
1 1 2 3
2 2 4 6

QUESTIONS
1. How many data types can the elements of an array have?









2. What happens if the number of initializes is more than the size of the array?









3. Suppose that we have the following array declaration:

const int mean = 6;
int num[mean];

What will be the value displayed if cout<<num[8]; is executed?

You might also like