IQOD #2

Posts 1–15 of 38 · Page 1 of 3
IQOD #2
Again Figure this out without compiler etc. I'm trying to do all the easy ones first

Consider the following program:
Code:
void e(int n)
{
    if(n>0)
    {
     e(--n);
     printf("%d", n);
     e(--n);
    }
}

void main()
{
     int a = 3;
     e(a);
}
What is the output it generates?
I won't post the answers until tomorrow or until it seems "everyone" (WHy,Zeco, Hell_Demon and lala, ya know the cool people ) has had a chance to post their answers
Answer: 12112321121
Sweet lucky me I just happened to be on at this time . I know I got it this time. At least I think I do. God i hate recursion D:!

Wow. I hope I got that right :{. I remember how difficult recursion was in Java when we learned it in school. hopefully I got it :P
well... try again
answer:error probably, cause n has no beginning value and so is the if never executed and in the if block is the only output line

my answer is probably wrong because i dont know what the code does.

what is void?
i think i am not at that chapter yet...
Oh. I get it: void main isn't right. In Java we do void main all the time but in C++ main must return an int! so nothing is printed.
With these problems always assume the code builds & runs.

Void main is totally fine in c++ just like with any other method.
It just means the main function does not return a value.

No correct answer yet

Hint: Just look at function 'e' and imagine passing in the value '3' to start.
Follow what it does and what it prints out
hey oldie

what is void?
what does it do?
Quote Originally Posted by lalakijilp View Post
hey oldie

what is void?
what does it do?
It just means the main function does not return a value.
voorbeeld
Code:
int functie()
{
   //do something
   return 5;  // returns 5
}

void functie2()
{
    //do something
    //doesn't return anything
}

//so you can call
...
int x = functie();
// but to call functie2 you'd just call:
functie2(); //no return value;
basically anything method signature is setup like:
WhatDoesItReturn FunctionName (Arguments)
So if you see void it literally means 'nothing' -> another example:
int DoSomething(); is the same as: int DoSomething(void);
there it basically just says 'no arguments'.

Having a return value for the Main method is only handy if you have some other type of
app calling it and you need to know if it completed successfully or not. in 99% of the time
a void is just fine since you don't care about the return or have nothing to even read that value
with.
i don't understand it yet (language problems)

i just continue with my tut and let this one pass.
ok this better?
Als je 'Void' schrijft betekent het gewoon dat de functie geen waarde terug
geeft.

Voorbeeld: Int optellen(int a, int b); Dit betekent dat de 'optellen' functie een 'Int' terug geeft (en natuurlijk ook 2 integers als argumentent heeft) waar je dan vervolgens iets mee kan doen. Als we dat nu veranderen in: void doeIets(int a, int b); betekent dat gewoon de functie geen waarde terug geeft. Beter zo?
Quote Originally Posted by B1ackAnge1 View Post
ok this better?
Als je 'Void' schrijft betekent het gewoon dat de functie geen waarde terug
geeft.

Voorbeeld: Int optellen(int a, int b); Dit betekent dat de 'optellen' functie een 'Int' terug geeft (en natuurlijk ook 2 integers als argumentent heeft) waar je dan vervolgens iets mee kan doen. Als we dat nu veranderen in: void doeIets(int a, int b); betekent dat gewoon de functie geen waarde terug geeft. Beter zo?
wat bedoel je met waarde teruggeven. waarschijnlijk als je het een klein beetje veranderd dan snap ik het.

en wat moet je met een amerikaanse vlag?
Quote Originally Posted by B1ackAnge1 View Post
With these problems always assume the code builds & runs.

Void main is totally fine in c++ just like with any other method.
It just means the main function does not return a value.

No correct answer yet

Hint: Just look at function 'e' and imagine passing in the value '3' to start.
Follow what it does and what it prints out
Ok. Last try o_O: 1213121
*crosses fingers and prays* :{


I think I thought about it to much in my first answer. Im a 100% sure this is correct! . I think....
Why06: Sorry pal - hint: only 4 digits get printed
outputs 0120

im having a hard time trying to explain it in english

ill try anyway:
the first 0 is because
e(--n); is called before the first n is printed(n is now 2)
then again e(--n) is called before that one is printed(n is now 1)
again (n is now 0)
now its not called and n gets printed as 0
then the print before that gets called(where n was 1) so it prints 1
then the one before that(n was 2) so it prints 2
then the very first e was called but n is 0 there(because of all decrements) so 0 again.
Posts 1–15 of 38 · Page 1 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us