Chapter Arrays-1
Chapter Arrays-1
R
1 Introduction
Array
LU
An array is a collection of data of the same data type stored in contiguous (adjacent) memory locations
For example,
2 5 8
U
shows an array of integers. This is because they are three integers stored in adjacent memory locations.
G
Declaring an Array
• Size of the array (how many elements can the array store)
• One-dimensional (1D) array - This array has one raw, and one or several columns. The array
shown above is a 1D array
R
• Two-Dimensional (2D) array - This array has several rows and one or several columns.
D
2 5 8
7 10 12
Dr. Raphael Angulu, BCS 111 SCI, MMUST, 2020: Arrays Notes 1
while
2
7
12
19
is also a 2D array made up of 4 rows and one column. So, we can say a 2D array is made up of more than
1 row.
R
For instance, to declare an array called data that can store 5 integers we write the line
1 int data [5];
when this line is executed, it reserves 5 adjacent memory cells, each cells being big enough to store an integer.
LU
Each cell in the array has an index, with the left-most cell having index 0 and right-most cell having
index s-1 where s is the size of the array.
For instance, the array data declared above could be thought of looking like
U
data
0 1 2 3 4
G
in memory. Notice the following
• Each cell has a name, which consist of arrayName (for our example its data) and the respective cell
index enclosed in squire brackets. For instance
.A
1 data[2] = 23;
stores the value 23 into the third cell of the array data.
D
You could read the value from the keyboard and store it into the fourth cell using the line
1 scanf(”%d”, &data[3]);
You can also declare and assign values to an array using the syntax
1 dataType ArrayName[size] = {val1, val2, val3...valN};
For example, we could declare and assign values to our array data as
1 int data[5] = {10, 54, 19, 400, 234};
Dr. Raphael Angulu, BCS 111 SCI, MMUST, 2020: Arrays Notes 2
1.1.1 Looping through an array
As we mentioned, each cell in an array has an index. The array indices starts from 0 to s-1, where s is the
array size. Therefore, it naturally follows that, we can use a loop to comfortably walk over an array.
Consider the code below that declares an array of integers of size 5, uses a loop to insert values into
the array
1 int data [5]; //Reserve 5 adjacent cells that can store integers , call the reserved block data
2 int index = 0; //start from the first index, 0
3 while ( index < 5) //loop upto the last index. Remember last index is always one less size of array
4 {
5 scanf(”%d”, &data[index]); //read an integer and store in the current cell (indexed)
6 index++; //move to the next cell (by incrementing the index)
7 }
R
1 for ( int index = 0; index < 5; index++)
2 {
3 scanf(”%d”, &data[index]); //read an integer and store in the current cell (indexed)
4 }
LU
You could follow the same procedure if you want to display (or process) the values in the array. Consider
the loop below which walks over the array and display values stored in each cell
1 for ( int index = 0; index < 5; index++)
2 {
3 printf (”%d\t”, data[index]); //display the value at the current index of array data
4 }
U
consider the code below that calculates the sum and average of values stored in an array data
1 int sum = 0;
G
2 float average = 0.0;
3 for ( int index = 0; index < 5; index++)
4 {
5 sum += data[index];
N
6 }
7 average = sum / 5.0;
.A
R
D
Dr. Raphael Angulu, BCS 111 SCI, MMUST, 2020: Arrays Notes 3
Example:
Given a decimal number, you can calculate its binary representation by repetitively dividing the number by
2, and keeping the remainder from every division, until the number becomes zero. The binary representation
is the reverse of the remainders. Program the algorithm described above such that it can convert decimal
numbers between 0 and 255 into their binary representation (Hint, binary will need 8 bits). Your program
should read an integer from the user and convert it into its binary representation. Display the number and
its binary equivalent. If the user enters a number like 29, your program should display Decimal = 29
Binary = 00011101.
1 #include<stdio.h>
2 int main()
3 {
4 int binary[8] = {0, 0, 0, 0, 0, 0, 0, 0};
5 int decimal;
6 printf (”Enter a decimal number: ”);
7 scanf(”%d”, &decimal);
R
8 if (decimal >= 0 && decimal <= 255)
9 {
10 for ( int i = 7; i >= 0; i−−)
11 {
12 binary[ i ] = decimal % 2;
LU
13 decimal = decimal / 2;
14 if (decimal == 0)
15 break;
16 }
17 printf (”Binary = ” );
18 for ( int x = 0; x < 8; x++)
19 {
U
20 printf (”%d”, binary[x]);
21 }
22 printf (”\n”);
23 }
G
24 else
25 {
26 printf (”Enter a number between 0 and 255”);
}
N
27
28 }
.A
• Number of rows
D
• Number of columns
declares an array called marks which can store 10 integers (2 × 5) arranged in 2 rows each having 5 columns.
Dr. Raphael Angulu, BCS 111 SCI, MMUST, 2020: Arrays Notes 4
A cell index consist of two indices, row index and column index. Row indices starts from 0 to r-1, where r
is the number of rows while column indices also start from 0 to c-1, where c is the number of columns.
For the array marks declared above, row indices and 0,1 while column indices are 0,1,2,3,4.
0 1 2 3 4
0
1 47 52
Therefore
• the name of the first cell (top-left cell) is marks[0][0]
R
• to store the value 52 in 4th cell of row 2 you write marks[1][3] = 52
You can declare and initialize the 2D array above as
1 int marks[2][5] = {{1, 2, 543, 344, 57},{160, 101, 172, 163, 142}};
LU
We can use loops to walk over a 2D array. We need two loops, one for the rows and the other for the column.
For instance, the code below reads values from the keyboard and insert them into the array marks de-
clared as above
1 int marks [2][5]; //reserve 10 adjacent cells (2 rows of 5 columns each)
U
2 for ( int rows = 0; rows < 2; rows++) //walk over rows
3 {
4 for ( int cols = 0; cols < 5; cols++) //for every column in the current row
5 {
G
6 scanf(”%d”, &marks[rows][cols]); //read an integer and put it in the current cell
7 }
8 }
N
You could access (process) the array using a similar loop. Consider the loop below which displays the values
in the array marks
1 for ( int rows = 0; rows < 2; rows++) //walk over rows
.A
2 {
3 for ( int cols = 0; cols < 5; cols++) //for every column in the current row
4 {
5 printf (”%d\t”, marks[rows][cols]) ; //display an integer stored in the current cell
6 }
7 printf (”\n”); //when done with a row, move the cursor to the next line
}
R
Assume each row represent a student and each column represent a mark a student got in a particular subject
(for this case, its 2 students 5 subjects). Calculate and display sum and average for each student (row). The
D
Dr. Raphael Angulu, BCS 111 SCI, MMUST, 2020: Arrays Notes 5