Professional Documents
Culture Documents
Submitted by:
Osama Abid
(MEEN 18111016)
Submitted to:
Engr. Faizan Shah
Date of Submission:
1
Computer Systems & Programming
Lab-7
Arrays
OBJECTIVE:
EQUIPMENT REQUIRED:
Laptop
Computer
TOOLS REQUIRED:
C++ IDE
• Array is a group of elements all have the same data type. An individual element of an
array is identified by its own unique index (or subscript).
i. name
iii. size
2
Computer Systems & Programming
For example ,
int age [ 10 ] ;
This declaration will cause the compiler to allocate space for 10 consecutive integer
variables in memory.
Initializing an Array
Note that the array has not been given a size, the compiler will make it large enough to
hold the number of elements in the list.
• In other words, Array Elements are 7 so by default complier will make the size of array 7.
3
Computer Systems & Programming
• If the array is given a size then this size must be greater than or equal to the number of
elements in the initialization list. For example:
It would reserve space for a ten element array but would only initialize the first five
elements.
• Given the declaration of a 10 elements array the compiler reserves space for 10
consecutive integer values and accesses these values using an index/subscript that takes
values from 0 to 9.
• The first element in an array in C++ always has the index 0, and if the array has n
elements the last element will have the index n-1.
• An array element is accessed by writing the identifier/name of the array followed by the
subscript in square brackets.
• For Example: To set the 5th element of the given array to 15 the following assignment
is used:
age[4] = 15;
• Note that since the first element is at index 0, then the ith element is at index i-1. Hence
in the above the 5th element has index 4.
#include<iostream.h>
void main ()
{
const int sizeOfArray=4;
float a[sizeOfArray] = { 33.3,44.4,55.5,66.6,2.3 };
for (int i=0; i<5; i++) // ERROR: index is out of bounds!
cout << "\ta[" << i << "] = " << a[i] << endl;
}
4
Computer Systems & Programming
#include<iostream.h>
void main ()
{
int i, n[10];
5
Computer Systems & Programming
Exercise:
1. Write a program in C++ that take age of five persons and then just display the age of each
person by using arrays.
Code
#include<iostream>
int main ()
int age[5];
cin>>age[i];
cout<<endl<<endl;
//Display Array
return 0;
6
Computer Systems & Programming
Result
2. Write a program that reads/gets five numbers from user and then prints them in reverse
order:
Output
Enter 5 numbers:
10
20
30
40
50
In reverse order:
50
40
30
20
10
Code
#include<iostream>
7
Computer Systems & Programming
int main ()
int num[6];
cin>>num[i];
cout<<endl<<endl;
cout<<num[i]<<" ";
cout<<endl<<endl;
cout<<"Reversed array."<<endl;
cout<<num[i]<<" ";
8
Computer Systems & Programming
return 0;
Figure 2 Result
3. Write a program to input data into two different arrays and then to add the two arrays and
store the result in the third array.
Code
#include<iostream>
int main()
int array1[30];
int array2[30];
int sum[30];
int n;
9
Computer Systems & Programming
cin >> n;
return 0;
10
Computer Systems & Programming
Rubric 1
02 Is not able to write the code in C++ using module programming and
interpret it. Major help is required in writing the program.
06 Can write and interpret C++ codes using module programming with
minor error help.
11
Computer Systems & Programming
12