Fundamentals of
Programming
Lecture 10
C h a m i l a K a r u n a t i l a ke
Department of ICT
Fa c u l t y o f Te c h n o l o g y
U n i v e rs i t y o f S r i J ay e w a r d e n e p u ra
itchamila@gmail.com
Arrays
2
Arrays in C
An array is a data structure that can store a sequential
collection of elements of the same type.
All arrays consist of consecutive memory locations.
Array can be thought of as a collection of variables of the same
type.
10 81 34 8 95 13 19 56 75
H E L L O !
3
Array Declaration
In normal variable declaration, only the data type and the variable
name is sufficient.
When declaring an array, there is one more addition to that. The
size of the array has to be mentioned with in brackets.
Syntax:
<type> <arrayName>[<array_size>];
int numbers[10];
The array size indicates the number of elements in the array.
Once the array is declared, the size cannot be changed.
4
Array Declaration
When an array is declared, a collection of memory locations are
reserved and labeled with the name of the array.
int numbers[10];
numbers
At this point, no values are assigned to the elements.
However, the array elements may contain garbage values.
5
Array Element Identification
Normal variable can be identified using its name.
Via name, read and write operations can be done to the variable.
int number;
number = 10;
printf(“%d”, number);
int i = number;
number++;
How this can be achieved with the array elements?
6
Array Element Identification
In an array, index is used when working with individual
elements.
Array Index is a sequential number which indicates the
position of a particular element.
Index starts with 0. First element of the array is indexed with 0.
The last element’s index become the size of the array - 1.
0 1 2 3 4 5 6 7 8
Size = 9
7
Array Element Identification
Element identification can be achieved using name and index
together(indexing the name).
<arrayName>[<index>]
0 1 2 3 4 5 6 7 8
numbers
If we want to deal with the fourth element(index 3) of the above
array, following syntax can be used.
numbers[3]
8
Array Initialization
Array initialization is the process of assigning values to the array
elements.
There are two initialization methods:
Initialize one element at a time
Initialize whole array at once
9
Array Initialization
Elements can be initialize one at a time using name of the
array and the index of the element.
e.g. numbers[0] = 12;
numbers[1] = 34;
…………………..
0 1 2 3 4 5 6 7 8
numbers 12 34
10
Array Initialization
Whole array can be initialized at once when array is
declared.
e.g. int numbers[10] = {12,34,13,56,78,72,90,11,80,67} ;
0 1 2 3 4 5 6 7 8 9
numbers 12 34 13 56 78 72 90 11 80 67
When the array is initialized in this way, the size declaration
is optional. You can declare the array with empty brackets
without size inside(un-sized).
int numbers[] = {12,34,13,56,78,72,90,11,80,67} ;
11
Array Initialization
There are several variations and special cases of initializing
whole array at once.
int numbers[10] = {0} ;
0 1 2 3 4 5 6 7 8 9
numbers 0 0 0 0 0 0 0 0 0 0
int numbers[10] = {} ;
0 1 2 3 4 5 6 7 8 9
numbers 0 0 0 0 0 0 0 0 0 0
12
Array Initialization
int numbers[10] = {1} ;
0 1 2 3 4 5 6 7 8 9
numbers 1 0 0 0 0 0 0 0 0 0
int numbers[10] = {12,34,5,26} ;
0 1 2 3 4 5 6 7 8 9
numbers 12 34 5 26 0 0 0 0 0 0
13
Accessing Array Elements
An element of an array can be accessed by indexing the
array name.
int myNumber = numbers[3];
int sum = numbers[3] + numbers[4];
printf(“%d”, numbers[3] );
14
#include <stdio.h>
int main()
{
int numbers[4] = {12,34,13,56} ;
printf("%d\n",numbers[0]);
printf("%d\n",numbers[1]);
printf("%d\n",numbers[2]);
printf("%d\n",numbers[3]);
float average = (numbers[0] + numbers[1] +
numbers[2] + numbers[4])/4.0 ;
printf("%.2f\n",average);
return 0;
}
15
Accessing Array Elements using loops
Loops are used frequently when working with arrays.
When printing the elements of the whole array, it could be done
using for loop.
for(int i=0;i<10;i++){
printf("%d\n",numbers[i]);
}
When initializing an array with values with a sequential patterns,
for loop can be used.
for(int i=0;i<10;i++){ for(int i=0;i<10;i++){
numbers[i] = 100 + i; numbers[i] = 10 * i;
} }
16
#include <stdio.h>
int main()
{
int numbers[10];
for(int i=0;i<10;i++)
{
numbers[i] = 12 * (i+1);
}
for(int i=0;i<10;i++)
{
printf("%d\n",numbers[i]);
}
return 0;
}
17
Passing Arrays as Function Arguments
As any other variables, an array can be passed as an
argument in a function.
Formal parameter can be declared as a sized or an un-
sized array.
void myFunction(int param[10]) {
.....
}
void myFunction(int param[]) {
.....
}
18
#include <stdio.h>
double getSum(int arr[], int size);
int main ()
{
int numbers[5] = {1000, 2, 3, 17, 50};
double result;
result = getSum(numbers, 5 ) ;
printf( "Average value is: %.2f ", result);
return 0;
}
19
double getSum(int arr[], int size)
{
double sum = 0;
for (int i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum;
}
20
Questions?
21
Lab Exercise 9
1. a) Write a C program to store exam marks of one subject for
10 students in an array. Write a separate function to
calculate the average of the marks. Print the average.
b) Extend the above program to get the marks as user
inputs(with scanf()). Then display the array values at the
end. Then calculate the average and print it.
22
Lab Exercise 9
Example:
Enter marks of student 1 : 69
Enter marks of student 2 : 78
……..
Student Marks : 69,78,…….
The average of the student marks is = 62.23
23
Lab Exercise 9
2. Write a C program to assist a shop cashier counter to
generate the invoice of the items that the customer bought.
a) First, the program should get the user inputs, amount and
unit price for each item (You can use fixed number of items for
instance, 5). Store these values in two arrays.
b) Then calculate the amount for each item and store it in a
different array. ( item amount = number of items * unit price)
c) Calculate the total amount by adding amounts of all items.
d) Display the invoice.
24
Lab Exercise 9
Example:
Enter unit price : 40
Enter number of Items : 10
Enter unit price : 129
Enter number of Items : 3
………………
………………
25
Lab Exercise 9
Unit Price Number Of Items Amount
----------------- ----------------------------- ---------------
40.00 10 400.00
129.00 3 387.00
………… … ……….
………… … ……….
Total 1452.00
26