0% found this document useful (0 votes)
26 views28 pages

Chapter 5

The document discusses one-dimensional arrays in C programming. It defines arrays as linear data structures that hold a fixed number of elements of the same type. The document outlines how to declare, initialize, access elements from, and perform operations on one-dimensional arrays in C using examples. It also discusses the physical and logical sizes of arrays and covers reading/writing elements to arrays in algorithms and C programs.

Uploaded by

raslen gharssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views28 pages

Chapter 5

The document discusses one-dimensional arrays in C programming. It defines arrays as linear data structures that hold a fixed number of elements of the same type. The document outlines how to declare, initialize, access elements from, and perform operations on one-dimensional arrays in C using examples. It also discusses the physical and logical sizes of arrays and covers reading/writing elements to arrays in algorithms and C programs.

Uploaded by

raslen gharssa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 5:

Arrays

1
Outcomes
By the end of this chapter, the students will:

• Be able to define, declare and fill an array of only one


dimension.

• Be able to define, declare and fill a two dimensional array.

• Know how to read elements from an array and a matrix by


writing an algorithm and a C program.

2
Why we use arrays ?

• What do you propose if you want to store 100 students


marks?

→ Declare 100 variables, one for each mark. X


→ Declare an array of it. ✔

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).

• By analogy with the mathematical concepts of vector and matrix,


arrays types with one and two indices are often called vector type
and matrix type, respectively. 4
• Array type may be identified with other data types such as strings.
1D Arrays Declaration
• A one-dimensional array has a single column of elements
• It is an array having a single index value that represents all the
array’s elements
• The compiler has to know the number of elements of an array
→ its size/length
• The size information corresponds to the number of memory slots to
be booked.
• Declaration in Algorithms
<ArrayName> :Array [1..<Size>] of <DataType> DataType indicates the type o
• Example f the Array values, such as int,
float, etc.
A1: Array [1..100] of integers
ArrayName specifies
• Declaration in a C program the name of the array
< DataType > <ArrayName> [<Size>]; 5
Size indicates the physical size
• Example of the array
int A1 [100];
1D - Arrays representation
int Abc[10] is an array of 10 integers as follows:

• Algorithm: 9th Element

Abc[1] Abc[2] Abc[3] ………. ………. ………. ………. Abc[9] Abc[10]

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

An index can also be an arithmetic expression :

Example : Abc[i+2], Abc[i*2+j]

The ith element of Abc array is denoted Abc[i] :


1st element : Abc[1]
2nd element : Abc[2]
3nd element : Abc[3]

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

Often, not all of the memory of an array is used.


For example, we might have an array with 100 spots while we
are currently only using the first 10 of them.

• The physical size of an array : is the number of slots that are


available .
- Ex: int A[100] means we physically booked 100 memory slots

• The logical size of an array : is the total number of occupied


slots. → it must be less than or equal to the physical size.
9
1D Arrays Declaration –
Example 1
Let’s say you want to declare an array of four floats named “Mark”
• We use the following instruction to declare it:
float Mark[4];
• Here, we declared an array, called Mark, of floating-point type and
size 4
• Meaning, it can hold 4 floating-point values

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};

• It is not necessary to define the size of arrays during initialization


float Mark[]={88.5,64,90.25,100};

• In this case, the compiler determines the size of array by


calculating the number of elements of an array

88.5 64 90.25 100 11


1D Arrays Data Access
• A specific element in an array is accessed by an index
• This is done by placing the index of the element within
square brackets after the name of the array
Example : Mark[1]
Arrays in C programming - Key notes
• Arrays have 0 as the first index not 1
• In this example, Mark[0]
• If the size of an array is n, to access the last element, (n-1)
index is used
• In this example, Mark[3]
12
1D Arrays Data Access –
Example2
Consider the declared array named “myfirstarray” of size 4.
• To fill each element (access it), you can use the following
code:

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 }
S0 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

The Schtroumpf will be as follows :


3 * 4 + 3 * 8 + 3 * 7 + 3 * 12 + 6 * 4 + 6 * 8 + 6 * 7 + 6 * 12 = 279
16
Solution
Algorithm schtroumpf

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

• In two-dimensional array, a single index is not anymore


enough to point an element in the array

• To point an element in a 2D array, two indices are needed :


• The row index (i)
• The column index (j)

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];

ArrayName specifies RowSize indicates the row size


the name of the 2D array ColSize indicates the column size
23
DataType indicates the type o
f the 2D array values, such as
int, float, etc.
2D Arrays Initialization
• In algorithms, we initialize a 2D array as follows:

Mat: Array [2,4] of integers


Mat [2,4] { 10, 11, 12, 13, 14, 15, 16, 17}
• In C programming, there are different ways to manually initialize
a 2D array. Example :

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)

Inner loop j=0 j=1 j=2 j=3

First iteration: i=0

Second iteration: i=1

Last iteration: i=2 27

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

Output: Sum of main diagonal elements : 1+5+9=15


28

You might also like