CSE 109 Lec 9 Array Basic
CSE 109 Lec 9 Array Basic
1
Lec 9: Array in C
PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
INTRODUCTION
An array is a collection of elements of the same type that are referenced by a
common name. An individual variable in the array is called an array element.
All the elements of an array occupy a set of contiguous memory locations.
Arrays allow programmers to group related items of the same data type in one
variable.
However, when referring to an array, one has to specify not only the array or
variable name but also the index number of interest.
Example:
int var[10];
float venus[20];
Books
double earth[5];
char pluto[7];
Can you imagine how long we have to write the declaration part by using normal variable
declaration?
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …, studMark998,
stuMark999, studMark1000;
…
…
return 0;
}
Given a list of test scores, determine the max, min, average, sum
Array count (Ex: Given the height measurements of students in a class, output
the names of those students who are taller than average.)
Array reverse (temp array, in-place)
Frequency of digits in a number
Read in a list of student names and rearrange them in alphabetical order
(sorting).
Searching a particular item from a given list.
All the consecutive array elements are at a distance of 4 bytes from each other as an int block
occupies 4 bytes of memory in the system (64-bit Architecture). Also, each array element contains
a garbage value because we have not initialized the array yet.
arrayName[indexExp]
where indexExp, called index, is any expression whose value is a nonnegative integer
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
Ar[3] = 1;
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]
11/20/2023 18