Using the functions of the previous exercises, write a program that reads an array A with a dimension less than or equal to 100 and displays the array and the sum of the elements of the array.


Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#define NMAX 100

int READARRAY(int TAB[])
{
	int I, N;
	do
	{
		printf("Size of the array (max.%d) : ", NMAX);
		scanf("%d", &N); 
	} while (N<0 || N>NMAX);

	for (I = 0; I<N; I++)
	{
		printf("Element[%d] : ", I);
		scanf("%d", &TAB[I]);
	}
	return N;
}


long SUM_TAB(int TAB[], int N)
{
	int i = 0;
	long SUM = 0;
	while (i<N)
	{
		SUM += TAB[i];
		i++;
	}
	return SUM;
}

 

int main()
{
 
    int N, T[NMAX];
    long sum;
    
	N = READARRAY(T);
	printf("Sum=%ld\n",SUM_TAB(T, N));
 
	return 0;
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Finding a value in an array using Interpolation search