0% found this document useful (0 votes)
14 views7 pages

Solution_Série4_ASD En

The document contains model solutions for various exercises in a programming module at Barika University Center, focusing on algorithms and data structures. It includes code snippets in C for tasks such as array manipulation, matrix operations, and palindrome checking. Each exercise demonstrates different programming concepts relevant to first-year mathematics and computer science students.

Uploaded by

sennisel
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)
14 views7 pages

Solution_Série4_ASD En

The document contains model solutions for various exercises in a programming module at Barika University Center, focusing on algorithms and data structures. It includes code snippets in C for tasks such as array manipulation, matrix operations, and palindrome checking. Each exercise demonstrates different programming concepts relevant to first-year mathematics and computer science students.

Uploaded by

sennisel
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/ 7

Barika University Center. Module: ADS (S1).

Departments of mathematics Level: First year of bachelor's


and computer science. degree.
Model Solution for Series 4

Exercice 1 :

Algorithm S4_EXO1 #include <stdio.h>


Var int main() {
T:Array[0..99] of integer int tab[100],i;
n,i:integer for( i = 0 ; i < 100 ; i++) tab[i] = i;
begin for( i = 0 ; i < 100 ; i++)
for i=0 to 99 do if((i+1) % 10 == 0)
T[i] ← i; printf("%.2d\n", tab[i]);
for i=0 to 99 do else
write(T[i],", ") endfor printf("%.2d,", tab[i]);
end return 0;
}

Exercice 2 :

Algorithm S4_EXO2 #include <stdio.h>


