-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings&summation.cpp
More file actions
60 lines (51 loc) · 1.77 KB
/
Copy pathstrings&summation.cpp
File metadata and controls
60 lines (51 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Logan Jones
// Filename: hw5.cpp
// ---------------------------------------------------------------------------
// This program explores recursion with the strings and summation
// ---------------------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
int sumRange(int val1, int val2);
void printRevString(string theString, int position);
int main(){
// ----------------------- summation --------------------------------------
int user1;
int user2;
cout << "Please enter your range of integers (on one or more lines)"
<< endl;
cin >> user1 >> user2;
cout << "The sum of all integers from " << user1 << " to " << user2
<< " is " << sumRange(user1, user2) << endl;
// ----------------------- string recursion -------------------------------
string line;
cout << "Please enter a string: ";
cin.ignore(1, '\n');
getline(cin, line);
cout << "The reverse of your input is:" << endl;
printRevString(line, 0);
cout << endl;
return 0;
}
int sumRange(int val1, int val2){
// if equal return value
if (val1 == val2){
return val1;
}
// ensure that val 1 is greater than val 2
if (val1 < val2) {
int tempInt = val1;
val1 = val2;
val2 = tempInt;
}
// return val1 + recursive call to sumrange
return val1 + sumRange(val1-1, val2);
}
void printRevString(string theString, int position){
// convert theString's size to an int and check if larger than current position
if (static_cast<int>(theString.length()) > position){
//recursively call with position plus one and then print out current position
printRevString(theString, position + 1);
cout << theString[position];
}
}