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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//function prototypes
void reverseDisplay(const string &s);
int largest(int *list, int highest_index);
void reverseDisplay(const string &s, int high);
int main()
{
cout << "test run" << endl;
//Test Largest function
const int SIZE = 10;
int listOfInts[SIZE] = {1,2,5,7,3,4,9,0,6}; //an array of integers
cout << "\n\nThe largest integer in the list is " << largest(listOfInts, 9) << endl;
//Test function reverseDisplay
cout << "Please enter a string... " << endl;
string sreverse;
getline(cin, sreverse); //asks user for a string
reverseDisplay(sreverse); //sends string to function for reversing
system ("pause");
return 0;
}
//FUNCION DEFINITIONS
//function that returns the largest integer in an array
int largest(int *list, int highest_index)
{
static int max;
for(int i = 0;i != highest_index ;)
{
if(list[i] > list[highest_index])
{
max = list[i];
largest(list, highest_index -1);
}
else if(list[i] < list[highest_index])
{
max = list[highest_index];
i++;
}
}
return max;
}
//function that reverses the order of a string
void reverseDisplay(const string &s, int high) //helper reverse function
{
//create a new string to take the reverse of string s
string s2;
//loop to assign reversed values to s2
for(int i = 0, j = high; ; i++)
{
if(i == high)
cout << "The string reversed is " << s2 << " .";
//reverse order of s and assign it to j
else
{
s2[j] = s[i];
reverseDisplay(s, high -1);
}
}
}
void reverseDisplay(const string &s) //reverse display function
{
reverseDisplay(s, s.size() - 1);
}
|