Lab Manual 9
Lab Manual 9
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.
3. Used to Implement other data structures like Stacks, Queues, Heaps, Hash
tables, etc.
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.
Here, the size of each element is increased by 4. This is because the size
of int is 4 bytes.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}
#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}