You are on page 1of 18

Computer

Programming 2
Arrays
Array

• An array is a list of elements of the same data type. You can


store value to array similar to variable.

Types of Array
– Single-dimensional Array – an array which has sing set of element.
– Two-dimensional Array – has two sets of elements. The values are
stored in a row-column matrix
– Multi-dimensional Array – has more than two sets of elements
Declaring Single-Dimensional
Array
• The syntax for declaring an array:
data_type array_name[size];

For instance:
int no[5];
Creates an array call no which consists of 5 elements that can store
integer numbers.
0 1 2 3 4
no
Storing value to array

• The syntax of storing value to array:


array[element] = value;
Example:
no[2] = {14};
• You can also store value to array using cin:
cin>>array[element];
Example:
cout<<“Enter value for array no[1]: ”;
cin>>no[1];
Accessing value to array

• To access the content of an array, you must also specify the element
number of the array that you want to access the value.

• Example:
no[1] = 10;
cout<<no[1];

The output will be 10.


Sample Program of storing and
accesing values of array
#include <iostream>
using namespace std;

int main ()
{
int testScore[3];
cout << "Enter test score #1: ";
cin >> testScore[0];
cout << "Enter test score #2: ";
cin >> testScore[1];
cout << "Enter test score #3: ";
cin >> testScore[2];
cout << "Test score #1: " << testScore[0] << endl;
cout << "Test score #2: " << testScore[1] << endl;
cout << "Test score #3: " << testScore[2] << endl;
system(“PAUSE”);
return 0;
}
Using array in a loop
#include <iostream>
using namespace std;
int main ()
{
int testScore[3];
for (int i = 0; i < 3; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> testScore[i];
}
for (i = 0; i < 3; i++)
{
cout << "Test score #" << i + 1 << ": " << testScore[i] << endl;
}
system(“PAUSE”);
return 0;
}
Using Constant in an array
#include <iostream>
using namespace std;
const int MAX = 3; --- use constant to declare the size of the variable
int main ()
{
int testScore[MAX];
for (int i = 0; i < MAX; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> testScore[i];
}
for (i = 0; i < MAX; i++)
{
cout << "Test score #" << i + 1 << ": " << testScore[i] << endl;
}
system(“PAUSE”);
return 0;
}
Declaring Two-Dimensional
Array
• The syntax for declaring an array:
data_type array_name[row_size][col_size];

For instance:
int grade[3][5];
Declare two-dimensional array grade with 3 row elements and 5 column
elements. The grade array contains 15 elements.
0 1 2 3 4
0
grade 1
2
Two-dimensional Array
Example
#include <iostream>
using namespace std;
int main ()
{
int sale[2][3];
for (row = 0;row<2;row++)
{
cout << “Saleman No. " <<row+1 <<endl<<endl;
for(col=0;col<3;col++)
{
cout<<“\tSale No. ” <<col+1<<“: ”;
cin>>sale[row][col];
}
cout<<endl;
}
Two-dimensional Array
Example
for(row=0;row<2;row++)
{
salesum = 0;
for(col=0;col<3;col++)
{
salesum = salesum + sale[row][col];
}

cout<<“Total Sale of Saleman No. ”<<row+1<<“ is ”<<salesum);


}

system(“PAUSE”);
return 0;
}
Is there any question or
clarification
Machine Problem #1: Single Dimensional Array

Create a program that will ask the user to enter the array size, then
ask to enter the element of the array to store the elements in the
array, then finally display the array element on the screen
Machine Problem #1: Single Dimensional Array

From machine problem #1, add an option to access the index


Passing Array as Function
Arguments
#include <iostream>
using namespace std;
void assignValues(int[], int);
void displayValues(int[], int);
const int MAX = 3;

int main ()
{
int testScore[MAX];
assignValues(testScore, MAX);
displayValues(testScore, MAX);
system("PAUSE");
return 0;
}
Passing Array as Function
Arguments
void assignValues(int tests[], int num)
{
for (int i = 0; i < num; i++)
{
cout << "Enter test score #" << i + 1 << ": ";
cin >> tests[i];
}
}

void displayValues(int scores[], int elems)


{
for (int i = 0; i < elems; i++)
{
cout << "Test score #" << i + 1 << ": "
<< scores[i] << endl;
}
}
Passing Array as Function
Arguments
• The assignValues function is used to assign values to the array.
The displayValues function is used to display values from the
array.
• Each function has two arguments. The first argument is the
array. The second argument is the number of elements in the
array. Each function loops through the array, its first argument,
using as an index limit the number of elements in the array, the
second argument.
• Since the first argument is not just an integer, but an array of
integers, the argument is specified with brackets, [], signifying
that what is being passed is an array.
• Note: You do not, and should not, put a number in the square
brackets in the argument list of either the prototype or the
function header.
Computer
Programming 2
Arrays