0% found this document useful (0 votes)
37 views15 pages

C++ Arrays: Programming Fundamentals Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views15 pages

C++ Arrays: Programming Fundamentals Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Lab Manual No 09,10

Programming Fundamentals
Arrays in C++

Semester: FALL 2023

CLO statement

Apply basic programming concepts in C++

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Objectives:-

The objectives of this session is to learn working and advantages of array in C++.

ARRAYS:-

An array is a sequence of objects of same data type. The objects in an array are also called
elements of array.

An array is represented in the computer memory by a consecutive group of storage locations.


These locations are referenced by a single variable called array name. Each element of an array
is referenced by its position in an array.

The position of an element in an array is represented by an index value or subscript. In an array


with “n” elements, the index values are 0,1,2,-----,n-1, where ‘0’ represents the index value of
first element and ‘n-1’ represents the index value of the last element.

An element of an array is accessed by its subscript value. The subscript or index value is written
inside a pair of square brackets [ ] with the name of array. An element of an array is referenced
by specifying name of the array and the index value of the element.

Arrays are used to process a large amount of data of same type. The data is stored in an array.
The array is accessed by a single variable name. The index values are used to access individual
elements of an array. A few statements are required to process data in an array. Therefore, the
use of arrays in a program reduces size of the program.

Arrays are divided into two types. These are:

i. One-Dimensional Arrays.
ii. Multi-Dimensional Arrays.

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

One Dimensional Array: -

One dimensional array/single-dimensional array is also known as linear list or a linear array. It
consists of only one column and one row.

Data_Type Identifier [Length]


Data_Type: Indicates the data types of the values to be stored in array.
Identifier: Indicates the name of the array
Length: Indicates total no. of values in array.
For example, the temperature of each hour of a day is stored in an array. The name of the array
is “temp” and its elements are
temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]………. , temp[23].

temp
temp[0] 22.2
temp[1]
temp[2] 23.5
temp[3] 19.7
temp[4]
temp[5]
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

temp[23] 15.3

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

The above array “temp” contain real type data. It has 24 elements. The first element of the
array is temp[0] that is in position 0 and temp[1] is the second element of array and is in
position 1. Similarly, 24th element is the last element and it reference is temp[23].

Declaring One Dimensional Arrays:-

Like other variables, an array is also declared. Defining the name of array, its type and the total
number of element of an array is called declaring of an array. When an array is declared a
memory block with required number of location is reserved in the computer memory for storing
data into elements of an array.

The general syntax to declare a one dimensional array is:

type array-name[n];

where “n” is an unsigned integer value. It represents the total number of elements of an array.
To declare a one dimensional array “temp” of type double with 24 elements, the statement is
written as:

double temp[24];

Similarly to declare a one dimensional array with array name “abc” having five elements and
of integer type, the statement is written as:

int abc[5];

Accessing Data in one Dimensional Array:-

Each element of an array is referenced by its index. In an array of n elements, each element has
an index value. The index of the first element is 0 and of the last element is n-1.

The index value is written within square brackets after the array name.

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Thus the first element of an array is referenced as temp[0]. Similarly the last element of an
array is referenced as temp[n-1]. Also access the array elements using loops as :

Int marks[5];
for(int i=0; i<5; i++)
marks[i] = i;
Above code use for loop to store different values in array. It uses counter variable I as an index.
In each iteration, value of i changed. The statement marks[i] refers to different array element
in each iteration.

Input/ Output Data in One Dimensional Arrays:-

Data is entered in an array using the input statements like “cin” or “assignment” statements. It
is entered into individual elements of an array. Separate input statement is used to enter data
into each element of an array.

Similarly to print the data from an array, the output statement are used. The data from each
individual element of an array is accessed.

Since similar input/output statement is used to access data in each element of the array, the
statement is written once and a loop structure is used to repeat the input/output statement to
access data of the elements of the array.

In the following program, an assignment statement has been used to input data into elements
of an array. The “cout” output statement has been used to print data of the elements of an array
on the screen. The loop structure repeats the output statement for each individual element of
the array by changing index values.

Example :1

Write a program that inputs five integers from the user and stores them in a array. It
then displays all the values in a array without using loops.

#include <iostream>

#include<cstdlib>

using namespace std;

int main()

int arr[5];

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

cout<<"Enter Five Integers : "<<endl;

cin>>arr[0];

cin>>arr[1];

cin>>arr[2];

cin>>arr[3];

cin>>arr[4];

cout<<"The Values in Array Are : \n";

cout<<arr[0]<<endl;

cout<<arr[1]<<endl;

cout<<arr[2]<<endl;

cout<<arr[3]<<endl;

cout<<arr[4]<<endl;

system("pause");

return 0; }

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Example 2:-

Write a program to declare an array of 5 elements and assign the values to the array and
then print the values on the screen.

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
float a[5];
int i;

