Exercice :
Écrire un programme qui lit deux tableaux d’entiers A et B et leurs dimensions N et M au clavier et qui ajoute les éléments de B à la fin de A. Utiliser deux pointeurs PA et PB pour le transfert et afficher le tableau résultant A.
Solution :
#include <iostream>
using namespace std ;
int main()
{
int N,M ;
cout << "Please enter the value of N :" ;
cin >> N ;
cout << "Please enter the value of M :" ;
cin >> M ;
int* tab1 = new int[N+M] ;
int* tab2 = new int[M] ;
cout << "Enter " << N << " values for the first array :" << endl ;
for(int i = 0 ; i < N ; i++)
{
cin >> tab1[i] ;
}
cout << "Enter " << M << " values for the second array :" << endl ;
for(int i = 0 ; i < M ; i++)
{
cin >> tab2[i] ;
}
int* ptr1 = tab1 + N ;
int* ptr2 = tab2 ;
for( ; ptr2 < tab2 + M ; ptr1++ , ptr2++)
{
*ptr1 = *ptr2 ;
}
cout << "The first array elements are : " ;
for(int i = 0 ; i < N+M ; i++)
{
cout << tab1[i] << " " ;
}
cout << endl << "The second array elements are : " ;
for(int i = 0 ; i < M ; i++)
{
cout << tab2[i] << " " ;
}
delete []tab1 ;
delete []tab2 ;
return 0 ;
}