If the sum is calculated to be greater than 20, the program has no problem displaying it. If the sum is less than 20, however, the program feels the need to add 20 to it? I implemented an if/ else statement to try and correct it, but it was unsuccessful.
Code:
#include <stdio.h>
#include <string.h>
int sum(int i, int element[i]); /*Prototype for recursive addition */
int main(void)
{
int max_elements, element[20], i, result, total;
i=0;
printf("Determine the number of elements to be entered from 1 to 20: ");
scanf("%d", &max_elements);
for(i=0; i<max_elements; i++)
{ printf("Enter an integer: ");
scanf("%d", &element[i]);
}
result=sum(max_elements, &element[i]);
if (sum(max_elements, element)<20)
{
total=((sum(max_elements, element))-20);
printf("The sum is %d", total);
}
else
{
printf("The sum is %d", sum(max_elements,element));
}
return 0;
}
int sum(int i, int element[i])
{
if (i==0)
{
return element[i];
}
else
{
return element[i]+ sum(i-1, element);
}
}