Write a program that reads the dimension N1 and N2 of two integer arrays T1 and T2 (maximum dimension: 50 components), fills the arrays with values entered on the keyboard and displays the array.
The program then indicates if these two arrays are identical or not.
Examples:
- T1={4, 5, 7, 8, 4} and T2={7, 5, 4, 8} are identical
- T1={4, 5, 7, 8, 4} and T2={7, 5, 4, 8, 0} are not identical
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define SIZE 50
void main()
{
int N1, N2; // nb of elements
int T1[SIZE], T2[SIZE]; // arrays of maximum size 50
int i, j, found;
do {
printf("Enter N1: ");
scanf("%d", &N1);
} while (N1<0 || N1>SIZE);
for (i = 0; i < N1; i++)
{
printf("Enter T1[%d]: ", i);
scanf("%d", &T1[i]);
}
printf("\n\nPrinting the elements of T1\n");
for (i = 0; i < N1; i++)
{
printf("T1[%d]=%d\n", i, T1[i]);
}
do {
printf("Enter N2: ");
scanf("%d", &N2);
} while (N2<0 || N2>SIZE);
for (i = 0; i < N2; i++)
{
printf("Enter T2[%d]: ", i);
scanf("%d", &T2[i]);
}
printf("\n\nPrinting the elements of T2\n");
for (i = 0; i < N2; i++)
{
printf("T2[%d]=%d\n", i, T2[i]);
}
for(i=0;i<N1;i++)
{
found=0; // flag to state that at the beginning, T1[i] is not yet found
for(j=0;j<N2;j++)
if(T1[i]==T2[j])
{
found=1; // set found to 1 if T1[i] is found in T2
break;
}
if(found==0) // if after looping, found is till not equal to 1, we end everything
break;
}
if(found==1) // check reverse order
{
for(i=0;i<N2;i++)
{
found=0; // flag to state that at the beginning, T2[i] is not yet found
for(j=0;j<N1;j++)
if(T2[i]==T1[j])
{
found=1; // set found to 1 if T2[i] is found in T1
break;
}
if(found==0) // if after looping, found is till not equal to 1, we end everything
break;
}
}
if(found==1)
printf("\nArrays are identical");
else
printf("\nArrays are not identical");
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Distinct values in a binary search tree