a[0]=9.9;
a[1]=12.9;
a[2]=13.1;
a[3]=8.9;
a[4]=10.6;
for(i=0; i<=4;i++)
cout<<"Value in a["<<i<<"]= "<<a[i]<<endl;

system("pause");
return 0; }

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Example 3:-

Write a program that inputs no of persons and their ages respectively. It then counts the
no. of persons in age group 50 to 60.

#include <iostream>
#include<cstdlib>

using namespace std;

int main()

{
int age[150];
int i,n,count=0;
cout<<"Enter the no of persons : ";
cin>>n;
cout<<"Enter Ages Of "<<n<<" persons"<<endl;

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

{
cin>>age[i];
if(age[i]>=50 && age[i]<=60)
count= count+1;

cout<<"Persons Between 50 and 60 are : "<<count<<endl;

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

system("pause");
return 0;
}

Example 4:-

Write a program in C++ to enter integer type data into an array and then to print the values in
reverse order.

#include<iostream>

#include<cstdlib>

using namespace std;

int main() {

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

int abc[5],i;

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

cout<<"Enter element no "<<i+1<<" of the array \n";

cin>>abc[i];

cout<<"The values in reverse order are: \n\n";

for(i=4;i>=0;i--)

cout<<"Value at location "<<i+1<<" is "<<abc[i]<<endl;

system("pause");

return 0; }

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Two-Dimensional Array:

A two-dimensional array in C++ is the simplest form of a multi-dimensional array. It can be


visualized as an array of arrays. It can be considered as a table that consist of rows and columns.
Each element in 2-D array is referred with the help of two indexes. One index is used to indicate
the row and the second index indicates the column of that element.

Syntax:

Data_Type Identifier [Rows][Cols];


Data_Type: Indicates the data types of the values to be stored in array.
Identifier: Indicates the name of the array
Rows: Indicates total no. of rows in the array.
Cols: Indicates total no. of columns in the array.

Examples:
Two dimensional array:
int two_d[10][20];
Three dimensional array:
int three_d[10][20][30];
Total number of elements that can be stored in a multidimensional array can be calculated by
multiplying the size of all the dimensions. i.e

The array int x[10][20] can store total (10*20) = 200 elements.

Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.

Initializing Two- Dimensional Arrays:-

• Elements in two-dimensional arrays are commonly referred by x[i][j]


where ‘i’ is the row number and
‘j’ is the column number.
• A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where
the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1).
• A two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

There are two ways in which a Two-Dimensional array can be initialized.

First Method:

int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}


The above array has 3 rows and 4 columns. The elements in the braces from left to right are
stored in the table also from left to right. The elements will be filled in the array in the order,
first 4 elements from the left in first row, next 4 elements in second row and so on.

Better Method:

int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};


This type of initialization make use of nested braces. Each set of inner braces represents one
row. In the above example there are total three rows so there are three sets of inner braces.

Accessing Elements of Two-Dimensional Arrays: Elements in Two-Dimensional arrays are


accessed using the row indexes and column indexes.

int x[2][1];
The above example represents the element present in third row and second column.
Note: In arrays if size of array is N. Its index will be from 0 to N-1. Therefore, for row index 2
row number is 2+1 = 3.
To output all the elements of a Two-Dimensional array we can use nested for loops. We
will require two for loops. One to traverse the rows and another to traverse columns.

Example 1:

Program to print the elements of a Two-Dimensional array

#include <iostream>
#include<cstdlib>

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

using namespace std;


int main()
{
// an array with 3 rows and 2 columns.
int x[3][2] = {{0,1}, {2,3}, {4,5}};
// output each array element's value
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << "Element at x[" << i
<< "][" << j << "]: ";
cout << x[i][j]<<endl;
}
}
system("pause");
return 0;
}

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Example: 2

A program that stores and displays integer values in an array of 2 rows and 4 columns.

#include <iostream>
#include<cstdlib>
using namespace std;
int main()
{
int arr[2][4],i,j;

for (int i = 0; i < 2; i++)


{
for (int j = 0; j < 4; j++)
{
cout <<"Enter The Integers : ";
cin >>arr[i][j];
}
}

for(i=0;i<2;i++)
{
for (j=0;j<4;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;

}
system("pause");
return 0;
}

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

Department Of Computer Science

Lab Task: -

1. Write a program that inputs ten odd values from the user. Calculate the sum and
average of the elements and then print the sum and average on the screen.
2. Write a program that inputs current day and month from the user. It then calculates
and displays the total number of days in current year till the entered date using
arrays
3. Write a program that inputs 10 no.s from the user in array and print the maximum
value in array.
4. Write a program that allows the user to enter the number of votes received by 5
candidate. The program should then output candidate’s number, the number of
votes received, and the percentage of the total votes received by the candidate. A
sample output is:

Candidate Votes Received % of Total Votes


1 5000 25.91

Programming Fundamentals Lab Instructor: Muhammad Faheem Saleem


Session:- CS- 2K23

You might also like