You are on page 1of 7

Programming Fundamentals

By Farrukh Toseef
Lecturer CS Riphah International University Fsd

Lecture#25
Arrays
 Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value.
 An array is a group of consecutive memory locations with same name and type.
 Simple variable is a single memory location with a unique name and type.
 But an array is a collection of different adjacent memory locations.
 All these memory locations have one collective name and type.
 The memory locations in the array are known as elements of array.
 The total number of elements in the array is called its length.
 Each element in the array is accessed with reference to its position of location in the array.
 This position is called index or subscript.
 Each element in the array has a unique index. Arrays are used to store a large amount of similar
kind of data.

Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Advantages/Uses of arrays

Some advantages of arrays are as follows:

 Arrays can store a large number of values with single name.


 Arrays are used to process many values easily and quickly.
 The values stored in an array can be stored easily.
 A search process can be applied on arrays easily.
Types of array:

 One dimensional array or 1D array


 Two dimensional array or 2D array
 Multidimensional array

Declaring One-Dimensional arrays

A type of array in which all elements are arranged in the form of a list is known as one-dimensional array.
It is also called single-dimensional array or linear list. It consists of one column or one row. The process
of specifying array name, length and data type is called array declaration.

Syntax

The syntax of declaring one-dimensional array is as follows:

Data_type Identifier [Length];

Example

int marks[5];

The above example declares an integer array marks of five elements. It allocates five consecutive locations
in memory. The index of first element is 0 and index of last element is 4.

Index of each element 0 1 2 3 4


Name of array Marks
Array Initialization

The process of assigning values to array elements at the time of array declaration is called array
initialization. The initialization process provides a list of initial values for array elements. The values are
separated with commas and enclosed with braces. There must be at least one initial value between
braces.

A syntax error occurs if the values in braces are more than the length of array. If the number of initial
values is less than the array size, the remaining array elements are initialized to zero.

The syntax to initialize array is as follows:

Data_type identifier[length] = {list of values};

Example

Int marks[5] = {70, 54, 82, 96, 49}


Index of each element
0 1 2 3 4
70 54 82 96 49
Name of array Marks

1. Array declaration by specifying size

// Array declaration by specifying size


int arr1[10];

2. Array declaration by initializing elements

// Array declaration by initializing elements


int arr[] = { 10, 20, 30, 40 }

// Compiler creates an array of size 4.


// above is same as "int arr[4] = {10, 20, 30, 40}"
3. Array declaration by specifying size and initializing elements
// Array declaration by specifying size and initializing elements.
int arr[6] = { 10, 20, 30, 40 }
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as 0.
// above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"

Accessing individual elements of array

Each individual element of an array is accessed by specifying the following:

 Name of array
 Index of element

Syntax

The syntax of accessing an individual element of an array as follows:

Array_Name[Index];

Example

int marks[5];

marks[0] = 20;

marks[1] = 50;

marks[2] = 70;
marks[3] = 80;

marks[4] = 90;

Accessing Array elements using loops

An easier and faster way of accessing array elements is using loops. The following example shows how
array elements can be accessed using for loop.

int marks[5];

for(int i=0; i<5; i++)

marks[i] = i;

The above example uses counter variable i as an index. In each iteration, the value of i is changed.

Input and output values of an array

The process of input and output with arrays is similar to the input and output with simple variables. The
cin object is used to input values in the arrays. The cout object is used to display values of arrays. However,
the use of these objects with each individual element becomes very time-consuming because each
element is treated as a simple variable.

Loops are frequently used to input and output data in an array. The use of loops with arrays makes
the process of input and output faster and easier. It also reduces the code written for this purpose.

