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

Hello I could use some help figuring this out please. So I am going to list firs

ID: 3716213 • Letter: H

Question

Hello I could use some help figuring this out please. So I am going to list first my problem and then what I have worked out but I have an issue with when I build it with my Microsoft Visual Studio program it will load and then go right off, it doesn't allow you to enter any input. I have seen others that show an output but my program doesn't. If someone could help me I would be greatful.

Thanks

So here is the problem that was given to me:

Write the following 2 functions and test them.

Both of them need to be recursive function.

int sum(int n);

// Recursive version to calculate the sum of

// 1 + 2 + .... + n

int str_length(char s[]);

// Recursive version of strlen in C strings.

// It returns the length of the string s[].

// (the null character, '', is not counted in the length)

Sample Run

Note: I use ? symbol to show user inputs, you do NOT need to do ? in your program.

Enter a positive integer: ?10

The sum of 1+2+...+10 is: 55

Enter a sentence: ?Hello World!

It contains 12 chars, including white spaces

Do you want to have another run? Y/N: ?y

Enter a positive integer: ?100

The sum of 1+2+...+100 is: 5050

Enter a sentence: ?I love programming!

It contains 19 chars, including white spaces

Do you want to have another run? Y/N: ?n

Program ended with exit code: 0

And here is what I have worked out and researched for a written program but like I mentioned earlier it will build but the window comes up and then goes right off. I use Microsoft Visual Studio to write my programs if that helps or makes a difference.

#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;

}

I have to turn this in by May 1. Thanks.

Explanation / Answer

PROGRAM

#include <stdio.h>

#include <stdlib.h>

// create functon str_length()

int str_length(char s[], int start) {

if (s[start] == '') { // check condition null then return start=0

return start;

}

else {

return str_length(s, start + 1); // else calling str_length() function recursively until character

}

}

// create function sum() funtion

int sum(int n) {

if (n == 0) { // check condition n=0 then return 0

return 0;

}

else {

return n + sum(n - 1); // else calling sum() function recursively until n=0

}

}

int main()

{

char s[100]; // declare character array

char ch; // declare character for choice

int n,i; // declare two integer variables

while(1) // cresate infinity while loop

{

printf(" Enter a positive integer: ? ");

scanf("%d",&n); // read positive integer

if(n>=0){ // check condition whether negative or not, if this condition true then

printf(" The sum of ");

for(i=1;i<=n;i++) // create for loop for print the series

{

printf("%d+",i); // print the series

}

printf(" is: %d",sum(n)); // calling sum() fucntion and print sum of series

}

else

printf(" It should be positive number only... "); // if the n value is negative then print this message

fflush(stdin); // clear the output buffer for reading string

printf(" Enter a sentence: ? ");

scanf("%[^ ]",s); // reading string with spaces

printf(" It contains %d chars, including white spaces",str_length(s,0)); // calling str_length() function

printf(" Do you want to have another run? Y/N: ?");

fflush(stdin); // clear the output buffer for reading string

scanf("%c",&ch); // read choice

if(ch=='n'||ch=='N') // check condition n then exit from the program

{

printf(" Program ended with exit code: 0");

exit(0);

}

}

//printf("String length is %d ", str_length(s, 0));

//printf("Sum of %d is %d ", n, sum(n));

return 0;

}

OUTPUT


Enter a positive integer: ? 10

The sum of 1+2+3+4+5+6+7+8+9+10+ is: 55
Enter a sentence: ? Hello World!

It contains 12 chars, including white spaces
Do you want to have another run? Y/N: ?y

Enter a positive integer: ? 100

The sum of 1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100+ is: 5050
Enter a sentence: ? I love programming!

It contains 19 chars, including white spaces
Do you want to have another run? Y/N: ?n

Program ended with exit code: 0

UPDATED PROGRAM

#include <stdio.h>

#include <stdlib.h>

// create functon str_length()

int str_length(char s[], int start) {

if (s[start] == '') { // check condition null then return start=0

return start;

}

else {

return str_length(s, start + 1); // else calling str_length() function recursively until character

}

}

// create function sum() funtion

int sum(int n) {

if (n == 0) { // check condition n=0 then return 0

return 0;

}

else {

return n + sum(n - 1); // else calling sum() function recursively until n=0

}

}

int main()

{

char s[100]; // declare character array

char ch; // declare character for choice

int n,i; // declare two integer variables

while(1) // cresate infinity while loop

{

printf(" Enter a positive integer: ? ");

scanf("%d",&n); // read positive integer

if(n>=0){ // check condition whether negative or not, if this condition true then

printf(" The sum of ");

for(i=1;i<=n;i++) // create for loop for print the series

{

printf("%d+",i); // print the series

}

printf(" is: %d",sum(n)); // calling sum() fucntion and print sum of series

}

else

printf(" It should be positive number only... "); // if the n value is negative then print this message

fflush(stdin); // clear the output buffer for reading string

printf(" Enter a sentence: ? ");

//scanf("%[^ ]",s); // reading string with spaces

gets(s);

printf(" It contains %d chars, including white spaces",str_length(s,0)); // calling str_length() function

printf(" Do you want to have another run? Y/N: ?");

fflush(stdin); // clear the output buffer for reading string

//scanf("%c",&ch); // read choice

ch=getchar();

if(ch=='n'||ch=='N') // check condition n then exit from the program

{

printf(" Program ended with exit code: 0");

exit(0);

}

}

//printf("String length is %d ", str_length(s, 0));

//printf("Sum of %d is %d ", n, sum(n));

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote