Question

c++ Write the following 2 functions and test them. Both of them need to be recursive...

c++ Write the following 2 functions and test them. Both of them need to be recursive functions.

int sum(int n);

// recursive version to calculate the sum of 1 + 2 + ..... + n

int str_length(char s[];

// Returns length of the string s[] and the null character, '0\', is not counted in the length).

Example of program execution;

Enter a positive integer: 10 (user input)

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

Enter a sentence: Hello World! (user input)

The sentence contains 12 characters, including white spaces.

Do you want to run again? Y/N: y (user input)

repeat above steps...

Do you want to have another run? Y/N? n (user input)

Program ended with exit code: 0

What I have so far;

#include "stdafx.h"

#include <iostream>

using namespace std;

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

int sum(int n);

char ch;

int main()

{

//characters

char sentence[100];

cout << "Enter a sentence: ";

cin.getline(sentence, 99);

cout << "There are " << str_length(sentence, 0) << " charcters in the sentence." << endl;

// sum

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Sum is " << sum(n) << endl;

}

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

if (s[start] == '\0')

return 0;

return 1 + str_length(s, start + 1); //recursive method to count char from 1 to '\0'

}

int sum(int n) {

if (n == 0)

return 0;

return n + sum(n - 1); //recursive method to calculate sum from 1 to n

}

I just need help with setting it up to ask the user if they want to run again and if they say yes, doing so. What I've tried so far either caused infinite loops or errors.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

below i written complete C++ code as per the requirement.

Please note the below program has been tested on ubuntu 16.04 system and compiled using g++ compiler. This code will also work on code blocks and visual studio.

-----------------------------------------------------------------------------------------------------------------------------------

Program:

------------------------------------------------------------------------------------------------------------------------------------

//#include "stdafx.h"

#include <iostream>

using namespace std;

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

int sum(int n);

char ch;

int main()

{

//characters

char sentence[100];

char ans;

do

{

cout << "Enter a sentence: ";

cin.getline(sentence, 99);

cout << "There are " << str_length(sentence, 0) << " charcters in the sentence." << endl;

// sum

int n;

cout << "Enter a positive integer: ";

cin >> n;

cout << "Sum is " << sum(n) << endl;

cout<<"DO you want to continue(Y/y| N/n) ? ";

cin>>ans;

cin.ignore();

if(ans == 'N' || ans == 'n')

{

cout<<"Exiting the program..!!!"<<endl;

break;

}

}while(ans == 'Y' || ans == 'y');

cout<<endl;

return 0;

}

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

if (s[start] == '\0')

return 0;

return 1 + str_length(s, start + 1); //recursive method to count char from 1 to '\0'

}

int sum(int n) {

if (n == 0)

return 0;

return n + sum(n - 1); //recursive method to calculate sum from 1 to n

}


===============================================================

Sample Output:

==================================================================

Kindly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
c++ Write the following 2 functions and test them. Both of them need to be recursive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // Write a program that determines how many of each type of vowel are in an...

    // Write a program that determines how many of each type of vowel are in an entered string of 50 characters or less. // The program should prompt the user for a string. // The program should then sequence through the string character by character (till it gets to the NULL character) and count how many of each type of vowel are in the string. // Vowels: a, e, i, o, u. // Output the entered string, how many of...

  • C++ code do while loop issue. The code I posted works but when it prompts the...

    C++ code do while loop issue. The code I posted works but when it prompts the user if they want to try against with another sentence, it only takes the Y and goes straight into another loop, without letting me enter a new sentence. I need it to be able to take the Y response from a user and then let me enter another sentence. do { cout<<"Enter a sentence:"; //user input sentence cin.getline(str,80); int wordsCount = 0; for (int...

  • I am creating a recursive function in c++ and have the following code so far. However,...

    I am creating a recursive function in c++ and have the following code so far. However, can you help me fix it so that it doesn't crash when negative numbers are entered. If negative numbers are entered i want to let the user know negative numbers are not accepted and to try again #include <iostream> using namespace std; int multiplication(int x, int y) { int sum = y; //if value of x is 1, then result of x*y will be...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

  • Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks....

    Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks. The first function prints out the numbers from the first argument up to and including the number passed as the second argument counting by the first argument (i.e. if the arguments are 2 and 10, the information below was given: #include <iostream> using namespace std; // function definitions// // main program int main() {   int firstNumber,secondNumber;   char countType, doAgain;   // we will do this...

  • I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

    I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT