Chapter7 Arrays
Chapter7 Arrays
Chapter7 Arrays
Chapter 7: Arrays
In this chapter, you will learn about
Introduction to Array Array declaration Array initialization Assigning values to array elements Reading values from array elements Relationship with pointers Passing array to function 2 Dimensional arrays Simple Searching Simple Sorting
NI S1 2009/10 1
Principles of Programming
Introduction to Array
In C, a group of items of the same type can be set up using Array An array is a group of consecutive memory locations related by the fact that they all have the same name and the same type. The compiler must reserve storage (space) for each element/item of a declared array. The size of an array is static (fixed) throughout program execution. To refer to a particular location or element in the array, we specify the name of the array and its index (the position number of the particular element in the array).
Principles of Programming
a[0]
-10
99 -8 100 27
The position number within the square brackets is formally called a subscript. A subscript can be an integer or an integer expression. For example if x = 1 and y = 2, then a[x+y] is equal to a[3].
a[1] a[2]
a[3]
a[4]
a[5] a[6]
10
1976 -2020 1
a[7] a[8]
3
Principles of Programming
Array Declaration
Array declaration is made by specifying the data type, its name and the number of space (size) so that the computer may reserve the appropriate amount of memory. General syntax:
data_type array_name[size];
Examples:
int my_array[100]; char name[20]; double bigval[5*200]; int a[27], b[10], c[76];
Principles of Programming
Array Initialization
There are 2 ways to initialize an array: during compilation and during execution. During compilation:
int arr[ ] = {1, 2, 3, 4, 5};
Unsized array : We can define how many elements that we want since the array size is not given.
Principles of Programming
Using loop and asking the user to specify the value for each element.
int arr[3], index; for (index = 0; index < 3; index++) { printf (arr[%d]:,index); scanf(%d,&arr[index]); }
6
Principles of Programming
The above initialization indicates that there are 3 people living in apartment 0, 2 people living in apartment 1 and so on. Let say that we have a new born in apartment 3, so we need to change the number of inhabitant living in apartment three.
apartment[3] = apartment[3] + 1;
apartment
2
7
Principles of Programming
We can read a value from a specific array element by referring to the index. For example, lets say we want to know how many people leaving in apartment 3, we could simple do this:
int apartment[5] = {3,2,6,4,5}; int no_of_people; no_of_people = apartment[3]; printf(Apartment 3 has %d people,no_of_people);
Principles of Programming
for (index = 0; index < size; index++) { total = total + apartment[index]; } printf("There are total of %d inhabitants",total); return(0);
There are total of 20 inhabitants Press any key to continue
9
Principles of Programming
#include <stdio.h> int main(void) { int apartment[5] = {3,2,6,4,5}; int index, total = 0; printf("%-7s %-15s\n","Apt No", "No of people");
return(0);
}
1
2 3 4
10
2
6 4 5
Principles of Programming
11
Principles of Programming
Say for example we want to write a function, called get_marks, which will read marks from the user and store the marks inside the array.
Function prototype: /* data type with square bracket */ void get_marks(float [ ]); Function call: get_marks(marks); /* just pass the array name */ Function header: void get_marks(float marks[ ])
12
#include <stdio.h> #define size void get_marks(float [ ]); float calc_average(float [ ]);
Principles of Programming
int main(void) { float marks[size] = {0.0}; /*initializing the array get_marks(marks); /* function call */ printf(Average for marks given is %.2f, calc_average(marks)); return(0); } void get_marks(float marks[ ]) /* reading the marks from the users */ { int i; for (i = 0; i < size; i++) { printf("Marks for student %d:",i + 1); scanf("%f",&marks[i]); } } float calc_average(float marks[ ]) { float total = 0.0; int i; for (i = 0; i < size; i++) { total = total + marks[i]; } return (total / size); } Marks for student 1: 56 Marks for student 2: 96 Marks for student 3: 78 Marks for student 4: 35 Marks for student 5: 66 Average for marks given is 66.2 Press any key to continue
13
Principles of Programming
2-Dimensional Array
It is possible to create an array which has more than one dimension. For example:
2D array: int array[4][2]; 3D array: int array[4][2][10];
1 3 5 7
2 4 6 8
Col 1 Col2
14
Principles of Programming
Although it is possible to create a multi-dimensional array, arrays above 2-dimensions are rarely used.
15
Principles of Programming
An array is stored consecutively in memory regardless of the number of dimensions. Therefore, specifying the subscripts in the function parameter will help the compiler to know the boundary of the different dimensions.
16
Principles of Programming
Data Searching
Searching is the process of determining whether an array contains a value that matches a certain key value/search key. The process of finding a particular element of an array is called searching. There are more than one algorithms that can be used to do a search. The most commonly used searching techniques are linear search and binary search. Here, we will discuss how to do searching by using linear search on an array.
17
Principles of Programming
Linear Search
Search key is a data element of the same type as the list elements.
If search key == list element value, the search is said to be successful. Otherwise, it is unsuccessful.
18
Principles of Programming
Principles of Programming
Sorting
Sorting is the process of placing data into a particular order such as ascending or descending. The following example shows the C code for sorting unsorted list to a list sorted in ascending order. Explanation for the working program and the concept behind it will be done during lecture hour (so please attend the class!!!!).
20
Principles of Programming
/*array initialisation by user */ List sorted in ascending order: for (index = 0; index < size; index++) 2 3 6 8 9 { Press any key to continue printf("list[%d]:",index); scanf("%d",&list[index]); } sort(list); /* calling function to sort the array */ printf("\nList sorted in ascending order:\n"); for (index = 0; index < size; index++) { /* printing the array element */ printf("%d\t",list[index]); } return(0); }
21
Principles of Programming
Principles of Programming
Summary
In this chapter, we have looked at:
Array declaration and initialization Reading and writing from/to array elements Passing array to function 2 dimensional array Simple search Array sorting
23
Principles of Programming
Exercise
Assume the following array declaration
float number[5] = {2.3, 4.2, 5.0, 7.9, 6.2};
); ); ); );
24
Principles of Programming
Exercise
Assume the following array declaration
int result[5] = {56, 69, 89}; int i = 2;
a) printf(%d, result[1]); b) printf(%d, result[4]); c) printf(%d, result[0] + result[1]); d) printf(%d %d, i, result[i]);
25
Principles of Programming
Exercise
Assume the following array declaration
int result[3*2];
a) Write C statements that would read the values for the array element from the user. b) Write C statements that would list down all the values in the array. c) Write C statements that would sum up all the values in the array. Rewrite the above task as separate function!!
26