Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cctype>
- #include <ctime>
- #include <string>
- //function prototypes for the recursive functions
- int sumOfNumbers(int);
- bool isMember(int*, int, int);
- std::string stringReverser(std::string, int);
- bool isPalindrome(std::string);
- int multiply(int, int);
- int recursiveMultiplication(int,int);
- const int ARRAY_SIZE = 10;
- int main()
- {
- int choice, num, num1, num2;
- int myArray[ARRAY_SIZE];
- srand(time(NULL));
- std::string userString, userStrWithoutSpaces;
- do{
- std::cout << "\n\nWhat do you want to do?\n";
- std::cout << "\t1. Sum of Numbers\n";
- std::cout << "\t2. IsMember Array Function\n";
- std::cout << "\t3. String Reverser\n";
- std::cout << "\t4. Palindrome Detector\n";
- std::cout << "\t5. Recursive Multiplication\n";
- std::cout << "\t6. End the Program\n";
- std::cout << "CHOOSE 1-6: ";
- std::cin >> choice;
- while(choice < 1 || choice > 6)
- {
- std::cout << "\nInvalid input! CHOOSE 1-6: ";
- std::cin >> choice;
- }
- switch(choice)
- {
- case 1:
- std::cout << "\n\nSUM OF NUMBERS\n";
- std::cout << "Enter an integer: ";
- std::cin >> num;
- std::cout << "\nThe result is: " << sumOfNumbers(num) << std::endl; //print out the result of the sumOfNumbers function here
- break;
- case 2:
- for(int x=0; x<ARRAY_SIZE; x++)
- {
- myArray[x] = (rand()%100)+1;
- }
- std::cout << "\n\nISMEMBER ARRAY FUNCTION\n";
- std::cout << "Enter an integer: ";
- std::cin >> num;
- std::cout << "\nHere are the array values: ";
- for(int x=0; x<ARRAY_SIZE; x++)
- {
- std::cout << myArray[x] << " ";
- }
- //print if the value that the user entered is in the array or not here
- if(isMember(myArray, num, ARRAY_SIZE))
- std::cout << "\nYour integer " << num << " is in the array." << std::endl;
- else
- std::cout << "\nYour integer " << num << " is not in the array." << std::endl;
- break;
- case 3:
- std::cout << "\n\nSTRING REVERSER\n";
- std::cout << "Enter a string and I will reverse it: ";
- std::cin.ignore();
- getline(std::cin, userString);
- //call string reverser function here
- std::cout << stringReverser(userString, ARRAY_SIZE) << std::endl; //prints with a question mark before
- break;
- case 4:
- std::cout << "\n\nPALINDROME DETECTOR\n";
- std::cout << "Enter a string and I will tell you if it is a palindrome: ";
- std::cin.ignore();
- getline(std::cin, userString);
- //change string to be all uppercase
- for(int x=0; x<userString.length(); x++)
- {
- userString[x] = toupper(userString[x]);
- }
- //remove spaces from string
- userStrWithoutSpaces = userString;
- for(int x=0; x<userStrWithoutSpaces.size(); x++)
- {
- if(userStrWithoutSpaces[x] == ' ')
- userStrWithoutSpaces.erase(x, 1);
- }
- //print out whether the user's string is a palindrome or not here.
- //when you print out the user's string, print out the uppercase version that doesn't have the spaces removed.
- if (!isPalindrome(userString))
- {
- std::cout << "Your string " << userString <<" is a palindrome." << std::endl;
- std::cout << "Reversed: " << stringReverser(userString,userString.length()) << std::endl;
- }
- else
- std::cout << "Your string is not a palindrome." << std::endl;
- break;
- case 5:
- std::cout << "\n\nRECURSIVE MULTIPLICATION\n";
- std::cout << "Enter in the first integer: ";
- std::cin >> num1;
- std::cout << "\nEnter in the second integer: ";
- std::cin >> num2;
- //print out the value returned from the multiply function here
- std::cout << "\nThe result is: " << recursiveMultiplication(num1, num2) << std::endl;
- break;
- }
- }while(choice != 6);
- std::cout << "\n\nGOODBYE!\n\n";
- return 0;
- }
- //implement the five recursive functions below!!!!
- //done
- int sumOfNumbers(int n)
- {
- if(n!=0)
- {
- return n+sumOfNumbers(n-1);
- }
- return 0;
- }
- bool isMember(int a[],int val,int size)
- {
- if(size<1)
- return false;
- else if(a[size]==val)
- return true;
- else
- return isMember(a,val,size-1);
- }
- std::string stringReverser(std::string s,int n)
- {
- if(n==0)
- return "";
- std::string last(1,s[n-1]);
- std::string reversed=stringReverser(s,n-1);
- return last+reversed;
- }
- bool isPalindrome(std::string str)
- {
- if(str.length() <= 1)
- return true;
- if(str[0] == str[(str.length()-1)])
- {
- return isPalindrome(str.substr(1,str.length()-1));
- }
- else
- return false;
- }
- int recursiveMultiplication(int x,int y)
- {
- static int product=0,i=0;
- if(i<x)
- {
- product=product+y;
- i++;
- recursiveMultiplication(x,y);
- }
- return product;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement