- Write a function ArrayEqual that takes two arrays A1 and A2 of the same size and checks if the two arrays are equal. Two arrays are equal when their same position elements are equal.
Example:
A1 = {2,4,5,1} and A2 = {2,4,5,1} are equal
A1 = {2,4,5,1} and A2 = {2,1,4,5} are NOT equal - Write a function ArrayReverse that takes two arrays A1 and A2 of the same size and put the elements of A1 in A2 in reverse order.
Example: if A1 = {2,4,5,1} --> A2 = {1,5,4,2} - Using the two previous functions, Write a function ArraySymmetric that takes an array A and checks if it is symmetric. An array is symmetric if it is mirrored according to its middle (if you read it from the end, you will have the same values as when you read it from the beginning)
Example: array A1 = {1,2,2,1} and A2 = {1,2,3,2,1} are symmetric.
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <math.h>
#define NMAX 20
int READARRAY(int TAB[])
{
int I, M;
do
{
printf("Size of the array (max.%d) : ", NMAX);
scanf("%d", &M);
} while (M<0 || M>NMAX);
for (I = 0; I<M; I++)
{
printf("Element[%d] : ", I);
scanf("%d", &TAB[I]);
}
return M;
}
int equal(int T1[], int S1, int T2[], int S2)
{
int i;
if(S1!=S2) return 0;
for(i=0; i <S1;i++)
if(T1[i]!=T2[i])
return 0;
return 1;
}
void reverse(int T1[], int S1, int T2[])
{
int i;
for(i=0;i<S1;i++)
T2[S1-i-1]=T1[i];
}
int ArraySymmetric(int T1[], int S1)
{
int T3[NMAX];
reverse(T1,S1,T3);
return equal(T1,S1,T3,S1);
}
int main()
{
int S1, T1[NMAX];
S1 = READARRAY(T1);
printf("Symmetric ? %d\n",ArraySymmetric(T1,S1));
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Variable static implementation of a stack