Lab 6 Arrays
Lab 6 Arrays
Lab 6 Arrays
Programming Fundamentals
CS-323
C++ Arrays
We will learn to work with arrays. We will learn to declare, initialize, and access array elements in C++
programming with the help of examples.
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];
In C++, the size and type of arrays cannot be changed after its declaration.
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.
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 2120.
Then, the address of the next element x[1] will be 2124, the address of x[2] will be 2128,
and so on.
Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
Here, we have not mentioned the size of the array. In such cases, the compiler automatically
computes the size.
In C++, if an array has a size n, we can store upto n number of elements in the array. However,
what will happen if we store less than n number of elements.
For example,
Here, the array x has a size of 6. However, we have initialized it with only 3 elements.
In such cases, the compiler assigns random values to the remaining places. Oftentimes, this
random value is simply 0.
Copying arrays
#include <iostream.h>
int main()
int age[4];
int same_age[4];
int i=0;
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
for (;i<4;i++)
same_age[i]=age[i];
for (i=0;i<4;i++)
cout<<same_age[i]<<endl;
return 0;
int a[5];
cin>>a[0]; // this will scan the value for the very first location of the array.
cout<<a[0]<<endl;
You can also scan the entire elements of the array using a loop.
You can also store characters and other type data (float etc.) in the arrays. Just
declare it as we’ve done in the case with int. There is no difference in dealing
with characters except you’ve to enclose the value in a single quote.
Char ar[3];
Here, we have used a for loop to iterate from i = 0 to i = 4. In each iteration, we have
printed numbers[i].
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].
Exercise
1. Write a program that asks for the number of hours worked by six employees. It stores the
values in an array.
2. Write a program that asks the user to type 10 integers of an array. The program must
compute and write how many integers are greater than or equal to 10.
3. Write a C++ program to swap first and last element of an integer 1-d array.
4. Write a C++ program to input 5 numbers and find their sum and average.
5. Write a C++ program to reverse the perfect number of an array.
6. Write a C++ program to display the even and odd of N numbers.
7. Write a C++ program to find the largest three elements in an array.