Chapter 5
Chapter 5
Arrays
1
Outcomes
By the end of this chapter, the students will:
2
Why we use arrays ?
3
Arrays definition
• An array is a linear data structure that holds a fixed-size sequential
collection of elements of the same type (homogeneous)
• The size of an array must be a constant value
• Two kinds of arrays are managed :
• The one-dimensional arrays
• The Multidimensional arrays (we are limited in this course to
two-dimensional arrays).
1 2 3 …………… 9 10
Index
9th Element
• In C language:
Abc[0] Abc[1] Abc[2] ……… ……… ……… ……… ……… Abc[8] Abc[9]
. . . . .
6
0 1 2 …………… 8 9
Index
1D- Arrays representation
7
Arrays representation
• Each element of the array is similar to a single variable and can handle
one value at a time.
• Likewise, an array element can :
Algorithm C language
Contain an assignment A[i] 5 A[i] = 5 ;
operation
Appear in an expression x A[i] *2 X= A[i] *2;
Appear in an output Write (" Output ", A[i] ) printf(" Output %d ", A[i] );
instruction
8
Appear in an input Read (A[i]) scanf("%d ",&A[i]);
instruction
Arrays Physical and Logical Sizes
Last index =
First index = 0 Length -1
10
1D Arrays Initialization-
Remarks
• In C programming, arrays can be initialized when they are declared:
float Mark[4]={88.5,64,90.25,100};
13
1D Arrays - Example 3
• A is an array of N integers. Read the N elements of A, compute
their sum and display it.
Algorithm SumArray
#include <stdio.h>
var
main()
N,i,S :Integer
{
A :Array [1..10] of Integer int A[10],N,i,S;
begin printf("Please enter the size of A :\t");
write("Please enter the size of A : ") scanf("%d", &N);
read (N) for (i=0;i<N;i++)
for i from 1 to N do {
write("A[i] : ") printf("A[%d] :\t", i+1);
read (A[i]) scanf("%d",&A[i]);
end for }
S0 S=0;
for i from 1 to N do for (i=0;i<N;i++)
S S + A [i] S+=A[i];
end for
printf("Sum = %d", S); 14
write (S)
}
end
1D Arrays Example 4
A is an array of N integers. Read the N elements of A, count the
number of null elements and display it.
Algorithm SumArray #include <stdio.h>
var main()
N,i,NE :Integer {
A: Array [1..100] of Integer int A[100],N,i,NE;
begin printf("Please enter the size of A :\t");
write("Please enter the size of A : ") scanf("%d", &N);
read (N) for (i=0;i<N;i++)
for i from 1 to N do {
write("A[i] : ") read (A[i]) printf("A[%d] :\t", i+1);
end for scanf("%d",&A[i]);
NE 0 }
for i from 1 to N do NE=0;
if(A[i]=0)
NE NE+1 for (i=0;i<N;i++)
if (A[i]==0)
end if
NE++; 15
end for
printf("Number of null elements : %d", NE);
write ("Number of Null elements : ", NE) }
end
Exercise 1
Having two arrays A1 and A2, write an algorithm that calculate the
schtroumpf of those arrays by multiplying each element of A1 by
each element of A2, then calculate the total sum.
Example:
A1:
3 3 3 3 6 6 6 6
A2:
4 8 7 12 4 8 7 12
Var
i, j, N12, S: integer
T1, T2: array [1..100] of integer
Filling the first array
begin
write("give N12")
read(N12)
for(i=0,i<N12,i++)
write("give the element in T1") Filling the second array
read(T1[i])
endfor
for(i=0,i<N12,i++)
write("give the element in T2")
read(T1[i]) Browsing the two arrays
End for
S←0
for(i=0,i<N12,i++)
S ← S + T1(i) * T2(i)
endfor
17
write( " The schtroumpf is : ", S)
end
#include<stdio.h>
main()
{int T1[100], T2[100];
int N12,i,S=0,j;
printf("give N1 and N2 \n");
scanf("%d",&N12);
for(i=0;i<N12;i++)
{
printf("give the element N° %d in T1 \n",i+1);
scanf("%d",&T1[i]);
}
for(i=0;i<N12;i++)
{printf("give the element N° %d in T2 \n",i+1);
scanf("%d ",&T2[i]);}
S = 0;
for(i=0;i<N12;i++)
{S +=T1[i] * T2[i]; } 18
printf ("The schtroumpf is : %d",S);
}
Exercise 2 (To do)
• Write an algorithm allowing the user to fill an array of a specific
size. The program must then, find the greatest value and its
position (index).
19
Two-Dimensional Arrays (2D)
• 2D arrays are the simplest form of multi-dimensional arrays,
they are generally known as matrices
20
2D Arrays in Algorithms
Row Column
index index Columns
Mat[1][1] 1 2 3 4 Mat[1][4]
2
Rows
Mat[3][3]
3
4 Mat[4][4] 21
Mat[4][1]
2D Arrays in C Programming
Row Column
index index Columns
Mat[0][0] 0 1 2 3 Mat[0][3]
1
Rows
Mat[2][2]
2
3 Mat[3][3] 22
Mat[3][0]
2D Arrays Declaration
Declaration in Algorithms
<MatrixName> :Array [1..<RowSize>, 1..<ColSize> ] of <DataType>
• Example
M1: Array [1..100,1..100] of integers
Declaration in a C program
< DataType > <MatrixName> [<RowSize>] [<ColSize>];
• Example
int M1 [100][100];
int Mat[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
OR 24
int Mat[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
2D Arrays Initialization
Important Notes
• When we give values during one dimensional array declaration,
we don’t need to mention dimension
• That’s not the case with 2D array; you must specify the second
dimension even if you are giving values during the declaration
Example
• /* Valid declaration*/
• int abc[2][2] = {1, 2, 3 ,4 }
• /* Valid declaration*/
• int abc[][2] = {1, 2, 3 ,4 }
• /* Invalid declaration – you must specify second dimension*/
• int abc[][] = {1, 2, 3 ,4 }
• /* Invalid because of the same reason mentioned above*/
• int abc[2][] = {1, 2, 3 ,4 } 25
2D Arrays Data Access
• To access an element in two dimensional, we use two indexes:
• One to access the rows
• The other to access the columns
Example: The following C program initializes a two-dimensional
array and prints each element.
26
2D Arrays Data Access
The first loop (outer loop) is used to browse the lines (3 lines)
The second loop (inner loop) is used to browse the colnnes (4 lines)
Outer loop
Exercise 3
• Write an algorithm to read elements in a matrix and find the sum
of main diagonal (major diagonal). (sum of all elements of main
diagonal of a matrix.
Example:
If the array elements are:
123
456
789