• Write the function $\texttt{int READDIM()}$ that reads and returns a strictly positive integer $N$ less than 50;
  • Write the function $\texttt{void READARRAY(int T[], int N)}$ that fills $N$ positive integers in the array $\texttt{T}$;
  • Write the function $\texttt{void PRINTARRAY(int T[], int N)}$ that prints $N$ elements of the array $\texttt{T}$;
  • Write the function $\texttt{int INDEX_MAX(int T[], int N)}$ that returns the index of the first occurrence of the maximum element appearing in $\texttt{T}$;
  • Write the function $\texttt{int NB_OCC(int T[], int N, int dist[], int eff[])}$ that given an array $\texttt{T}$ containing $N$ elements, performs the following:
    • Fills the array $\texttt{dist}$ with the distinct elements of $\texttt{T}$;
    • Fills the array $\texttt{eff}$ with the number of occurrence of each distinct element of $\texttt{T}$;
    • Returns the number of distinct elements of $\texttt{T}$.
    Example: Let $\texttt{T}$ contains the following elements: 4 5 5 7 4 2 3 5 3 3.
    • Array $\texttt{dist}$ will contain the following elements: 4 5 7 2 3.
    • Array $\texttt{eff}$ will contain the following elements: 2 3 1 1 3, since the elements 4 appears 2 times in $\texttt{T}$, the elements 5 appears 3 times in $\texttt{T}$, and so on $\cdots$
    • The returned value is 5 since the number of distinct elements is 5 in $\texttt{T}$.
  • By calling all the above written functions, write a $\texttt{main}$ function that performs the following:
    • Read the dimension of an array $\texttt{T}$;
    • Fills $\texttt{T}$ with elements;
    • Displays the elements of $\texttt{T}$;
    • Fills and displays arrays $\texttt{dist}$ and $\texttt{eff}$;
    • Calculates and displays the maximum element and its number of occurrence in $\texttt{T}$;
    • Calculates and displays the first element that appeared the maximum times and its number of occurrence in $\texttt{T}$.

Running example


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#define size 50

int READDIM()
{
	int N; 
	do {
		printf("Saisir la dimension: ");
		scanf("%d", &N);
	} while (N <= 0 || N>size);
	return N;
}

void READARRAY(int Arr[], int C)
{
	int i;
	printf("Saisir %d elements: ", C);
	for (i = 0; i < C; i++)
		scanf("%d", &Arr[i]);
}

void PRINTARRAY(int Arr[], int C)
{
	int i;
	for (i = 0; i < C; i++)
		printf("%d ", Arr[i]);
	printf("\n");
}

int index_max(int A[], int N)
{
    int i , imax=0;
    for(i=1 ; i <N; i++)
        if(A[i]>A[imax])
            imax = i;
    return imax;
}

int Nb_OCC(int A[], int N, int dist[], int eff[])
{
	int i, j, c = 0;
	for (i = 0; i < N; i++)
	{
	    for(j=0; j<c; j++)
	        if(dist[j]==A[i])
	        {
	            eff[j]++;
	            break;
	        }
	   if(j>=c)
	   {
	       dist[j]=A[i];
	       eff[j]=1;
	       c++;
	   }
	}
	return c;
}

int main()
{
	int A[size], dist[size], eff[size];
	int N, M , imax; 

	printf("** Tableau **\n");
	N = READDIM();
	READARRAY(A, N);
	printf("Elemets du tableau: ");
	PRINTARRAY(A,N);
	
	M =  Nb_OCC(A, N, dist, eff);
	printf("Elements de Dist : ");
	PRINTARRAY(dist,M);
	printf("Elements de Eff: ");
	PRINTARRAY(eff,M);
 
    imax = index_max(dist, M);
 
    printf("Element maximal est \"%d\" et il est apparu %d fois dans le tableau.\n",dist[imax],eff[imax]);
    
    
    imax = index_max(eff, M);
 
    printf("Le premier element qui est apparu le maximum de fois est \"%d\" et il est apparu %d fois.\n",dist[imax],eff[imax]);
    
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Asymptotic Analysis 19