Série d’exercices de C++ sur les tableaux
Voici une troisièmes série de langage C++ sur les tableaux, 10 exercices avec solutions pour bien maîtriser la notion des tableaux.
Cette liste commence par les exercices les plus faciles jusqu’à les plus difficile. Bonne apprentissage 🙂
Exercice 1 – Ecrivez un programme C ++ pour trouver le plus grand élément d’un tableau donné d’entiers.
solution :
#include<bits/stdc++.h>
using namespace std;
int find_largest(int nums[], int n)
{
return *max_element(nums, nums + n); }
int main()
{
int nums[] = { 5, 4, 9, 12, 8 };
int n = sizeof(nums) / sizeof(nums[0]);
cout << "Original array:";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
cout << "\nLe plus grand élément dans le tableau: "<< find_largest(nums, n);
return 0;
}
Exercice 2 – Ecrivez un programme C ++ pour trouver les trois plus grands éléments d’un tableau.
solution :
#include<bits/stdc++.h>
using namespace std;
void three_largest(int arr[], int arr_size)
{
int i, first, second, third;
if (arr_size < 3)
{
cout << "Invalid Input"; }
third = first = second = INT_MIN;
for (i = 0; i < arr_size ; i ++)
{
if (arr[i] > first)
{
third = second;
second = first;
first = arr[i]; }
else if (arr[i] > second)
{
third = second;
second = arr[i];
}
else if (arr[i] > third) third = arr[i];
}
cout << "\nLes trois plus grand éléments sont: " <<first <<", "<< second <<", "<< third;
}
int main()
{
int nums[] = {7, 12, 9, 15, 19, 32, 56, 70};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++) cout << nums[i] <<" ";
three_largest(nums, n);
return 0;
}
Exercice 3 – Ecrivez un programme C ++ pour trouver le deuxième élément le plus grand dans un tableau d’entiers donné.
solution :
#include<bits/stdc++.h>
using namespace std;
void second_largest(int nums[], int arr_size)
{
int i, first_element, second_element;
/* There should be atleast two elements */
if (arr_size < 2)
{
cout<< " Invalid Input ";
return;
}
first_element = second_element = INT_MIN;
for (i = 0; i < arr_size ; i ++)
{
if (nums[i] > first_element)
{
second_element = first_element;
first_element = nums[i];
}
else if (nums[i] > second_element && nums[i] != first_element)
{
second_element = nums[i];
} }
if (second_element == INT_MIN)
{
cout<< "No second largest element";
}
else
{
cout<< "\nThe second largest element is: " <<second_element;
} }
int main()
{
int nums[] = {7, 12, 9, 15, 19, 32, 56, 70};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
second_largest(nums, n);
return 0;
}
Exercice 4 – Ecrivez un programme C ++ pour trouver k plus grands éléments dans un tableau d’entiers donné.
solution :
#include<bits/stdc++.h>
using namespace std;
void kLargest(int nums[], int n, int k)
{
sort(nums, nums+n, greater<int>());
cout << "\nLargest " << k << " Elements: ";
for (int i = 0; i < k; i++)
cout << nums[i] << " ";
}
int main()
{
int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
int k = 4;
kLargest(nums, n, k);
}
Exercice 5 – Ecrivez un programme C ++ pour rechercher les éléments les plus petits et les plus petits dans un tableau d’entiers donné.
solution :
#include <iostream>
using namespace std;
int find_Second_Smallest(int array_num[], int n)
{
int smallest_num, second_smallest_num;
if (array_num[0] < array_num[1])
{
smallest_num = array_num[0];
second_smallest_num = array_num[1];
}
else
{
smallest_num = array_num[1];
second_smallest_num = array_num[0];
}
for (int i = 0; i < n; i++)
{
if (smallest_num > array_num[i])
{
second_smallest_num = smallest_num;
smallest_num = array_num[i];
}
else if (array_num[i] < second_smallest_num)
{
second_smallest_num = array_num[i];
} }
return second_smallest_num;
}
int main()
{
int n = 7;
int array_num[7] = { 5, 6, 7, 2, 3, 4, 12 };
int s = sizeof(array_num) / sizeof(array_num[0]);
cout << "Original array: ";
for (int i=0; i < s; i++)
cout << array_num[i] <<" ";
int second_smallest_num = find_Second_Smallest(array_num, n);
cout<<"\nSecond smallest number: "<<second_smallest_num;
return 0;
}
Exercice 6 – Ecrivez un programme C ++ pour trouver tous les éléments dans un tableau d’entiers comportant au moins deux éléments supérieurs.
solution :
#include<bits/stdc++.h>
using namespace std;
void find_greater_elements(int nums[], int n)
{
cout << "\nÉlements qui ont au moins deux éléments supérieurs: ";
for (int i=0; i<n; i++)
{
int ctr = 0;
for (int j=0; j<n; j++)
if (nums[j] > nums[i])
ctr++;
if (ctr >= 2)
cout << nums[i] << " ";
} }
int main()
{
int nums[] = {7, 12, 9, 15, 19, 32, 56, 70};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
find_greater_elements(nums, n);
return 0;
}
Exercice 7 – Ecrivez un programme C ++ pour trouver l’élément le plus existant dans un tableau d’entiers.
solution :
#include<bits/stdc++.h>
using namespace std;
void most_occurred_number(int nums[], int size)
{
int max_count = 0;
cout << "\nMost occurred number: ";
for (int i=0; i<size; i++)
{
int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count>max_count)
max_count = count;
}
for (int i=0;i<size;i++)
{
int count=1;
for (int j=i+1;j<size;j++)
if (nums[i]==nums[j])
count++;
if (count==max_count)
cout << nums[i] << endl;
} }
int main()
{
int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
most_occurred_number(nums, n);
}
Exercice 8 – Ecrivez un programme C ++ pour trouver le prochain élément supérieur de chaque élément d’un tableau d’entiers donné. Ignorer les éléments qui n’ont pas de plus grand élément.
solution :
#include<bits/stdc++.h>
using namespace std;
void next_greater(int nums[], int n)
{
stack<int> data_stack;
data_stack.push(nums[0]);
for (int i=1; i<n; i++)
{
int next_element = nums[i];
if (data_stack.empty() == false)
{
int array_element = data_stack.top();
data_stack.pop();
while (array_element < next_element)
{
cout << array_element << ": " << next_element << endl;
if (data_stack.empty() == true) break;
array_element = data_stack.top();
data_stack.pop();
}
if (array_element > next_element)
data_stack.push(array_element);
}
data_stack.push(next_element);
} }
int main()
{
int nums[] = {4, 1, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
cout << "\nNext Greater Element:\n";
next_greater(nums, n);
}
Exercice 9 – Ecrivez un programme C ++ pour trier un tableau d’entiers non trié, sous forme d’onde.
Remarque: Un tableau est sous forme d’onde lorsque array [0]> = array [1] <= array [2]> = array [3] <= array [4]> =. . . .
solution :
#include<iostream>
#include<algorithm>
using namespace std;
void swap_elements(int *a, int *b)
{
int t = *a; *a = *b; *b = t;
}
void array_wave(int nums[], int n)
{
sort(nums, nums+n);
for (int i=0; i<n-1; i += 2)
swap_elements(&nums[i], &nums[i+1]);
}
int main()
{
int nums[] = {4, 5, 9, 12, 9, 22, 45, 7};
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Le tableau originale: ";
for (int i=0; i < n; i++) cout << nums[i] <<" ";
array_wave(nums, n);
cout << "\nLa forme d'onde de tableau: ";
for (int i=0; i<n; i++) cout << nums[i] << " ";
return 0;
}
Exercice 10 – Ecrivez un programme C ++ pour trouver le plus petit élément manquant dans un tableau trié.
solution :
#include<iostream>
#include<algorithm>
using namespace std;
int smalest_missing_num(int nums[], int start_pos, int end_pos)
{
if (start_pos > end_pos)
return end_pos + 1;
if (start_pos != nums[start_pos])
return start_pos;
int mid = (start_pos + end_pos) / 2;
if (nums[mid] == mid)
return smalest_missing_num(nums, mid + 1, end_pos);
return smalest_missing_num(nums, start_pos, mid);
}
int main()
{
int nums[] = {0, 1, 3, 4, 5, 6, 7, 8, 10};
int result;
int n = sizeof(nums)/sizeof(nums[0]);
cout << "Original array: ";
for (int i=0; i < n; i++)
cout << nums[i] <<" ";
result = smalest_missing_num(nums, 0, n-1);
cout << "\nLe plus petit élement est " << result;
return 0;
}