3. Write a C++ recursive function that takes a string and reverse the string.
4. Write a C++ recursive function that takes an array of words and returns an array
that contains all the words capitalized.
5. Write a linear search C++ program that searches for a number in a two
dimensional array.
6. What is each on the following functions going to achieve:
a)
int fun(int x, int y)
{
if (x == 0)
return y;
else
return fun(x - 1, x + y);
}
b)
int fun(int a, int t)
{
if (a == 0)
return t;
t = (t * 10) + (a % 10);
return fun(a / 10, t);
}
6.a: this function finds sum y and all natural numbers with x inclusive
(for example, x = 3, y = 5. The result of function is 1 + 2 + 3 + 5 = 11)
6.b: this function ascribes the inverted a to t at the back
(for example, a = 1092, y = 532. The result of function is 5322901)
# include <string>
# include <cctype>
# include <vector>
std::string reverse(std::string input) {
if (!input.empty()) {
auto buff = input.back();
input.pop_back();
return buff + reverse(input);
}
else return "";
}
std::vector<std::string> capitalize(std::vector<std::string> input) {
if (input.size() > 1) {
std::string buf = static_cast<char>(std::toupper(input.back()[0])) + input.back();
input.pop_back();
std::vector<std::string> output(capitalize(input));
output.push_back(buf);
return output;
}
else {
input[0][0] = static_cast<char>(std::toupper(input[0][0]));
return input;
}
}
std::pair<int, int> linear_search(std::vector<std::vector<int>> mass, int desired_num) {
int i0, i1;
for (i0 = 0; i0 < mass.size(); ++i0)
for (i1 = 0; i1 < mass[i0].size(); ++i1)
if (mass[i0][i1] == desired_num) return std::pair<int, int>(i0, i1);
return std::pair<int, int>(i0 + 1, i1 + 1);
}
Comments
Leave a comment