0% found this document useful (0 votes)
2 views

Lab 01

This document provides a comprehensive overview of arrays in C++, covering their declaration, initialization, and operations such as accessing elements, displaying values, and calculating sums and averages. It includes examples of code demonstrating how to work with arrays, including user input and basic array manipulations. Additionally, it outlines in-lab and post-lab tasks for practical application of the concepts learned.

Uploaded by

Muqeet Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 01

This document provides a comprehensive overview of arrays in C++, covering their declaration, initialization, and operations such as accessing elements, displaying values, and calculating sums and averages. It includes examples of code demonstrating how to work with arrays, including user input and basic array manipulations. Additionally, it outlines in-lab and post-lab tasks for practical application of the concepts learned.

Uploaded by

Muqeet Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB 1: Operations related to Arrays

Objective:
In this lab, we will learn to work with arrays i.e to perform different operation on arrays.

Introduction:
In C++, an array is a variable that can store multiple values of the same type. For example,

Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27
separate variables, we can simply create an array:

Int array[27];

Here, grade is an array that can hold a maximum of 27 elements of double type.

In C++, the size and type of arrays cannot be changed after its declaration.

Array Declaration:
dataType arrayName[arraySize];

For example,

int x[6];

Here,

• int - type of element to be stored


• x - name of the array
• 6 - size of the array

Access Elements in Array


In C++, each element in an array is associated with a number. The number is known as an array index.
We can access elements of an array by using those indices.

// syntax to access array elements

array[index];

Consider the array x we have seen above.


Elements of an array

• The array indices start with 0. Meaning x[0] is the first element stored at index 0.
• If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the
last element.
• Elements of an array have consecutive addresses. For example, suppose the starting address
of x[0] is 2120d. Then, the address of the next element x[1] will be 2124d, the address
of x[2] will be 2128d and so on.

Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.

Array Initialization:
In C++, it's possible to initialize an array during declaration. For example,

// declare and initialize and array

int x[6] = {19, 10, 8, 17, 9, 15};

Array elements and their data

Example 1: Displaying Array Elements


#include <iostream>

using namespace std;


int main() {

int numbers[5] = {7, 5, 6, 12, 35};

cout << "\nThe numbers are: ";

// Printing array elements

// using traditional for loop

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

cout << numbers[i] << " ";

return 0;

Here, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we have printed numbers[i].

Example 2: Take Inputs from User and Store Them in an Array


#include <iostream>

using namespace std;

int main() {

int numbers[5];

cout << "Enter 5 numbers: " << endl;

// store input from user to array

for (int i = 0; i < 5; ++i) {

cin >> numbers[i];

cout << "The numbers are: ";


// print array elements

for (int n = 0; n < 5; ++n) {

cout << numbers[n] << " ";

return 0;

Output:
Enter 5 numbers:

11

12

13

14

15

The numbers are: 11 12 13 14 15

Once again, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we took an input
from the user and stored it in numbers[i].

Then, we used another for loop to print all the array elements.

Example 3: Display Sum and Average of Array Elements Using for Loop
#include <iostream>

using namespace std;

int main() {

// initialize an array without specifying size

double numbers[6] = {7, 5, 6, 3, 1, 2};

int i,count=0;
double sum = 0;

double average;

cout << "The numbers are: ";

// print array elements

for (i=0;i<=5;i++) {

cout<<numbers[i]<<" ";

// calculate the sum

sum = sum+numbers[i];

count=count+1;

// print the sum

cout << "\nTheir Sum = " << sum << endl;

// find the average

average = sum / count;

cout << "Their Average = " << average << endl;

return 0;

Output:
The numbers are: 7 5 6 3 1 2

Their Sum = 24

Their Average = 4
In this program:

We have initialized a double array named numbers but without specifying its size. We also declared
three double variables sum, count, and average.

Here, sum =0 and count = 0.

Then we used a for loop to print the array elements. In each iteration of the loop, we add the current
array element to sum.

We also increase the value of count by 1 in each iteration, so that we can get the size of the array by the
end of the for loop.

After printing all the elements, we print the sum and the average of all the numbers. The average of the
numbers is given by average = sum / count

In-Lab Tasks
In-Lab Task 1:
Take an array from the user and then swap its first and last values.

In-Lab Task 2:
Take an element of an array from user, display its location and then take a new element from the user
to replace the previous element.

Post-Lab Tasks

Post-Lab Task:
Take an element of an array from the user to delete it and then display the new array.

You might also like