1. write a program that inputs five integers from the user and stores them in an array. It then
displays all values in the array without using loops.
#include<iostream>
using namespace std;
int main( )
{
int arr[5];
cout<<”Enter five integers:”<<endl;
cin>>arr[0];
cin>>arr[1];
cin>>arr[2];
cin>>arr[3];
cin>>arr[4];
cout<<”The values in array are:\n”;
cout <<arr[0]<<endl;
cout <<arr[1]<<endl;
cout <<arr[2]<<endl;
cout <<arr[3]<<endl;
cout <<arr[4]<<endl;
}
2. write a program that inputs five integers from the user and stores them in an array. It then
displays all values in the array using loops.
#include<iostream>
Output
using namespace std;
int main( ) Enter an integer: 96
{
int arr[5], i; Enter an integer: 72
for(i=0; i<5; i++) Enter an integer: 41
{
cout<<”Enter an integer:”; Enter an integer: 39
cin>>arr[i]; Enter an integer: 68
}
cout<<”The values in array are:\n”; The values in array are:
for(i=0; i<5; i++)
96 72 41 39 68
cout<<arr[i];
}
3. Write a program that inputs five values from the user, stores them in an array and displays the
sum and average of these values.
#include<iostream> Output
using namespace std;
int main( ) Enter value: 3
{
Enter value: 7
int arr[5], i, sum = 0;
float avg = 0.0; Enter value: 5
for(i=0; i<5; i++)
Enter value: 9
{
cout<<”Enter value:”; Enter value: 6
cin>>arr[i];
sum = sum + arr[i]; Sum is 30
} Average is 6
avg = sum/5.0;
cout<<”Sum is”<<sum<<endl;
cout<<”Average is”<<avg;
}
4. Write a program that inputs the age of different persons and counts the number of persons in
the age group 50 and 60.
#include<iostream>
using namespace std;
int main( ) Output
{
int age[150], i, n, count=0; Enter the number of persons required 3
cout<<”Enter the number of persons required”;
Enter ages of 3 persons.
cin>>n;
cout<<”Enter ages of”<<n<<”persons.”<<endl; 51 55 20
for(i=0; i<n; i++)
2 persons are between 50 and 60.
{
cin>>age[i];
if(age[i]>=50 && age[i]<=60)
count=count+1;
}
Cout<<count<<”persons are between 50 and 60.”;
}
5. Write a program that inputs ten numbers from the user in an array and displays the maximum
number.
#include<iostream> Output
using namespace std;
int main( ) Enter value:11
{
Enter value:82
int arr[10], i, max;
for(i=0; i<10; i++) Enter value:73
{ Enter value:65
cout<<”Enter value:”;
cin>>arr[i]; Enter value:97
} Enter value:55
max = arr[0];
for(i=0; i<10; i++) Enter value:48
if(max<arr[i]) Enter value:96
max=arr[i];
cout<<”Maximum value:”<<max; Enter value:24
} Enter value:37
Maximum value: 97

6. Write a program that inputs ten numbers from the user in array and displays the minimum
number.
#include<iostream> Output
using namespace std;
int main( ) Enter value:11
{ Enter value:82
int arr[10], i, min;
for(i=10; i<10; i++) Enter value:73
{ Enter value:65
cout<<”Enter value:”;
cin>>arr[i]; Enter value:97
} Enter value:55
min = arr[0];
for(i=0; i<10; i++) Enter value:48
if(min>arr[i]) Enter value:96
min=arr[i];
Enter value:24
Enter value:37
Minimum value: 11
cout<<”Minimum value:”<<min;
}

7. Write a program that inputs five numbers in an array and displays them in actual and reverse
order.
#include<iostream> Output
using namespace std;
int main( ) Enter an integer:35
{ Enter an integer:35
int num[5], i;
for(i=0; i<5; i++) Enter an integer:35
{ Enter an integer:35
cout<<”Enter an integer:”;
cin>>num[i]; Enter an integer:35
} The array in actual order:
cout<<”\n The array in actual order:\n;
for(i=0; i<5; i++) 35 47 83 66 72
cout<<num[i]<<” “; The array in reverse order:
cout<<”\n The array in reverse order:\n”;
for(i=4; i>=0, i--) 72 66 83 47 35
cout<<num[i]<<” “;
}

You might also like