Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program uses iteration to sort integers entered by the user into an array and recursion find their sum
- // David Hawkins
- // 06/19/2020
- ///////////////////////////////////////////////////////////////////////////////////////////
- // Header Files
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <sstream>
- using namespace std;
- ///////////////////////////////////////////////////////////////////////////////////////////
- // Function Prototypes
- void intro(const int); // This creates a nice header
- int getlen(string, int*); // This gets the length of the array needed hold the numbers in the input after filtering.
- int getsum(int*, int); // This function takes the filtered array and calculates the sum.
- void makarr(string, int*, int*); // This function filters out the spaces and non integer data in the input string to make the array
- ///////////////////////////////////////////////////////////////////////////////////////////
- // Main Function
- int main() {
- const int width = 80;
- string input;
- int length;
- int *arr = nullptr;
- int answer = 0;
- intro(width);
- cout << "\nEnter a bunch of integers seperated by anything\n\npress enter when finished\n" << endl;
- getline(cin, input);
- length = input.length(); //This statement set the length to a value that works for the getlen() function
- length = getlen(input, &length); //The getlen() function changes length to the lenth of the int array.
- arr = new int[length];
- makarr(input, arr, &length);
- answer = getsum(arr, length);
- cout << endl;
- cout << setw(width) << setfill('*') << '*' << endl;
- cout << "\nThe sum of the numbers your entered is " << answer << "." << endl;
- cout << setw(width) << setfill('*') << '*' << endl;
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////
- // Function Definitions
- void intro(const int x) {
- string pad;
- string intro = "This Program Finds The Sum Of An Array";
- int cent = (80 - intro.length()) / 2;
- for (int i = 0; i < cent; i++)
- pad += ' ';
- cout << setw(x) << setfill('*') << '*' << endl;
- cout << setfill(' ') << right << pad << intro << endl;
- cout << setw(x) << setfill('*') << '*' << endl;
- }
- int getlen(string test, int* strlen) {
- stringstream str;
- str.str(test);
- int count = 0;
- char ch = str.peek();
- for (int i = 0; i < *strlen; i++) { //***************************************************************
- if (isdigit(ch) || ch == '-') { // This for loop counts the number of times a digit is found *
- count++; // that is not preceded by another digit. It uses the length *
- ch = str.peek(); // of the input strign as a limit. *
- while (isdigit(ch)) // *
- ch = str.get(); //***************************************************************
- }
- else
- ch = str.get();
- }
- return count;
- }
- void makarr(string test, int* temp, int* length) {
- stringstream str;
- str.str(test);
- int count = 0;
- char ch = str.peek();
- while (count < *length) {
- ch = str.peek();
- if (isdigit(ch)) { //***************************************************************
- str >> temp[count]; // This if statement checks for positive integers to enter *
- count++; // into the array arr for the getsum() function. *
- ch = str.peek(); //***************************************************************
- }
- else if (ch == '-') { //***************************************************************
- str >> temp[count]; // This if statements checks for negative integers to enter *
- count++; // into the array arr. *
- ch = str.peek(); //***************************************************************
- }
- else //***************************************************************
- ch = str.get(); // This statements skips past irrelevant characters. *
- } //***************************************************************
- }
- int getsum(int* temp, int count) {
- int hold;
- count--;
- if (count > 0) { //***************************************************************
- hold = temp[count] + getsum(temp, count); // This calls the function on itself until it reaches the first*
- return hold; // entry in the arr array. *
- } //***************************************************************
- else
- hold = temp[count]; //***************************************************************
- return hold; // This handles the base case when the hold value is the first *
- // value stored in the arr array. *
- } //***************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement