exo 1:
1-
#include <iostream>
using namespace std;
void LIRE_TAB(int TAB[], int& N) {
cout << "Enter the size of the array: ";
cin >> N;
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < N; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> TAB[i];
}
}
2-
#include <iostream>
using namespace std;
void ECRIRE_TAB(int TAB[], int N) {
cout << "Array elements:" << endl;
for (int i = 0; i < N; ++i) {
cout << TAB[i] << " ";
}
cout << endl;
}
3-
#include <iostream>
using namespace std;
int SOMME_TAB(int TAB[], int N) {
int sum = 0;
for (int i = 0; i < N; ++i) {
sum += TAB[i];
}
return sum;
}
4-
#include <iostream>
using namespace std;
int MIN_TAB(int TAB[], int N) {
int minVal = TAB[0];
for (int i = 1; i < N; ++i) {
if (TAB[i] < minVal) {
minVal = TAB[i];
}
}
return minVal;
}
5-
#include <iostream>
using namespace std;
int MAX_TAB(int TAB[], int N) {
int maxVal = TAB[0];
for (int i = 1; i < N; ++i) {
if (TAB[i] > maxVal) {
maxVal = TAB[i];
}
}
return maxVal;
}
6-
#include <iostream>
using namespace std;
int main() {
const int MAX_SIZE = 100;
int TAB[MAX_SIZE];
int N;
// Exercise 1
LIRE_TAB(TAB, N);
ECRIRE_TAB(TAB, N);
cout << "Sum of array elements: " << SOMME_TAB(TAB, N) << endl;
cout << "Minimum element: " << MIN_TAB(TAB, N) << endl;
cout << "Maximum element: " << MAX_TAB(TAB, N) << endl;
return 0;
}
-------------------
exo 2:
#include <iostream>
using namespace std;
int puissance(int a, int b) {
int result = 1;
for (int i = 0; i < b; ++i) {
result *= a;
}
return result;
}
---
#include <iostream>
using namespace std;
// Function prototype
int puissance(int a, int b);
int main() {
int base, exposant;
cout << "Entrez la base : ";
cin >> base;
cout << "Entrez l'exposant : ";
cin >> exposant;
// Function call
int resultat = puissance(base, exposant);
cout << base << " exposant " << exposant << " = " << resultat << endl;
return 0;
}
// Function definition
int puissance(int a, int b) {
if (b == 0) {
return 1;
} else if (b > 0) {
int resultat = 1;
for (int i = 0; i < b; ++i) {
resultat *= a;
}
return resultat;
} else {
cerr << "Erreur : L'exposant n'est pas géré pour les nombres négatifs." <<
endl;
return -1;
}
}
------------
exo3:
a-
#include <iostream>
using namespace std;
int MAX1(int TAB[], int N) {
int maxVal = TAB[0];
for (int i = 1; i < N; ++i) {
if (TAB[i] > maxVal) {
maxVal = TAB[i];
}
}
return maxVal;
}
b-
#include <iostream>
using namespace std;
int MAX2(int TAB[], int N) {
int maxIndex = 0;
for (int i = 1; i < N; ++i) {
if (TAB[i] > TAB[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
c-
#include <iostream>
using namespace std;
int* MAX3(int TAB[], int N) {
int* maxAddress = &TAB[0];
for (int i = 1; i < N; ++i) {
if (TAB[i] > *maxAddress) {
maxAddress = &TAB[i];
}
}
return maxAddress;
}
d-Programme principal pour tester les trois fonctions:
#include <iostream>
using namespace std;
int main() {
const int MAX_SIZE = 100;
int TAB[MAX_SIZE];
int N;
// Prompt user for array size and elements
cout << "Enter the size of the array: ";
cin >> N;
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < N; ++i) {
cout << "Element " << i + 1 << ": ";
cin >> TAB[i];
}
// Testing MAX1, MAX2, and MAX3
cout << "Maximum value in the array: " << MAX1(TAB, N) << endl;
cout << "Index of the maximum element: " << MAX2(TAB, N) << endl;
cout << "Address of the maximum element: " << MAX3(TAB, N) << endl;
return 0;
}
----------------
exo4:
Fonction récursive pour calculer la somme des entiers de 0 à n:
#include <iostream>
using namespace std;
int sommeRecusive(int n) {
if (n == 0) {
return 0;
} else {
return n + sommeRecusive(n - 1);
}
}
Programme principal pour tester la fonction récursive:
#include <iostream>
using namespace std;
int main() {
int n;
// Prompt user for a positive integer
cout << "Enter a positive integer (n): ";
cin >> n;
// Test the recursive sum function
cout << "Sum of integers from 0 to " << n << ": " << sommeRecusive(n) << endl;
return 0;
}
------
or #include <iostream>
using namespace std;
// Fonction récursive pour calculer la somme des entiers de 0 à n
int sommeJusquAN(int n) {
// Cas de base : si n est 0, la somme est 0
if (n == 0) {
return 0;
} else {
// Appel récursif : somme des entiers de 0 à n-1, plus n
return sommeJusquAN(n - 1) + n;
}
}
int main() {
int n;
cout << "Entrez un entier positif n : ";
cin >> n;
// Vérification que l'entrée est un entier positif
if (n < 0) {
cerr << "Erreur : Veuillez entrer un entier positif." << endl;
return 1; // Code d'erreur
}
// Appel de la fonction récursive pour calculer la somme
int resultat = sommeJusquAN(n);
// Affichage du résultat
cout << "La somme des entiers de 0 à " << n << " est : " << resultat << endl;
return 0;
}