C Arrays: - A Collection of Same Type Data, 1D, 2D
C Arrays: - A Collection of Same Type Data, 1D, 2D
www.tenouk.com, © 1/25
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:
www.tenouk.com, © 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;
}
www.tenouk.com, © 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.
www.tenouk.com, © 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.
www.tenouk.com, © 5/25
ARRAYS
One Dimensional Array: Declaration
www.tenouk.com, © 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,
int xNum[20], yNum[50];
float fPrice[10], fYield;
char chLetter[70];
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.
www.tenouk.com, © 8/25
ARRAYS
Array Initialization
www.tenouk.com, © 9/25
ARRAYS
Initialization of an array of type char for holding strings may take the following
form,
For example, the array chVowel in the previous example could have been
written more compactly as follows,
C compiler automatically creates an array which is big enough to hold all the
initializer.
www.tenouk.com, © 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.
www.tenouk.com, © 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.
www.tenouk.com, © 12/25
ARRAYS
Program example 3: Searching the biggest valu
e
. By modifying the previous example we can
search the biggest value.
www.tenouk.com, © 13/25
ARRAYS
Program example 4: Searching the location for th
e given value in an array
www.tenouk.com, © 14/25
ARRAYS
Program example 5: Storing and reading
a string
www.tenouk.com, © 15/25
ARRAYS
Program example 6: Storing and reading array
content and its index
www.tenouk.com, © 16/25
ARRAYS
Two Dimensional/2D Arrays
For examples,
int xInteger[3][4];
float matrixNum[20][25];
The first line declares xInteger as an integer array with 3 rows and
4 columns.
Second line declares a matrixNum as a floating-point array with 20
rows and 25 columns.
www.tenouk.com, © 17/25
ARRAYS
If we assign initial string values for the 2D array it will look
something like the following,
www.tenouk.com, © 18/25
ARRAYS
Take note that for strings the null character (\0) still needed.
From the shaded square area of the figure we can determine the size of the
array.
For an array Name[6][10], the array size is 6 x 10 = 60 and equal to the
number of the colored square. In general, for
array_name[x][y];
ThreeDimArray[2][4][7] = 2 x 4 x 7 = 56.
And if you want to illustrate the 3D array, it could be a cube with wide, long
and height dimensions.
www.tenouk.com, © 19/25
ARRAYS
Program example 7: Storing and reading
array content and its index
www.tenouk.com, © 20/25
ARRAYS
Program example 8: Swapping iIndex (iR
ow) with jIndex (iColumn) in the previous
program example
www.tenouk.com, © 21/25
ARRAYS
1. Program example 9: Strings are read in by the rows.
2. Each row will have one string. Enter the following data:
“you”, “are”, “cat” for the following example.
3. Remember that after each string, a null character is
added.
4. We are reading in strings but printing out only characters.
www.tenouk.com, © 22/25
ARRAYS
The contents of the array in memory after the three
strings are read in the array.
www.tenouk.com, © 23/25
ARRAYS
1. Does your output agree?
2. How is the null character, '\0' printed?
3. Is there a garbage character in a[1][3]? If so,
why?
www.tenouk.com, © 24/25
End-of-C-arrays
www.tenouk.com, © 25/25