|
-
March 6th, 2003, 05:09 AM
#1
some questions
Dear Gurus
i came across some questions please help me to understand those questions:
1>
int x;
while(x<100)
{
cout<<x;
x++;
}
2>
void afunction()
{
if(1)
{
break;
a_function();
cout<<"Err";
}
}
3>
void afunction(int *x)
{
x=new int;
*x=12;
}
int main()
{
int v=10;
afunction(&v);
cout<<v;
}
4>
Would you rather wait for the results of a quicksort, a linear search, or a bubble sort on a 200000 element array?
Quicksort
Linear Search
Bubble Sort
5> what is difference between endl and /n
6> what is the order in which the following is executed
!(1 &&0 || !1)
!(1&&1||1&&0)
i think answer should be
1>
0 1 2 upto 99
2>
exit from if block
3>
12
4>
no guesses
5>no difference
6>??
the answers which the site from where i took these question are different,please help me to understand the answer
thanks in advance
bye
vishal sharma
-
March 6th, 2003, 05:34 AM
#2
1. Note that x is not initialised.
2.
3. Consider for a moment the difference between pass-by-value and pass-by-pointer. How many "*"s do you need to allow a change within the function to get outside the function? If I have "f(int i)", will any change to i from within f() be seen after f() returns? If I have "f(int * i)", will a change to *i be seen outside the function? What about a change to i?
4. Look up descriptions of the various types of sort.
5. endl is a stream manipulator - it is a class-type object. '\n' is just a character. What does that imply for endl?
6. Read up on operator precedence and evaluation order.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 6th, 2003, 05:55 AM
#3
dear graham
thanks for ur reply but according to me for the question number 3
as we are using
f(int *x)
which i thinks is passing by reference so value should be 12 and not 10 as mentioned in the reference site?
and for question 2
the answer is complier error but my thinking is that there is no error
i am not getting how there is error
vishal
-
March 6th, 2003, 07:01 AM
#4
Question 2 - what does break break out of? Hint: Is an if statement a loop?
Question 3 - Read my hints again. If I have f(int* i), and inside that I assign a value to i (not *i), does the change in i get seen outside the function? For example:
Code:
void f(int* i)
{
i = new int;
}
int main()
{
int *p = 0;
f(p);
}
After the call to f(), does p point to valid memory, or is its value 0? Compare that with
Code:
void f(int i)
{
i = 10;
}
int main()
{
int i = 0;
f(i);
}
After the call to f(), does i have the value 0 or 10?
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 6th, 2003, 07:22 AM
#5
graham
thanks for helping me to understand the question number 2
but sorry still for question number 3
suppose my code is like this
int f(int *i)
{
*i = 10;
}
int main()
{
int n;
n = 1;
cout<<n;
f(n);
cout<<n;
return 0;
}
according to me for this answer should be
1
10
as i am passing as reference
now is using
i = new int;
makes some difference
as far as ur question is concerned i think in case 2 answer will be 0
and for case 1 answer (should?) be some garbage value
i am not sure...
sorry for troubling u but please continue to help me..
-
March 6th, 2003, 12:24 PM
#6
OK.
When you write a definition like:
Code:
void f(int i)
{
i = 10;
}
You are declaring a local variable called i in the function f(). When you call f(), that local variable is initialised with whatever value you give as an argument to the call. So, if you call f(n), say, then i is initialised with the current value of n. Note: this means that i inside the function is a copy of n outside the function. If you modify i, you are changing the copy not the original. So, if I have
When it first enters the function f(), i has the value 2 (a copy of the argument). f() then changes the value of its local variable. But this does not change n, it only changes the copy so, after the call, n still has the value 2.
Similarly, if I now have
Code:
void g(int *p)
{
*p = 10;
}
If I call g() with a pointer-to-int, the variable p (local to g()), is initialised with that value - it's a copy again. Now, because we're dealing with pointers, the original and the copy both point to the same thing. Therefore, if I modify the pointed-to object with either the original or the copy, it will be modified.
So,
after the call to g(), n has the value 10 because we changed it via a copy of its address. Note the difference between f(n) and g(&n). When we call g(), the argument is &n, not n. That's because we don't give n to g(), we give the address of n (i.e. a pointer to n).
OK, now let's consider
Code:
void h(int *p)
{
p = new int;
*p = 10;
}
int main()
{
int n = 2;
h(&n);
}
When we call h(), the variable p (local to h()) is intialised with a copy of the argument (the address of n). So, on entry to h(), p points to n and we could change the value of n by assigning to *p. However, what we do is to change the value of p by assigning to it the address of some newly allocated memory. Remember, we are assigning to a copy - the original value (the address of n) does not get changed. We now put the value 10 into that newly-allocated bit of memory (not into n). When h() exits, the variable p is lost and we can no longer refer to the memory that we new'd in the function (this is called a memory leak - memory has been allocated but we don't know where it is any more). But, because h() changed p, n in main() was unaffected by anything we did to *p - p no longer pointed at n.
Now to take your last bit of code:
Code:
int f(int *i)
{
*i = 10;
}
int main()
{
int n;
n = 1;
cout<<n;
f(n);
cout<<n;
return 0;
}
This will not compile. the function f() wants a pointer-to-int as its argument. When you call it, you are supplying an int. The two are not compatible, so the compiler flags an error.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 7th, 2003, 05:58 AM
#7
Thanks graham
now i am able to get what u intented
graham i like to be expert in c/c++ like u,give me any suggestion...
-
March 7th, 2003, 09:28 AM
#8
Time.........
I started with FORTRAN 20+ years ago, moved on to C about 15 years ago, then C++ about 8 years ago. Read good books and listen to people who know. There's no magic, only experience.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
March 8th, 2003, 07:44 AM
#9
again a small question
Graham
is a comma operator a LHS or RHS operator
i mean to say
if a = 10,20,30,40,50
then a is 10 or 50?
i think it should be 50???is it?
vishal sharma
-
March 8th, 2003, 09:22 AM
#10
Directly out of MSDN:
The comma operator has left-to-right associativity. Two expressions separated by a comma are evaluated left to right. The left operand is always evaluated, and all side effects are completed before the right operand is evaluated.
Consider the expression
e1 , e2
The type and value of the expression are the type and value of e2; the result of evaluating e1 is discarded. The result is an l-value if the right operand is an l-value.
graham i like to be expert in c/c++ like u,give me any suggestion...
Trying to find out answers by yourself will gain you experience.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|