0% found this document useful (0 votes)
7 views4 pages

Lab Manual 9

This lab manual for Fall 2021 focuses on C++ arrays, explaining their declaration, initialization, and access methods. It includes examples of using arrays to store data, access elements, and modify values, along with tasks for students to implement. The document also highlights the applications of arrays in programming and data structures.

Uploaded by

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

Lab Manual 9

This lab manual for Fall 2021 focuses on C++ arrays, explaining their declaration, initialization, and access methods. It includes examples of using arrays to store data, access elements, and modify values, along with tasks for students to implement. The document also highlights the applications of arrays in programming and data structures.

Uploaded by

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

Lab Manual (Lab 09)

Session: Fall 2021


School of Systems and Technology
UMT Lahore Pakistan

LAB INSTRUCTOR: AYESHA MAJID ALI


Contact: [email protected] Room #: 504 Last cabin 4th floor SST
C++ Arrays
In this tutorial, we will learn to work with arrays. We have learned to declare, initialize, and access
array elements in C++ programming with the help of examples within our last lab.

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:

double grade[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.

Applications on Array
1. Array stores data elements of the same data type.

2. Arrays can be used for CPU scheduling.

3. Used to Implement other data structures like Stacks, Queues, Heaps, Hash
tables, etc.

Few Things to Remember:


4. The array indices start with 0. Meaning x[0] is the first element stored at
index 0.

5. 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.

6. 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.

Access the Elements of an Array


You access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

Example
#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}

Change an Array Element


To change the value of a specific element, refer to the index number:

#include <iostream>
#include <string>
using namespace std;

int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
return 0;
}

Task 1:
Write a C++ program to store the elements of array ={2,3,5,54,23,12,32,443,5,12,13,43,56,75,3,21} into
two new arrays such that the array named even consist of all even numbers from the given array and the
array named odd consist of all the odd numbers from the given array.

Task 2:
Write a C++ program to sort the given array in descending order and ascending order.
array ={2,3,5,54,23,12,32,443,5,12,13,43,56,75,3,21}

You might also like