Nothing Special   »   [go: up one dir, main page]

8-Lecture8 ODM103 C++Arrays

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

THE OPEN UNIVERSITY OF TANZANIA

FACULTY OF SCIENCE, TECHNOLOGY AND ENVIRONMENTAL STUDIES


ICT DEPARTMENT

ODM 103 –INTRODUCTION TO COMPUTER PROGRAMMING


LANGUAGES

LECTURE EIGHT: ARRAYS IN C++


C++ Arrays
In programming, one of the frequently arising problem is to handle similar types of data. Consider this
situation: You have to store marks of more than one student depending upon the input from user. These types
of problem can be handled C++ programming (almost all programming language have arrays) using arrays.

An array is a sequence of variable that can store value of one particular data type. For example: 15 int type
value, 105 float type value etc.

Defining Arrays
type array_name[size];

Consider this code.

int test[85];

The above code creates an array which can hold 85 elements of int type.

Array Elements
The maximum number of elements an array can hold depends upon the size of an array. Consider this code
below:

int age[5];

This array can hold 5 integer elements.


Notice that the first element of an array is age[0] not age[1]. This array has 5 elements and notice fifth is
age[4] not age[5]. C++ programmer should always keep this is mind as it may differ from other
programming languages.

Array Initialization
Array can be initialized at the time of declaration. For example:

float test[5] = {12, 3, 4, -3, 9};

It is not necessary to write the size of an array during array declaration.

float test[] = {12, 3, 4, -3, 9};

I prefer writing size of array during array declaration because it quickly gives the information on size of an
array while help me in debugging code.

Example 1: C++ Array


C++ Program to store 5 numbers entered by user in an array and display first and last number only.

#include <iostream>
using namespace std;

int main() {
int n[5];
cout<<"Enter 5 numbers: ";
/* Storing 5 number entered by user in an array using for loop. */
for (int i = 0; i < 5; ++i) {
cin>>n[i];
}

cout<<"First number: "<<n[0]<<endl; // first element of an array is n[0]


cout<<"Last number: "<<n[4]; // last element of an array is n[SIZE_OF_ARRAY -
1]
return 0;
}

Output

Enter 5 numbers: 4
-3
5
2
0
First number: 4
Last number: 0

C++ Multidimensional Arrays


In arrays, you learned about one dimensional array, that is, single variables specifies array. C++ allows
programmer to create array of an array known as multidimensional arrays. Consider this example:

int x[3][4];

Here, x is a two dimensional array. This array can hold 12 elements. You can think this array as table with 3
row and each row has 4 column.

Three dimensional also array works in similar way. For example:

float x[2][4][3];

This array x can hold 24 elements. You can think this example as: Each 2 elements can hold 4 elements,
which makes 8 elements and each 8 elements can hold 3 elements. Hence, total number of elements this array
can hold is 24.

Multidimensional Array Initialisation


You can initialise a multidimensional array in more than one way. Consider this examples to initialise two
dimensional array.

int test[2][3] = {2, 4, -5, 9, 0, 9};


Better way to initialise this array with same array elements as above.

int test[2][3] = { {2, 4, 5}, {9, 0 0}};

Initialisation of three dimensional array


int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,
2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

Better way to initialise this array with same elements as above.

int test[2][3][4] = {
{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
};

Example 1: Two Dimensional Array

C++ Program to display all elements of an initialised two dimensional array.

#include <iostream>
using namespace std;

int main() {
int test[3][2] = {
{2, -5},
{4, 0},
{9, 1}
};
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 2; ++j) {
cout<< "test["<< i << "][" << ;j << "] = " << test[i][j]<<endl;
}
}
return 0;
}
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1

Example 2: Two Dimensional Array


C++ Program to store temperature of two different cities for a week and display it.

#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;

int main() {
int temperature[CITY][WEEK];
cout<<"Enter all temperature for a week of first city and then second city. \n";
for (int i = 0; i < CITY; ++i) {
for(int j = 0; j < WEEK; ++j) {
cout<<"City "<<i+1<<", Day "<<j+1<<" : ";
cin>>temperature[i][j];
}
}
cout<<"\n\nDisplaying Values:\n";
for (int i = 0; i < CITY; ++i) {
for(int j = 0; j < WEEK; ++j) {
cout<<"City "<<i+1<<", Day "<<j+1<<" = "<< temperature[i][j]<<endl;
}
}
return 0;
}
Enter all temperature for a week of first city and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23

Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23

Example 3: Three Dimensional Array

C++ Program to Store value entered by user in three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
int test[2][3][2]; // this array can store 12 elements
cout<<"Enter 12 values: \n";
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cin>>test[i][j][k];
}
}
}
cout<<"\nDisplaying Value stored:"<<endl;
/* Displaying the values with proper index. */
for(int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
for(int k = 0; k < 2; ++k ) {
cout<< "test["<<i<<"]["<<j<<"]["<<k<<"] = "<< test[i][j][k]<<endl;
}
}
}

return 0;
}

Output

Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Value stored:


test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

As the number of dimension increases, the complexity also increases tremendously although the concept is
quite similar.

Passing Array to a Function in C++ Programming


Arrays can be passed to a function as an argument. Consider this example to pass one-dimensional array to a
function:

Example 1: Passing One-dimensional Array to a Function


C++ Program to display marks of 5 students by passing one-dimensional array to a function.

#include <iostream>
using namespace std;
void display(int marks[5]);

int main() {
int marks[5] = {88, 76, 90, 61, 69};
display(marks);
return 0;
}

void display(int m[5]) {


cout<<"Displaying marks: "<<endl;
for (int i = 0; i <5; ++i) {
cout<<"Student "<<i+1<<": "<<m[i]<<endl;
}
}

Output

Displaying marks:
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69

When an array is passed as an argument to a function, only the name of an array is used as argument.

display(marks);

Also notice the difference while passing array as an argument rather than variable.

void display(int m[5]);

The argument used marks in the above code represents the memory address of first element of array
marks[5]. And the formal argument int m[5] in function declaration decays to int* m;. That's why,
although the function is manipulated in the user-defined function with different array name m[5], the original
array is manipulated. The C++ programming language handles passing array to a function in this way to save
memory and time.

Note: You need to have understanding of pointers to understand passing array to a function. Learn more: Call
by reference

Passing Multidimensional Array to a Function


Multidimensional array can be passed in similar way as one-dimensional array. Consider this example to pass
two dimensional array to a function:

Example 2: Passing Multidimensional Array to a Function


C++ Program to display the elements of two dimensional array by passing it to a function.

#include <iostream>
using namespace std;
void display(int n[3][2]);

int main() {
int num[3][2] = {
{3, 4},
{9, 5},
{7, 1}

};
display(num);
return 0;
}
void display(int n[3][2]) {

cout<<"Displaying Values: "<<endl;


for(int i = 0; i < 3; ++ i) {
for(int j = 0; j < 2; ++j) {
cout<<n[i][j]<<" ";
}
}
}

Output

Displaying Values:
3 4 9 5 7 1

Multidimensional array with dimension more than 2 can be passed in similar way as two dimensional array.

SUMMARY

int age; // normal variable


int age []; //array of 1D
int age [][]; //array of 2D
int age [100]; //This is array of 1D and 100 size
(100 elements/values)
int age [3] = {34,45,-2};
[0]=34, [1]=45, [2]=-2
The first element of array starts with index 0 and
the last is with index N-1

Eg: for the array of [100] i.e.


indexes 0 to 99.

You might also like