On my first csci test dealing with recursion and some other stuff I got a recursive problem that was the following.
Write a recursive function that takes in a course name and an integer(# of parenthesies). Have the function output the results to the screen. Example:
parenthesize("CSCI", 3);
the output of the above would be (((CSCI)))
another example would be
parenthesize("Bobby", 5);
output: (((((Bobby)))))
ok that was the first recursive problem on our first exam.
On the Final exam we had another recursive problem which was to calculate the following.
1+ (1/3) - (1/5) + (1/7) ..... (1/2n -1) // the series alternates.
The function would take an integer and return a float as the answer
For the above problems I had to do them within an hour but there were also 4 other problems on the test. This class was the second one for c++ at my school. The first test covered recursion, pointers, linked lists,pointers inside of classes (copy constructor, destructor all that fun stuff).
All of our tests are handwritten in 1 hour 15 mins to write programs. THere were absolutely no code correction problems , what is a string any of that stuff.
I would like to see what some of you guys come up with for these two above problems. I had to do them on the test in about 10-15 mins roughly. Just trying to give some of you guys a challenge. Have fun with it guys.
My teacher lol. I think he prides himself on making problems like that. Pretty much useless problems. I made an A in both of the programming classes but one of few. I think he tries to weed people out with problems like that. He succeeds too. We had 20 kids in the class at the beginning and ended up with 7 that passed.
heres how i did my parenthesize on the test
1 2 3 4 5 6 7 8 9 10 11
void parenthesize(string name, int num)
{
if(num < 1)
cout << name << endl;
else
{
name = '(' + name + ')';
return parenthesize(name, num - 1);
{
}
My teacher lol. I think he prides himself on making problems like that. Pretty much useless problems. I made an A in both of the programming classes but one of few. I think he tries to weed people out with problems like that. He succeeds too. We had 20 kids in the class at the beginning and ended up with 7 that passed.
Sadly, there are many teachers like that, and frankly it's a downright pathetic attitude for a teacher to have.
It's not bad. The problem is that recursive solutions are always more expensive than iterative ones. For one, because in languages like C and C++, the stack is limited. That imposes a practical limit on the input before the program fails, and recursive algorithms don't fail nicely, either.
Whenever possible, an iterative algorithm should be chosen over a recursive algorithm.
template <typename T>
std::string parenthesize(const T &s,unsigned n){
return std::string(n,'(') //Create string with n number of parenthesis
+s //Append original string.
+std::string(n,')'); //Append n number of parenthesis on the right side.
//And then return the finished string via return function.
}
This should help those who didn't understand helios's code at first.
helios, why make this a template function?
EDIT: The reason I ask is since the only thing that can append like this is a string, I'm confused as to why we need to ask the person using the function for what is most likely going to be an std::string.
I'd love to take test like this. I'm sick of being asked baby questions like what's an integer. I also laugh when I write a 2 page essay on it because I'm bored.
So that if the user passes a string literal or a char array, an extra std::string isn't unnecessarily constructed. Although right now I'm wondering if it would make the call ambiguous. I'd have to try it.
so that you could pass arguments to it, for example if you ever play counter-strike, it passes arguments notify half life to directly jump to counter-strike mode... the same with warcraft 3, it passes -windows for it to start in window mode. here's a sample code..
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main(int argc, char* argv[]) {
if(argc>1) {
cout << "you've pass \"" << argv[1] << "\" as an argument\n";
cout << "do something about the arguments.\n";
cout << "special execution or whatever..\n";
} else {
cout << "execute normally\n";
}
return 0;
}
run this in commad prompt:
>program.exe
[output]
execute normally
>program.exe hello
[output]
you've pass "hello" as an argument
do something about the arguments
special execution or whatever..
note: don't type the > symbol