You are on page 1of 12

Computer Systems & Programming

Khawaja Fareed University of Engineering and Information


Technology, Rahimyar Khan
Department of Mechanical Engineering
KFUEIT

Computer and Programming lab: Lab manual #7

Submitted by:
Osama Abid
(MEEN 18111016)

Submitted to:
Engr. Faizan Shah

Date of Submission:

17𝑡ℎ July, 2020

1
Computer Systems & Programming

Lab-7

Arrays

OBJECTIVE:

To demonstrate the use of arrays in C++.

EQUIPMENT REQUIRED:
 Laptop
 Computer

TOOLS REQUIRED:
 C++ IDE

Theory & Procedure:

• Array is a group of elements all have the same data type. An individual element of an
array is identified by its own unique index (or subscript).

• In C++ each array has

i. name

ii. data type

iii. size

They occupy continuous area of memory

Storage of Array in the Memory:

2
Computer Systems & Programming

arrayDataType arrayName [numberOfElements];

For example ,

int age [ 10 ] ;

This declaration will cause the compiler to allocate space for 10 consecutive integer
variables in memory.

More than one array can be declared in a line

int age [10] , height [10] , names [20] ;

Mix declaration of variables with declaration of arrays

int i , j , age [10] ;

Initializing an Array

int age [ ] = { 10,11,12,13,14,15,16 };

 Note that the array has not been given a size, the compiler will make it large enough to
hold the number of elements in the list.

• In other words, Array Elements are 7 so by default complier will make the size of array 7.

• It is better not to mention array size

3
Computer Systems & Programming

• If the array is given a size then this size must be greater than or equal to the number of
elements in the initialization list. For example:

int age[10] = {1, 2, 3, 5, 7};

It would reserve space for a ten element array but would only initialize the first five
elements.

Accessing Array Elements

• Given the declaration of a 10 elements array the compiler reserves space for 10
consecutive integer values and accesses these values using an index/subscript that takes
values from 0 to 9.

• The first element in an array in C++ always has the index 0, and if the array has n
elements the last element will have the index n-1.

• An array element is accessed by writing the identifier/name of the array followed by the
subscript in square brackets.

• For Example: To set the 5th element of the given array to 15 the following assignment
is used:

age[4] = 15;

• Note that since the first element is at index 0, then the ith element is at index i-1. Hence
in the above the 5th element has index 4.

Array Index Out Of Bound

#include<iostream.h>
void main ()
{
const int sizeOfArray=4;
float a[sizeOfArray] = { 33.3,44.4,55.5,66.6,2.3 };
for (int i=0; i<5; i++) // ERROR: index is out of bounds!
cout << "\ta[" << i << "] = " << a[i] << endl;
}

4
Computer Systems & Programming

Inserting Values in an Array

#include<iostream.h>
void main ()
{
int i, n[10];

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


{
cout<<"Enter the Elements of Array "<<i<<" = ";
cin>>n[i];
cout<< endl;
}
//Display Array
for(i=0; i<10; i++)
{
cout<<"Element "<<i<<" = "<<n[i]<<endl;
}
}

5
Computer Systems & Programming

Exercise:

1. Write a program in C++ that take age of five persons and then just display the age of each
person by using arrays.

Code
#include<iostream>

using namespace std;

int main ()

int age[5];

for(int i=1; i<=5; i++)

cout<<"\tEnter the age of person "<<i<<" : ";

cin>>age[i];

cout<<endl<<endl;

//Display Array

for(int i=1; i<=5; i++)

cout<<"\tAge of person "<<i<<" is "<<age[i]<<"."<<endl;

return 0;

6
Computer Systems & Programming

Result

Figure 1Compiled code

2. Write a program that reads/gets five numbers from user and then prints them in reverse
order:

Output
Enter 5 numbers:
10
20
30
40
50
In reverse order:
50
40
30
20
10

Code
#include<iostream>

7
Computer Systems & Programming

using namespace std;

int main ()

int num[6];

for(int i=1; i<=5; i++)

cout<<"\tEnter any numbers : ";

cin>>num[i];

cout<<endl<<endl;

//Display user entered Array

cout<<"User entered array."<<endl;

for(int i=1; i<=5; i++)

cout<<num[i]<<" ";

cout<<endl<<endl;

//Display Reversed Array

cout<<"Reversed array."<<endl;

for(int i=5; i>=1; --i)

cout<<num[i]<<" ";

8
Computer Systems & Programming

return 0;

Figure 2 Result

3. Write a program to input data into two different arrays and then to add the two arrays and
store the result in the third array.

Code
#include<iostream>

using namespace std;

int main()

int array1[30];

int array2[30];

int sum[30];

int n;

//user initiatlization of array

cout << "Enter the number of elements for array : ";

9
Computer Systems & Programming

cin >> n;

cout << "Enter the values of 1st array\n";

for (int i = 1; i <= n; i++)

cin >> array1[i];

cout << "Enter the values of 2nd array\n";

for (int i = 1; i <= n; i++)

cin >> array2[i];

cout << "Sum of two arrays is " << endl;

for (int i = 1; i <= n; i++)

sum[i] = array1[i] + array2[i];

cout << sum[i] << endl;

return 0;

10
Computer Systems & Programming

Figure 3 Compiled code

Rubric 1

Marks CLO3 – Level C2 mapped to PLO2 (Problem Analysis)

02 Is not able to write the code in C++ using module programming and
interpret it. Major help is required in writing the program.

06 Can write and interpret C++ codes using module programming with
minor error help.

10 Can write and interpret C++ codes using module programming


effectively and confidently.

11
Computer Systems & Programming

12

You might also like