Computer Programming 09
Computer Programming 09
Computer Programming
Experiment No.9
Introduction to Arrays
Prepared for:
Mam Rania Muqadas
By:
Name: Haider Ali
ID: SU91-BBETM-F23-014
Section: Fall
Semester: 2nd
Experiment No.9
Introduction to Arrays
(Rubrics)
A. PSYCHOMOTOR
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained
B. AFFECTIVE
Serial Criteria Allocated Unacceptable Poor Fair Good Excellent Total
No. Marks 0% 25% 50% 75% 100% Obtained
1 Lab Report 2 0 0.5 1 1.5 2
2
Computer Programming Lab 9
CONTENTS OF LAB 9
1 Objectives ............................................................................................................................................. 4
2 Arrays in C++ ....................................................................................................................................... 4
2.1 Declaration of Arrays .................................................................................................................... 4
2.2 Initializing Arrays ........................................................................................................................... 4
2.2.1 Example 2 .............................................................................................................................. 5
2.3 Accessing Array Elements ............................................................................................................. 5
2.3.1 Few Things to Remember: .................................................................................................... 6
2.4 Array Initialization ......................................................................................................................... 6
2.4.1 Example 3 .............................................................................................................................. 6
2.5 Array With Empty Members ......................................................................................................... 6
2.5.1 Example 4 .............................................................................................................................. 6
2.6 Inserting and Printing Array Elements .......................................................................................... 7
2.6.1 Example 5 .............................................................................................................................. 7
2.7 Displaying Array Elements ............................................................................................................ 7
2.7.1 Example 6 .............................................................................................................................. 7
2.8 Take Inputs From User To Store Elements in Array and Printing the Stored Values .................... 8
2.8.1 Example 7 .............................................................................................................................. 8
2.8.2 Example 8 .............................................................................................................................. 9
3 Lab Tasks ............................................................................................................................................ 10
3.1 Task 1 .......................................................................................................................................... 10
3.2 Task 2 .......................................................................................................................................... 10
3.3 Task 3 .......................................................................................................................................... 10
3.4 Task 4 .......................................................................................................................................... 10
4 Home Assignment:.............................................................................................................................. 11
4.1 Task 1 .......................................................................................................................................... 11
4.2 Task 2 .......................................................................................................................................... 11
3
Computer Programming Lab 9
Introduction to Arrays
1 OBJECTIVES
• To get a basic understanding of arrays in C++.
• To understand the basics of 1D arrays.
• To get hands-on experience working with 1D arrays.
• To declare, initialize, and access array elements in C++ programming with the help of examples.
2 ARRAYS IN C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the
same type. An array is used to store a collection of data, but it is often more useful to think of an array as
a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one
array variable such as numbers and use numbers [0], numbers [1], and ..., numbers [99] to represent
individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and
type can be any valid C++ data type.
Example 1
int x [6];
Here,
4
Computer Programming Lab 9
double balance [5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } cannot be larger than the number of elements that we declare
for the array between square brackets [ ].
2.2.1 Example 2
Following is an example to assign a single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if
you write:
You will create exactly the same array as you did in the previous example.
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will
be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called
base index. Following is the pictorial representation of the same array we discussed above −
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.
array[index];
5
Computer Programming Lab 9
2.3.1 Few Things to Remember:
• 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 above 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.
2.4.1 Example 3
For example,
Here, we have not mentioned the size of the array. In such cases, the compiler automatically computes
the size.
2.5.1 Example 4
For example,
6
Computer Programming Lab 9
int x[6] = {19, 10, 8};
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.
2.6.1 Example 5
The code and output is given as follows:
Code
int mark[5] = {19, 10, 8, 17, 9}
2.7.1 Example 6
Code Output
#include <iostream> Value Stored at Index 0 is :1
using namespace std; Value Stored at Index 1 is :2
Value Stored at Index 2 is :3
int main() { Value Stored at Index 3 is :4
int n[5]; //Declaring Array Value Stored at Index 4 is :5
7
Computer Programming Lab 9
return 0;
}
2.8 TAKE INPUTS FROM USER TO STORE ELEMENTS IN ARRAY AND PRINTING THE STORED VALUES
The given example uses for-loop to take input from user.
2.8.1 Example 7
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.
Code Output
#include <iostream> Enter 5 numbers:
using namespace std; 11
12
int main() { 13
int numbers[5]; 14
15
cout << "Enter 5 numbers: " << endl; The numbers are: 11 12 13 14 15
return 0;
}
2.8.2 Example 8
Following program is displaying and printing one dimensional array.
Code Output
#include<iostream> How Many Elements You Want to Store into
using namespace std; an Array?
int main() 6
{ Enter 6 Elements to Store into an Array
int arr[50], num, i; 1
cout<<"\n How Many Elements You Want to Store into an 2
Array? \n"; 3
cin>>num; 4
cout<<"\n Enter "<<num<<" Elements to Store into an 5
Array : \n"; The Elements in the Array are:
for(i=0; i<num; i++) 1 2 3 4 5 6
{
cin>>arr[i];
}
cout<<"\n The Elements in the Array are : \n";
for(i=0; i<num; i++)
{
cout<<arr[i]<<"\t";
}
return 0;
}
9
Computer Programming Lab 9
3 LAB TASKS
3.1 TASK 1
Write a program to check the largest value stored in the given array:
3.2 TASK 2
Write a program to calculate the sum and product of values stored in an array. You have to take input
from the user, then display the stored values, and finally print the sum and product.
3.3 TASK 3
Write a program to calculate the average of the marks using an array. The program should display the
name of the subject before storing the value. The size of the array must be based on five subjects i.e.
English, Programming, Calculus, Computing, and Electronics.
3.4 TASK 4
Write a program to calculate the arithmetic mean of 5 numbers stored in an array.
Note: Arithmetic mean is the sum of a collection of numbers divided by the number of numbers in the collection.
10
Computer Programming Lab 9
4 HOME ASSIGNMENT:
4.1 TASK 1
Writ a program to count even and odd numbers in an array.
4.2 TASK 2
Write a C++ program to reverse an array.
11