Advertisement
Limited_Ice

Recursive Array Sum

Jun 20th, 2020
1,416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. //  This program uses iteration to sort integers entered by the user into an array and recursion find their sum
  2. //  David Hawkins
  3. //  06/19/2020
  4. ///////////////////////////////////////////////////////////////////////////////////////////
  5. //  Header Files
  6.  
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <string>
  10. #include <sstream>
  11.  
  12. using namespace std;
  13.  
  14. ///////////////////////////////////////////////////////////////////////////////////////////
  15. //  Function Prototypes
  16.  
  17. void intro(const int);  //  This creates a nice header
  18. int getlen(string, int*);   //  This gets the length of the array needed hold the numbers in the input after filtering.
  19. int getsum(int*, int);  //  This function takes the filtered array and calculates the sum.
  20. void makarr(string, int*, int*);    //  This function filters out the spaces and non integer data in the input string to make the array
  21.  
  22. ///////////////////////////////////////////////////////////////////////////////////////////
  23. //  Main Function
  24.  
  25. int main() {
  26.  
  27.  
  28.     const int width = 80;
  29.     string input;
  30.     int length;
  31.     int *arr = nullptr;
  32.     int answer = 0;
  33.  
  34.         intro(width);
  35.         cout << "\nEnter a bunch of integers seperated by anything\n\npress enter when finished\n" << endl;
  36.         getline(cin, input);
  37.  
  38.         length = input.length();                //This statement set the length to a value that works for the getlen() function
  39.         length = getlen(input, &length);        //The getlen() function changes length to the lenth of the int array.
  40.  
  41.         arr = new int[length];                         
  42.         makarr(input, arr, &length);
  43.  
  44.         answer = getsum(arr, length);
  45.  
  46.         cout << endl;
  47.         cout << setw(width) << setfill('*') << '*' << endl;
  48.         cout << "\nThe sum of the numbers your entered is " << answer << "." << endl;
  49.         cout << setw(width) << setfill('*') << '*' << endl;
  50.  
  51.     return 0;
  52. }
  53. ///////////////////////////////////////////////////////////////////////////////////////////
  54. //  Function Definitions
  55.  
  56. void intro(const int x) {
  57.  
  58.     string pad;
  59.     string intro = "This Program Finds The Sum Of An Array";
  60.     int cent = (80 - intro.length()) / 2;
  61.     for (int i = 0; i < cent; i++)
  62.         pad += ' ';
  63.  
  64.     cout << setw(x) << setfill('*') << '*' << endl;
  65.     cout << setfill(' ') << right << pad << intro << endl;
  66.     cout << setw(x) << setfill('*') << '*' << endl;
  67.  
  68. }
  69.  
  70. int getlen(string test, int* strlen) {
  71.  
  72.     stringstream str;
  73.     str.str(test);
  74.     int count = 0;
  75.     char ch = str.peek();
  76.  
  77.     for (int i = 0; i < *strlen; i++) {         //***************************************************************
  78.         if (isdigit(ch) || ch == '-') {         //  This for loop counts the number of times a digit is found   *
  79.             count++;                            //  that is not preceded by another digit.  It uses the length  *
  80.             ch = str.peek();                    //  of the input strign as a limit.                             *
  81.             while (isdigit(ch))                 //                                                              *
  82.                 ch = str.get();                 //***************************************************************
  83.         }
  84.         else
  85.             ch = str.get();
  86.     }
  87.  
  88.     return count;
  89. }
  90.  
  91. void makarr(string test, int* temp, int* length) {
  92.  
  93.     stringstream str;
  94.     str.str(test);
  95.     int count = 0;
  96.     char ch = str.peek();
  97.        
  98.     while (count < *length) {
  99.             ch = str.peek();
  100.             if (isdigit(ch)) {                  //***************************************************************
  101.                 str >> temp[count];             //  This if statement checks for positive integers to enter     *
  102.                 count++;                        //  into the array arr for the getsum() function.               *
  103.                 ch = str.peek();                //***************************************************************      
  104.             }
  105.  
  106.             else if (ch == '-') {       //***************************************************************
  107.                 str >> temp[count];     //  This if statements checks for negative integers to enter    *
  108.                 count++;                //  into the array arr.                                         *
  109.                 ch = str.peek();        //***************************************************************
  110.             }
  111.             else                        //***************************************************************
  112.             ch = str.get();             //  This statements skips past irrelevant characters.           *
  113.     }                                   //***************************************************************
  114. }
  115.  
  116. int getsum(int* temp, int count) {
  117.  
  118.     int hold;
  119.     count--;
  120.     if (count > 0) {                                //***************************************************************
  121.         hold = temp[count] + getsum(temp, count);   //  This calls the function on itself until it reaches the first*
  122.         return hold;                                //  entry in the arr array.                                     *
  123.     }                                               //***************************************************************
  124.  
  125.     else
  126.         hold = temp[count];     //***************************************************************
  127.         return hold;            //  This handles the base case when the hold value is the first *
  128.                                 //  value stored in the arr array.                              *
  129.     }                           //***************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement