0% found this document useful (0 votes)
19 views18 pages

5 Arrays A

An array is a collection of elements of the same data type stored in contiguous memory locations that can be individually accessed via an index. It allows storing multiple values as a single variable without needing separate variables. Elements in an array are accessed using an index with values ranging from 0 to the array size minus one. Arrays can be one-dimensional, storing elements in a single list, or multi-dimensional, arranging elements into multiple dimensions like rows and columns. One-dimensional arrays are commonly used to store student marks or temperature values while two-dimensional arrays arrange these into tables.

Uploaded by

Gul sher Baloch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views18 pages

5 Arrays A

An array is a collection of elements of the same data type stored in contiguous memory locations that can be individually accessed via an index. It allows storing multiple values as a single variable without needing separate variables. Elements in an array are accessed using an index with values ranging from 0 to the array size minus one. Arrays can be one-dimensional, storing elements in a single list, or multi-dimensional, arranging elements into multiple dimensions like rows and columns. One-dimensional arrays are commonly used to store student marks or temperature values while two-dimensional arrays arrange these into tables.

Uploaded by

Gul sher Baloch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Array:

 An array is a sequence of objects of same data type. The objects


are also called elements of array which are placed in adjacent
memory locations that can be individually referenced by adding an
index to a unique identifier.

 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:

There is an array listed in the book of temperature values.


How many temperature values should be there for the
temperature of Lahore city in the one dimensional array?
These will be the elements of this array.
Can you check in your mobile the temperatures of Lahore for
today?
We will use these values for our initial examples.

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

S.No Participant Id: Participant Name: Ob T


1 14001134003 Sanwal Bin Tahir 18 25
2 14001134004 Fahad Qamar 21.5 25
3 14001134005 Hafiz Muhammad Usama Maood 13 25
4 14001134007 Muhammad Dawood 14.5 25
5 14001134008 Muhammad Shahid 17.5 25

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.

Student/Subject PF Materials Algebra


Engineering
Sanwal Bin Tahir 18 25 10
Fahad Qamar 24 23 6
Hafiz Muhammad 19 21 22
Usama Maood
Muhammad Dawood 17 20 23

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

You might also like