+ 2
Here are a bunch of questions that I don't know how to solve. Could anyone explain these to me?
============= Question 1 : ============= int fact (int n) { if (!n) return 1; return n * fact(n - 1); } int main () { cout << fact (3) - fact (2); } //The output is 4. But how? ============= Question 2: ============= template <class T> /* Why doesn't the parameter list of f(T) have a variable? Shouldn't it be "void f(T x);" instead? */ void f(T) { static int i = 0; cout << ++i; } int main () { f(1); f(1.0); f(1); } //This time the output is 112. Again... I don't know how. ERROR: Max string length
10 Answers
+ 3
1.) the code is
double fun (double a) { static int t; t++;)
see the "t++" part so 0 became 1 in the first iteration
and no it's not an array the value of t is stored in some kind of system memory so it is initialized only once
the next time you increment it it becomes 2
and the next time it becomes 3
note: static variables are initialized only once i.e when they are called the first time
2.) as you said function is called thrice...
but the second time it has a floating value as the argument
so a new static variable is assigned to that particular data type i.e float
now if you call that function with character as the argument
eg: fun(w)
you will get 1 as output
call it once again with char argument
eg: fun(u)
you will get 2 as output
but for an int argument
eg: fun(5)
output = 1
so for each datatype (int,float,char) a new static variable is
initialized
I hope you are clear
if you have any doubt comment it
+ 11
Question1 // Edit: i am poor at c++
** !n means false for every n except 0. [generally 0 represents false and therefore !n is true for 0] **
fact(3)=> 3* fact(2)
fact(2)=>2*fact(1)
fact(1)=>1*fact(0)
fact(0)=> 1//if(!n)
fact(1)= 1*1=1
fact(2)= 2*1=2
fact(3)=3*2=6
fact(3)-fact(2)=6-2=4
+ 2
Thanks Hannah. That was a great explanation! I will be glad if you explain the other ones as well, especially Question no° 3.
+ 2
Q1 factorial of 3 - factorial of 2
= 3*2*1 - 2*1
=6-2
=4
note: if (!n) means if negation of n is 1 which is possible only when n=0
Q2 it's not necessary to have a varible name as we are not dealing with that variable in the function.
each time a different data type is given as arguement, static int becomes 0 for that particular data type.So,
fun(1) will give you 1,
fun(1.0) will give you 1,
fun(2) will give you 2,
fun(8) will give you 3,
it's like calling two functions with with same name with arguments of different data type (function overloading)
eg:
int fun(int)(static int t; t++;}
double fun(double){static int t; t++;}
these functions will give same output as the template
Q3 i'm not sure how that works
+ 2
Q4
first loop iterates 5 times (i < 5 so 5)
second loop iterates 4 times (j<4 so 4)
so total = i * j
= 4 * 5
= 20.
but according to the condition....
if (i>1) continue;
so for i=0 , i=1 the second loop displays the message (4 + 4 =8 times )
but when i >1 i.e for i=3 and so on...
printing of the message is skipped due to "continue" statement
so the message is displayed only 8 times
Q3
for c1 -> A.a=1 A.b=2 B.b=3 B.a=4
for c2 -> A.a=5 A.b=6 B.b=7 B.a=8
so c1.b.a = 4
c1.a.b = 6
6 + 4 =10
Note: B.b is assigned 3 because b is declared first in struct B
+ 1
=============
Question 3:
=============
struct A {
int a;
float b;
};
struct B {
int b;
float a;
};
struct C {
A a; B b;
};
int main () {
C c1 = {1,2,3,4};
C c2 = {5,6,7,8};
cout << c1.b.a + c2.a.b;
} // I don't even remember what this one's output is.
+ 1
=============
Question 4:
=============
How many times will "Hello World!" be printed?
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
if (i > 1)
continue;
cout << "Hello World!\n";
}
}
/* The output is 8. Here's what I think the answer should be: the if statement is true for i = 2, 3 and 4.
Therefore the first loop will iterate 3 times for the condition to be true. The second loop iterate four times for j = 0, 1, 2 and 3. It has four cases. Henceforth, "Hello World!" should be printed out 3*4 i.e. 12 times. But hey! I could also be absolutely wrong. 😄
+ 1
@Sreejith You said that each time a different data type is passed as arguement, static int i becomes 0. Regarding that, I have a few doubts:
First, does static int i store different values for the different types of variables passed as arguements? Doesn't that make i an array?
Second, the first line of code in f() dictates "static int i = 0". It overrides the value of i with 0. And then i is incremented by 1 and the result is printed out to the screen. Now, if the value of i is changed to 0 and 1 is added to it every time the function is called, shouldn't the result always be 1 irrespective of what the value passed argument actually is? Then why is the output 112 and not 111 when the function is called thrice in main()?
P.S. Someone please answer this one, I really need to know!
+ 1
Thanks a lot! Most of it makes sense now. One last thing, in your answer to the third question don't you mean a.a =1, a.b = 2 and so on... instead of A.a=1, B.b=2... because A and B are data structures while a and b are objects of type A and B respectively. Therefore data members a and b of A and B should be called by the objects a and b, right?
0
yeah, you are right
I capitalized A to avoid confusion :)