Heres a fun challenge for you guys

Pages: 12
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.
1
2
3
4
5
6
7
8
9
10
11
template <typename T>
std::string parenthesize(const T &s,unsigned n){
	return std::string(n,'(')+s+std::string(n,')');
}

float f(unsigned n){
	float r=0;
	for (unsigned a=0;a<n;a++)
		r+=1.0f/(2.0f*a+1.0f);
	return r;
}

What kind of asshole would do this using recursion?
Last edited on
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);
{
}
wow! i'm hearin' echo from hello, LOL :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

void parenthesize(string str, int par) { 

    if(par<1)
        return;
    string s = '(' + str + ')';
    cout << s << '\n';
    parenthesize(s, --par);
}

int main(int argc, char* argv[]) {
    parenthesize("hello", 5);
    return 0;
}
(hello)
((hello))
(((hello)))
((((hello))))
(((((hello)))))
Last edited on
btripp:
Error:8: void value not ignored as it ought to be.
Error:8: void function returning value.
lol palm to face. its because i put return before parenthesize. when it doesnt return my b
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.
my teacher is an asshole!
Well, as a beginner in C++, I perfectly understand Blackcoder's and brtipp's code, but do not understand the code of Helios.

So is using recursion that bad? Why?

EDIT:
Nvm, Helios' code is actually very easy lol..
Last edited on
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.
closed account (S6k9GNh0)
1
2
3
4
5
6
7
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.
Last edited on
helios, why make this a template function?
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.
Last edited on
I'm sick of being asked baby questions like what's an integer.

Good to know I'm not the only one in this situation.
closed account (jLNv0pDG)
int main(int argc, char* argv[]) {

@blackcoder41: Why are there parameters in main?
I guess I'll try doing the other one--in one line, of course! ;)
1
2
3
4
float f( unsigned int n )
{
    return ( n == 1 )? 1 : ( ( ( n % 2 )? -1 : 1 ) * ( 1.0 / ( 1 + ( ( n - 1 ) * 2 ) ) ) ) + f( n - 1 );
}
Last edited on
closed account (jLNv0pDG)
"series"? --is that a variable / function?
Last edited on
"series"? --is that a variable / function?


Oops! I renamed the function from "series" to "f" when I posted it and forgot to change the name in both places.
farcear wrote:
Why are there parameters in main?
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>
using namespace 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
Bv202 wrote:
Well, as a beginner in C++, I perfectly understand Blackcoder's and brtipp's code, but do not understand the code of Helios.
i'm glad, you've figured out helios code
closed account (jLNv0pDG)
so that you could pass arguments to it


Is it not illegal/impossible/undesirable to call main()?

http://www.cplusplus.com/forum/beginner/17885/
Pages: 12