- Write a C function DisplayArray that takes as parameter an array and two index m and n. This function should display all arrays’ elements that are between these indexes.
- Write a C function MaxMin that takes as parameter an array of integers and its size that calculates and displays (using DisplayArray) the maximum of first half of array, and calculates and displays the minimum of second half of array as in the example of execution.
- Write a C function ReadArray that take as parameter an array and its size then fills the array by values entered by the user.
- Write a main function that asks the user to enter the size of the array (should be an even number), fills the array (function ReadArray), then calculates the maximum of first half and the minimum of second half of the array (MaxMin).
Example of execution:
Enter the size of the array (even nb): 9
Enter the size of the array (even nb): 10
Enter 10 elements of array: 5 2 8 3 1 10 9 7 18 12
The maximum of first half of array 5 2 8 3 1 is 8
The minimum of second half of array 10 9 7 18 12 is 7
Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define SIZE 50
void DisplayArray(int T[], int m, int n)
{
int i;
for (i=m; i<n; i++)
printf("%d ",T[i]);
}
void MaxMin(int T[], int n)
{
int i, max=T[0],min=T[n/2];
for(i=0;i<n/2;i++)
{
if(T[i]>max)
max=T[i];
if(T[n/2+i]<min)
min=T[n/2+i];
}
printf("The maximum of first half of array ");
DisplayArray(T,0,n/2);
printf(" is %d\n",max);
printf("The minimum of second half of array ");
DisplayArray(T,n/2,n);
printf(" is %d\n",min);
}
void ReadArray(int T[], int n)
{
int i;
printf("Enter %d elements of array : ",n);
for(i=0;i<n;i++)
scanf("%d",&T[i]);
}
main ()
{
int n, T[SIZE];
do
{
printf("Enter the size of the array (even nb): ");
scanf("%d",&n);
} while (n%2 !=0);
ReadArray(T,n);
MaxMin(T,n);
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Ancestor sum in binary trees