Var int main() {
tab:array[0..99] integer int tab[100],i,n; printf("introduire n
n,i: integer "); scanf("%d",&n);
begin for( i = 0 ; i < n ; i++){
write ("introduire n") printf("tab[%d]=",i);
read(n) scanf("%d",&tab[i]);
for i=0 to n-1 do }
write ("tab[",i,"]=") printf("Nombres pairs: \n"); for( i
read (tab[i]) = 0 ; i < n ; i++)
if(tab[i] % 2 == 0) printf("%.2d\t",
endfor
for i=0 to n-1 do tab[i]);
printf("\nNombres impairs: \n"); for( i
if(tab[i] mod 2 =0)then
= 0 ; i < n ; i++)
write(tab[i]," ")
if(tab[i] % 2 != 0) printf("%.2d\t",
endif tab[i]);
endfor return 0;
for i=0 to n-1 do }
if(tab[i] mod 2 =1)then
write(tab[i]," ")
endif
endfor
end
Exercice 5 :

1- The same number of rows and columns for both matrices.


2- The number of columns in the first matrix is equal to the number of columns in the second matrix.
3- In order to calculate the sum and product of two matrices, M1 and M2, they must be square
matrices.

Algorithm S4_EXO5 end for


Variables end for
M1, M2, S, P: Array[100][100] of integer for i=0 to n-1 do
n, i, j, k: integer for j=0 to n-1 do
Begin for k=0 to n-1 do
write("Enter n") P[i][j] ← P[i][j] + M1[i][k] * M2[k][j]
read(n) end for
for i=0 to n-1 do end for
for j=0 to n-1 do end for
write("M1[",i,"][",j,"]=") for i=0 to n-1 do
read(M1[i][j]) for j=0 to n-1 do
end for write(S[i][j]," ")
end for end for
for i=0 to n-1 do write("┘")
for j=0 to n-1 do end for
write("M2[",i,"][",j,"]=") for i=0 to n-1 do
read(M2[i][j]) for j=0 to n-1 do
end for write(P[i][j]," ")
end for end for
for i=0 to n-1 do write("┘")
for j=0 to n-1 do end for
S[i][j] ← M1[i][j] + M2[i][j] End
#include <stdio.h> }
int main() { printf("\nM1 :\n");
int 1[10][10],M2[10][10],i,j,k,S[10][10],P[10][10],n; for (i=0;i<n;i++){
printf("enter n :"); for (j=0;j<n;j++)
scanf("%d",&n); printf("%.2d\t",M1[i][j]);
for (i=0;i<n;i++) printf("\n");
for (j=0;j<n;j++){ }
printf("M1[%d][%d]=",i,j); printf("\nM2 :\n");
scanf("%d",&M1[i][j]); for (i=0;i<n;i++){
} for (j=0;j<n;j++)
for (i=0;i<n;i++) printf("%.2d\t",M2[i][j]);
for (j=0;j<n;j++){ printf("\n");
printf("M2[%d][%d]=",i,j); }
scanf("%d",&M2[i][j]); printf("\nS :\n");
} for (i=0;i<n;i++){
for (i=0;i<n;i++) for (j=0;j<n;j++)
for (j=0;j<n;j++){ printf("%.2d\t",S[i][j]);
S[i][j]=0; printf("\n");
P[i][j]=0; }
} printf("\nP :\n");
for (i=0;i<n;i++) for (i=0;i<n;i++){
for (j=0;j<n;j++){ for (j=0;j<n;j++)
S[i][j]=M1[i][j]+M2[i][j]; printf("%.2d\t",P[i][j]);
} printf("\n");
for (i=0;i<n;i++) }
for (j=0;j<n;j++) return 0;
for (k=0;k<n;k++){
}
P[i][j]+=M1[i][k]*M2[k][j];

3
Exercice 6 :

Algorithm S4_EXO6 end for


Variables max ← min ← S[0]
M: Array[100][100] of integer J_max ← J_min ← 0
S: Array[100] of integer for i=0 to m-1 do
i, j, n, m, max, J_max, min, J_min, temp: integer if (max < S[i]) then
Begin max ← S[i]
write("Enter n and m") J_max ← i
read(n, m) else
for i=0 to n-1 do if (min > S[i]) then
for j=0 to m-1 do min ← S[i]
write("M[",i,"][",j,"]=") J_min ← i
read(M[i][j]) end if
end for end if
end for end for
for j=0 to m-1 do write("Max Value: S[",J_max,"]=",max)
S[j] ← 0 write("Min Value: S[",J_min,"]=",min)
for i=0 to n-1 do for i=0 to n-1 do
S[j] ← S[j] + M[i][j] temp ← M[i][J_max]
end for M[i][J_max] ← M[i][J_min]
end for M[i][J_min] ← temp
write("M before permutation=") end for
for i=0 to n-1 do write("M After permutation:")
for j=0 to m-1 do for i=0 to n-1 do
write(S[i][j]," ") for j=0 to m-1 do
end for write(" ", M[i][j])
write("┘") end for
end for write("┘")
write("S=") end for
for i=0 to m-1 do End
write(S[i]," ")

4
#include <stdio.h> //Calcule J_max et J_min
int main() { max=min=S[0];
int M[10][10],S[10],i,j,n,m,max,J_max,min,J_min,temp; J_max=J_min=0;
printf("enter n and m :"); for (i=1;i<m;i++)
scanf("%d%d",&n,&m); if(max<S[i]){
for (i=0;i<n;i++) max=S[i];
for (j=0;j<m;j++){ J_max=i;
printf("M[%d][%d]=",i,j); }else
scanf("%d",&M[i][j]); if(min>S[i]){
} min=S[i];
for (j=0;j<m;j++){ J_min=i;
S[j]=0; }
for (i=0;i<n;i++) printf("\nVal Max : S[%d]=%d\n",J_max,max);
S[j]+=M[i][j]; printf("\nVal Min : S[%d]=%d\n",J_min,min);
} // permutation
for (i=0;i<n;i++){
printf("\nM before permutation :\n"); temp=M[i][J_max];
for (i=0;i<n;i++){ M[i][J_max]=M[i][J_min];
for (j=0;j<m;j++) M[i][J_min]=temp;
printf("%.2d\t",M[i][j]); }
printf("\n");
} printf("\nM after permutation :\n");
printf("\nSum_Columns :\n"); for (i=0;i<n;i++){
for (j=0;j<m;j++)
for (i=0;i<m;i++){ printf("%.2d\t",M[i][j]);
printf("%.2d\t",S[i]); printf("\n");
}
} return 0;}
}

5
Exercice 7:

Algorithm S4_EXO7 #include <stdio.h>


Variables #include <string.h>
word: Array[0..49] of character int main() {
test, i: integer char mot[50]; int i=0,test=1;
Begin printf("enter a word\n");
write("Enter a word") gets(mot);
read(word)
while(i<strlen(mot)/2 && test==1){
test ← 1
if(mot[i]!=mot[strlen(mot)-1-i])
i←0
while i < length(word)/2 and test = 1 do test=0;
if (mot[i] <> mot[length(mot)-1-i]) then i++;
test ← 0 }
end if if(test==1)
i←i+1 printf("\nThe entered word is a
end while palindrome \n");
if (test = 1) then else
write("The entered word is a printf("\n The entered word is not a
palindrome") palindrome \n");
else return 0;
write("The entered word is not a
}
palindrome")
end if
End

6
Exercice 8:

#include <stdio.h>
Algorithm S4_EXO8
#include <string.h>
Variables int main() {
ch1, ch2: Array[0..49] of character char ch1[50],ch2[50];
co, i, j, test: integer int co=0,i,j,test;
Begin printf("entrer Ch1\n");
write("Enter Ch1") scanf("%s",ch1);
read(ch1) getchar();
write("Enter Ch2") printf("entrer Ch2\n");
read(ch2) scanf("%s",ch2);
co ← 0 getchar();
for i=0 to length(ch1)-1 do for(i=0;i<strlen(ch1);i++){
test ← 1 test=1;
j←0
j=0;
while j < length(ch2) and test = 1 do
while(j<strlen(ch2) && test==1){
if (ch1[i+j] <> ch2[j]) then
test ← 0 if(ch1[i+j]!=ch2[j])
end if test=0;
j←j+1 j++;
end while }
if (test = 1) then if(test==1)
co ← co + 1 co++;
end if }
end for printf("Ch2 exists %d times in Ch1",co);
write("Ch2 exists", co, " times in Ch1") return 0;
End }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy