Advertisement
JoshuaNHardison

lab8.cpp

Nov 2nd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cctype>
  4. #include <ctime>
  5. #include <string>
  6.  
  7.  
  8. //function prototypes for the recursive functions
  9. int sumOfNumbers(int);
  10. bool isMember(int*, int, int);
  11. std::string stringReverser(std::string, int);
  12. bool isPalindrome(std::string);
  13. int multiply(int, int);
  14. int recursiveMultiplication(int,int);
  15.  
  16. const int ARRAY_SIZE = 10;
  17.  
  18. int main()
  19. {
  20.     int choice, num, num1, num2;
  21.     int myArray[ARRAY_SIZE];
  22.     srand(time(NULL));
  23.     std::string userString, userStrWithoutSpaces;
  24.    
  25.     do{
  26.  
  27.         std::cout << "\n\nWhat do you want to do?\n";
  28.         std::cout << "\t1.  Sum of Numbers\n";
  29.         std::cout << "\t2.  IsMember Array Function\n";
  30.         std::cout << "\t3.  String Reverser\n";
  31.         std::cout << "\t4.  Palindrome Detector\n";
  32.         std::cout << "\t5.  Recursive Multiplication\n";
  33.         std::cout << "\t6.  End the Program\n";
  34.         std::cout << "CHOOSE 1-6:  ";
  35.         std::cin >> choice;
  36.         while(choice < 1 || choice > 6)
  37.         {
  38.             std::cout << "\nInvalid input!  CHOOSE 1-6:  ";
  39.             std::cin >> choice;
  40.         }
  41.         switch(choice)
  42.         {
  43.             case 1:
  44.                 std::cout << "\n\nSUM OF NUMBERS\n";
  45.                 std::cout << "Enter an integer:  ";
  46.                 std::cin >> num;
  47.                 std::cout << "\nThe result is: " << sumOfNumbers(num) << std::endl; //print out the result of the sumOfNumbers function here
  48.                
  49.                 break;
  50.                
  51.                
  52.             case 2:
  53.                 for(int x=0; x<ARRAY_SIZE; x++)
  54.                 {
  55.                     myArray[x] = (rand()%100)+1;
  56.                 }
  57.                 std::cout << "\n\nISMEMBER ARRAY FUNCTION\n";
  58.                 std::cout << "Enter an integer:  ";
  59.                 std::cin >> num;
  60.                 std::cout << "\nHere are the array values:  ";
  61.                 for(int x=0; x<ARRAY_SIZE; x++)
  62.                 {
  63.                     std::cout << myArray[x] << " ";
  64.                 }
  65.                 //print if the value that the user entered is in the array or not here
  66.                 if(isMember(myArray, num, ARRAY_SIZE))
  67.                     std::cout << "\nYour integer " << num << " is in the array." << std::endl;
  68.                 else
  69.                     std::cout << "\nYour integer " << num << " is not in the array." << std::endl;
  70.                
  71.                 break;
  72.                
  73.                
  74.             case 3:
  75.                 std::cout << "\n\nSTRING REVERSER\n";
  76.                 std::cout << "Enter a string and I will reverse it:  ";
  77.                 std::cin.ignore();
  78.                 getline(std::cin, userString);
  79.                 //call string reverser function here
  80.                 std::cout << stringReverser(userString, ARRAY_SIZE) << std::endl; //prints with a question mark before
  81.                
  82.                 break;
  83.                
  84.                
  85.             case 4:
  86.                 std::cout << "\n\nPALINDROME DETECTOR\n";
  87.                 std::cout << "Enter a string and I will tell you if it is a palindrome:  ";
  88.                 std::cin.ignore();
  89.                 getline(std::cin, userString);
  90.                
  91.                 //change string to be all uppercase
  92.                 for(int x=0; x<userString.length(); x++)
  93.                 {
  94.                     userString[x] = toupper(userString[x]);
  95.                 }
  96.                
  97.                 //remove spaces from string
  98.                 userStrWithoutSpaces = userString;
  99.                 for(int x=0; x<userStrWithoutSpaces.size(); x++)
  100.                 {
  101.                     if(userStrWithoutSpaces[x] == ' ')
  102.                         userStrWithoutSpaces.erase(x, 1);
  103.                 }
  104.                 //print out whether the user's string is a palindrome or not here.
  105.                 //when you print out the user's string, print out the uppercase version that doesn't have the spaces removed.
  106.                 if (!isPalindrome(userString))
  107.                 {
  108.                     std::cout << "Your string " << userString <<" is a palindrome." << std::endl;
  109.                     std::cout << "Reversed: " << stringReverser(userString,userString.length()) << std::endl;
  110.                 }
  111.                 else
  112.                     std::cout << "Your string is not a palindrome." << std::endl;
  113.                 break;
  114.                
  115.                
  116.             case 5:
  117.                 std::cout << "\n\nRECURSIVE MULTIPLICATION\n";
  118.                 std::cout << "Enter in the first integer:  ";
  119.                 std::cin >> num1;
  120.                 std::cout << "\nEnter in the second integer:  ";
  121.                 std::cin >> num2;
  122.                 //print out the value returned from the multiply function here
  123.                 std::cout << "\nThe result is: " << recursiveMultiplication(num1, num2) << std::endl;
  124.                
  125.                 break;
  126.                
  127.         }      
  128.     }while(choice != 6);
  129.    
  130.     std::cout << "\n\nGOODBYE!\n\n";
  131.     return 0;
  132. }
  133.  
  134.  
  135. //implement the five recursive functions below!!!!
  136. //done
  137.  
  138. int sumOfNumbers(int n)
  139. {
  140.     if(n!=0)
  141.     {
  142.     return n+sumOfNumbers(n-1);
  143.     }
  144.     return 0;
  145. }
  146.  
  147. bool isMember(int a[],int val,int size)
  148. {
  149.     if(size<1)
  150.     return false;
  151.     else if(a[size]==val)
  152.     return true;
  153.     else
  154.     return isMember(a,val,size-1);
  155. }
  156.    
  157. std::string stringReverser(std::string s,int n)
  158. {
  159.     if(n==0)
  160.     return "";
  161.     std::string last(1,s[n-1]);
  162.     std::string reversed=stringReverser(s,n-1);
  163.     return last+reversed;
  164. }
  165.  
  166. bool isPalindrome(std::string str)
  167. {
  168.     if(str.length() <= 1)
  169.     return true;
  170.     if(str[0] == str[(str.length()-1)])
  171.     {
  172.     return isPalindrome(str.substr(1,str.length()-1));
  173.     }
  174.     else
  175.     return false;
  176. }
  177.  
  178. int recursiveMultiplication(int x,int y)
  179. {
  180.     static int product=0,i=0;
  181.     if(i<x)
  182.     {
  183.     product=product+y;
  184.     i++;
  185.     recursiveMultiplication(x,y);
  186.     }
  187.     return product;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement