Lec 9 Programming Fundamentals (2)
Lec 9 Programming Fundamentals (2)
Lecture 09
Week 07
Ms. Noor-ul-Huda
Senior Lecturer-I
Department of Computer Science
College of Computer Science and Information Systems
[email protected]
ARRAYS
An array is a collection of elements of the same type that
are referenced by a common name.
Compared to the basic data type (int, float & char) it
is an aggregate or derived data type.
All the elements of an array occupy a set of contiguous
memory locations.
Why need to use array type?
Consider the following issue:
2/25
ARRAYS
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;
}
3/25
ARRAYS
By using an array, we just declare like this,
int studMark[1000];
This will reserve 1000 contiguous memory locations for storing the students’
marks.
Graphically, this can be depicted as in the following figure.
4/25
ARRAYS
This absolutely has simplified our declaration of the variables.
We can use index or subscript to identify each element or location in the
memory.
Hence, if we have an index of jIndex, studMark[jIndex] would refer to the
jIndexth element in the array of studMark.
For example, studMark[0] will refer to the first element of the array.
Thus by changing the value of jIndex, we could refer to any element in the
array.
So, array has simplified our declaration and of course, manipulation of the
data.
5/25
ARRAYS
One Dimensional Array: Declaration
6/25
ARRAYS
For example, to declare an array of 30 characters, that construct a people
name, we could declare,
char cName[30];
Which can be depicted as follows,
7/25
ARRAYS
Examples of the one-dimensional array declarations,
The first example declares two arrays named xNum and yNum of type int.
Array xNum can store up to 20 integer numbers while yNum can store up to 50 numbers.
The second line declares the array fPrice of type float. It can store up to 10 floating-point
values.
fYield is basic variable which shows array type can be declared together with basic type
provided the type is similar.
The third line declares the array chLetter of type char. It can store a string up to 69
characters.
Why 69 instead of 70? Remember, a string has a null terminating character (\0) at the
end, so we must reserve for it.
8/25
ARRAYS
Array Initialization
9/25
ARRAYS
Initialization of an array of type char for holding strings may take the following
form,
10/25
ARRAYS
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.
Program example 1: Sum of array’s element.
Notice the array's element which is not initialized is set to 0
automatically.
11/25
ARRAYS
Program example 2: Searching the smallest value.
Finding the smallest element in the array named fSmallest.
First, it assumes that the smallest value is in fSmallest[0] and
assigns it to the variable nSmall.
Then it compares nSmall with the rest of the values in fSmallest,
one at a time.
When an element is smaller than the current value contained in
nSmall, it is assigned to nSmall. The process finally places the
smallest array element in nSmall.
12/25
ARRAYS
Program example 3: Searching the biggest value.
By modifying the previous example we can search the
biggest value.
13/25
ARRAYS
Program 4: Searching the location for the given
value in an array
14/25