UNIVERSITY OF CONSTANTINE 1 - CAMPUS
CHAAB ERSAS
G 1 YEAR COMMON CORE - ST
ST
Initiation à la programmation
Lab #07 : The one-dimensional arrays
(Vectors)
Part I : Algorithms
Exercise No. 01
Write an algorithm that calculates the average of values in an array of 30 integer elements.
Exercise No. 02
Write an algorithm that calculates the number of positive values and the sum of even values
in an array of 20 elements.
Exercise No. 03
Write an algorithm that finds the maximum and minimum values of an array of 20 real
numbers.
Exercise No. 04
Write an algorithm that displays the number of occurrences of a given number 'X' in an array
of 50 real elements.
Exercise No. 05 (additional)
Write a program that sorts an array of 25 real elements.
Part II : Programming in C
Let T be an array of 10 integer elements. Write a C program that determines the positions of
the smallest and largest elements in this array.
UNIVERSITY OF CONSTANTINE 1– CAMPUS CHAAB ERSAS1 ST YEAR COMMON CORE - ST
Initiation à la programmation - Correction of Exercise Series No. 07 : Vectors.
Part I : Algorithms
Exercise No. 01 Exercise N°03
Algorithm Avr30Val; Algorithm MinMax;
Constants n=30; Constants n=20;
Variables Variables
Vect: array [1..n] of integer; T: array [1..n] of real;
Sum, i: integer; Avr: real; i: integer; max,min: real;
Begin Begin
For i=1 to n Do For i=1 to n Do
Read (Vect[i]); Read (T[i]);
EndFor EndFor
Sum 0; max T[1]; min T[1];
For i=1 to n Do For i=2 to n Do
Sum Sum+ Vect[i]; If (T[i] > max) Then
EndFor max T[i];
EndIf
Avr Sum/n; If (T [i] < min) Then
Write ('Average = ', Avr) ; min T[i];
End. EndIf
EndFor
Exercise N°02 Write('Min=',min,' Max=', max);
End.
Algorithm calculate;
Constants n=20;
Variables Exercise N°04
V: array [1..n] of integer;
i, sev, npv: integer; Algorithm Occurrence;
//npv : number of positive values Constants n=50;
//sev : sum of even values Variables
Vect: array [1..n] of real;
Begin i, co: integer; X: real;
For i=1 to n Do
Read (V[i]); Begin
EndFor For i=1 to n Do
Read (Vect[i]);
nvp 0; sev 0; EndFor
Read (X);
For i=1 to n Do
If (V[i] >= 0) Then co 0;
npv npv + 1; For i=1 to n Do
EndIf If (Vect[i] = X) Then
co co + 1;
If (V[i] mod 2 = 0) Then EndIf
sev sev + V[i]; EndFor
EndIf
EndFor Write ('The number of occurrences
of ',X,' is: ', co);
Write('The number of positive End.
values is: ', npv);
Write('The sum of even values
is: ', sev);
End.
UNIVERSITY OF CONSTANTINE 1– CAMPUS CHAAB ERSAS 1ST YEAR COMMON CORE - ST
Module: Informatique II - Corrigé Série d’exercices n°06: Les vecteurs.
Exercise N°05
#include <stdio.h>
#define N 25
main()
{
float V[N];
int i, j; float T;
printf("Enter 50 real numbers: \n");
for (i=0; i<N ; i++)
scanf("%f", &V[i]);
for (i=0; i<N-1 ; i++)
for (j=i+1; j<N ; j++)
if (V[i] > V[j])
{
T = V[i];
V[i] = V[j];
V[j] = T;
}
for (i=0; i<N ; i++)
printf("%f\n", V[i]);
}
Part II : Programming in C
#include <stdio.h>
#define N 5
main()
{
int T[N];
int i, max, min, Pmax, Pmin;
printf("Enter 5 integer values: \n");
for (i=0; i<N ; i++)
scanf("%i", &T[i]);
max = T[0]; min = T[0];
Pmax = 0; Pmin = 0;
for (i=1; i<N ; i++)
{
if (T[i] > max)
{
max = T[i];
Pmax = i;
}
if (T[i] < min)
{
min = T[i];
Pmin = i;
}
}
printf("Min= %i\t Position of Min= %i\n", min, Pmin);
printf("Max= %i\t Position of Max= %i\n", max, Pmax);
}
UNIVERSITY OF CONSTANTINE 1 - CAMPUS
CHAAB ERSAS
G 1 YEAR COMMON CORE - ST
ST
Initiation à la programmation
Lab #08 : The two-dimensional arrays
(Matrices)
Part I : Algorithms
Exercise No. 01
Write an algorithm that calculates the sum of two matrices with 10 rows and 15 columns of
integer elements.
Exercise No. 02
Write an algorithm that determines the maximum value along with its indices in a matrix
with 5 rows and 5 columns of integer elements.
Exercise No. 03
Write an algorithm that calculates, for each row, the sum of even values, and for each
column, the count of odd values in a matrix with 4 rows and 8 columns of integer elements.
Exercise No. 04 (additional)
Given a matrix with 20 rows and 20 columns of real elements.
Write an algorithm to determine the row number with the maximum product of its
elements.
Translate this algorithm into the C language.
Part II : Programming in C
A une matrice d’ordre (NxM) de nombres entiers. Ecrire un programme en langage C qui
permet de:
1) Read matrix A
2) Calculate the average of all elements of matrix A
3) Calculate the number of matrix elements that are greater than this average.
UNIVERSITY OF CONSTANTINE 1– CAMPUS CHAAB ERSAS1 ST YEAR COMMON CORE - ST
Initiation à la programmation - Correction of Exercise Series No. 08 : Matrices.
Part I : Algorithms
Exercise No. 01 Exercise No. 03
Algorithm Sum2Mat; Algorithm SEV_COV;
Constants n=10; m=15; Constants n=4; m=8;
Variables Variables
A,B,S: matrix[1..n,1..m] of integer; Mat: matrix[1..n,1..m]of integer;
i, j : integer; i,j,S,Co : integer;
Begin
For i=1 to n Do Begin
For j=1 to m Do For i=1 to n Do
Read (A[i,j]); For j=1 to m Do
EndFor Read (Mat[i,j]);
EndFor EndFor
For i=1 to n Do EndFor
For j=1 to m Do
Read (B[i,j]); For i=1 to n Do
EndFor S0;
EndFor For j=1 to m Do
For i=1 to n Do If (Mat[i,j]mod2 = 0) Then
For j=1 to m Do S S + Mat[i,j];
S[i,j] A[i,j]+B[i,j]; EndIf
EndFor EndFor
EndFor Write('Row:',i,'Sum:',S);
EndFor
For i=1 to n Do
For j=1 to m Do
For j=1 to m Do
Write (S[i,j]);
EndFor Co 0;
EndFor For i=1 to n Do
End. If (Mat[i,j]mod2 <> 0) Then
Co Co + 1;
Exercise No. 02 EndIf
Algorithm MaxIndices; EndFor
Constants n=5; m=5; Write('Column:',j,'Count:',Co);
Variables EndFor
Mat: matrix[1..n,1..m]d’integer; End.
i,j, Max,iMax,jMax: integer;
Begin
For i=1 to n Do
For j=1 to m Do
Read (Mat[i,j]);
EndFor
EndFor
Max Mat[1,1];
iMax 1; jMax 1;
For i=1 to n Do
For j=1 to m Do
If (Mat[i,j]>Max) Then
Max Mat[i,j];
iMax i; jMax j;
EndIf
EndFor
EndFor
Write ('Maximum value =', Max);
Write ('Indices =',iMax, jMax);
End.
UNIVERSITY OF CONSTANTINE 1– CAMPUS CHAAB ERSAS1 ST YEAR COMMON CORE - ST
Computer Science II - Correction of Exercise Series No. 08: Matrices.
Exercise No. 04 Part II : Programming in C
Algorithm Calculate; #include <stdio.h>
Constants n=20; #define N 3
Variables #define M 4
X: matrix[1..n,1..n]of real; main()
i,j,row : integer; {
Max,prod : real; typedef int A_Type[N][M];
Begin A_Type A;
For i=1 to n Do int i, j, sum, co; float avr;
For j=1 to n Do
printf("Enter the elements of
Read (X[i,j]);
EndFor the matrix : \n");
EndFor for (i=0; i<N ; i++)
for (j=0; j<M ; j++)
scanf("%i", &A[i][j]);
Prod 1;
For j=1 to n Do sum = 0;
Prod Prod * X[1,j]; for (i=0; i<N ; i++)
EndFor for (j=0; j<M ; j++)
sum = sum+ A[i][j];
row 1; avr = (float)sum/(N*M);
Max Prod;
co = 0;
For i=2 to n Do for (i=0; i<N ; i++)
Prod 1; for (j=0; j<M ; j++)
For j=1 to n Do if (A[i][j] > avr)
Prod Prod * X[i,j];
co++;
EndFor
printf("The number of elements
If (Prod > Max) Then greater than %f is: %i", avr, co);
row i; }
Max Prod;
EndIf
EndFor
Write('The row number with the
maximum product is : ', row);
End.