Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: 1. Review top-down design 2. Understanding and apply the concept of r

ID: 3572467 • Letter: O

Question

Objective:

1.      Review top-down design

2.      Understanding and apply the concept of recursive thinking

Problem description:

Writer the following two functions and test them:

In str_length(char s[], int start:

// Recursive version of strlen

// It returns the length of the substring from s[start] to

// the char before ‘’,

// inclusive. When the str_length(s, 0) is called, the length

// of s is returned.

int sum(int n);

// Recursive version to calculate the sum of

// 1 + 2 + …..+ n

Please run the demo to see how the program should run.

Explanation / Answer

#include <stdio.h>
int str_length(char s[], int start){
if(s[start] == ''){
return start;
}
else{
return str_length(s, start+1);
}
}
int sum(int n){
if(n == 0){
return 0;
}
else{
return n + sum(n-1);
}
}
int main()
{
char s[100] ="Suresh";
int n =10;
printf("String length is %d ", str_length(s,0));
printf("Sum of %d is %d ", n, sum(n));
  
return 0;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

String length is 6                                                                                                                                                                                                                                     

Sum of 10 is 55