5 Arrays A
5 Arrays A
That means that, for example, five values of type int can be
declared as an array without having to declare 5 different variables
(each with its own identifier). Instead, using an array, the five int
values are stored in adjacent memory locations, and all five can be
accessed using the same identifier, with the proper index.
1
Array:
Consider the marks you obtain of each course per semester.
Marks constitute the array type and the number obtained by
each student is the elements of that array.
The position of an element in an array is represented by an index
value or subscript. This can be either the role number of a student
or serial number on the attendance sheets.
E.g. Humaira is the first student so either her name or roll number
can be the first element of the array and the index value will be
the first serial number.
In an array of ‘n’ elements, the index values are 0,1,….., n-1.
2
What is Array?
Here 0 represents the index value of first element and n-1
represents the index value of last element.
Which in the case of CS-211, ‘n’ is 12, 0 is Amir (or his
roll number) and n-1(i.e, 11) is Zain.
Array is used to store/process a large amount of data.
There are 2 types of array.
One dimensional
Two dimensional
3
One Dimensional:
It is also known as list array and can have either
one row or one column.
E.g. marks obtained by Amir in different subjects
or marks obtained by all the students in PF.
4
One Dimensional:
5
One Dimensional
An array can be defined by using the following syntax.
type array_name [n]
Here n is unsigned integer value which can represent
the
total number of elements in the array.
What will following do?
double temp_Lahore [24]
6
Example 4-01
S.No Participant Id: Temp
1 Temp[0] 38
2 Temp[1] 37
3 Temp[2] 36
4 Temp[3] 36
5 Temp[4] 35
.
.
.
24 Temp[23] 37
7
Example 4-01 (Temperature of Lahore)
{
int i; float a[5];
a[0]=38;
a[1]=37;
a[2]=36;
a[3]=36;
a[4]=35;
for(i=0; i<=4; i+
+)
cout <<
“temperature in
a["<<i<<"] =
"<<a[i]<<endl;
return 0; 8
Example 4-01 (cout)
9
Example 4-01
1
0
Two Dimensional
Multidimensional arrays can be described as "arrays of arrays". For
example, a bidimensional array can be imagined as a two-dimensional
table made of elements, all of them of a same uniform data type.
Muhammad Shahid 14 11 22
11
Example 4-01 (Marks obtained)
{
int i;
float
a[5];
a[0]=18;
a[1]=21.
5;
a[2]=13;
a[3]=14.
5;
a[4]=17.
5;
for(i=0;
i<=4; i+ 1
2
Example 4-01 (cout)
10
1
3
One Dimensional:
Here a is the array name for marks obtained by students in the
class quiz (for example).
[5] is the number of elements, which in our case is 5
students.
a[0] is the first element of the array or the first student on our
list as shown before. Here it is Sanwal. ‘18’ is the value
assigned to a[0] which are the marks obtained by Sanwal.
Similar is the case with other elements/students in the
array.
We have used the for loop to show the array or list on
the
screen. 14
Example 4-02 (doing cin of the marks)
double abc[5];
int i;
for(i=0; i<=4; i++)
{
cout<<"Enter the value in element "<<i<<endl;
cin>>abc[i];
}
cout<<"the values of the array are"<<endl<<endl;
for(i=0; i<=4; i++)
cout<<"the value in a["<<i<<"] is "<<abc[i]<<endl;
cout << "and in the descending order it is!" <<
endl<<endl; for(i=4; i>=1; i--)
cout<<"the value in a["<<i<<"] is "<<abc[i]<<endl;
cout<<"program ends"<<endl;
15
Example 4-01 (cout)
16
One Dimensional
We did this example with values used in the first
example, though this time we input the values ourselves
and in the process, also showed them in descending
order of the index values.
The first loop was used to cin values for each element.
The values for each element were saved corresponding to
its index value. Like for the first element abc[0] it was 18
and for the last element abc[4] it was 17.5.
abc[0] for the marks obtained by Sanwal and abc[5] by
the marks obtained by Shahid.
17
1
8