i need to find the sum of a sequence of numbers, + and - while using only recursion (not allowed to use loops).
I can only change the function and nothing more, that includes the pointer in the function (and can't add anything else to it). I'm also not allowed to use any other library but stdio.h.
#include <stdio.h>
#define MAX_LENGTH 100
int calc_sum_string(char *s);
int main() {
char s[MAX_LENGTH];
scanf("%s", s);
printf("%d", calc_sum_string(s));
return 0;
}
int calc_sum_string(char *s) {
int sum = s[MAX_LENGTH];
if (*s == '\0'){
return sum;
}
if (*s == '+'){
sum = calc_sum_string(s-1)+ calc_sum_string(s+1);
}
if (*s == '-'){
sum = calc_sum_string(s+1) - calc_sum_string(s-1);
return sum;
}
input: 7-8+9
output: 8
Your present code has several problems. The ones you need to address first include:
Fix those items. If you still have problems, please give us a proper posting of your remaining problems, and we'll help you get over the hump.
thank you all, this is my final code