| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"arnuld" |
| Date: |
31 Mar 2007 08:05:21 AM |
| Object: |
Stroustrup 5.9, exercise 4 (page 105) |
this programme compiles but runs into some strange results, a semantic-
BUG is there. i even put some "std::cout" to know what is wrong but it
does something that i did NOT order it to do:
--------------- PROGRAMME -------------------
/* Stroustrup, 5.9 exercise 4
STATEMENT:
write a programme that swaps 2 integers. solve it
using 1st with pointers and then references.
METHOD:
1.) asks the user to input 2 integers
2.) swaps the value using pointers,
prints 2 statements to show what it is doing
3.) prints the SWAPPED values
4.) swaps the vale sagain using References
5.) so after swapping 2 times we get the original input values.
*/
#include<iostream>
void swap_p(int* x, int* y);
void swap_r(int& x, int& y);
int main()
{
using std::cout;
using std::cin;
using std::endl;
int i;
int j;
cout << "Enter 1st integer: ";
cin >> i;
cout << "enter 2nd integer: ";
cin >> j;
cout << "i = " << i << '\t'
<< "j = " << j << endl;
int* pi = &i;
int* pj = &j;
swap_p(pi, pj);
cout << "\nvalues swapped using Poiners\n"
<< "i = " << i << '\t'
<< "j = " << j << endl;
swap_r(i, j);
cout << "\nswappe dusing Refrences\n"
<< "i = " << i << '\t'
<< "j = " << j << endl;
return 0;
}
void swap_p(int* x, int* y)
{
int temp_val = *x;
x = y;
std::cout << "------------------------------\n";
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
*y = temp_val;
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
std::cout << "------------------------------\n";
}
void swap_r(int& x, int& y)
{
int temp_val = x;
x = y;
y = temp_val;
}
-------------- OUTPUT ------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-04.cpp
[arch@voodo tc++pl]$ ./a.out
Enter 1st integer: 3
enter 2nd integer: -2
i = 3 j = -2
------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------
values swapped using Poiners
i = 3 j = 3
swappe dusing Refrences
i = 3 j = 3
[arch@voodo tc++pl]$
------------------------------
x = -2 y = -2
x = 3 y = 3
------------------------------
1st line is fine but why does it change value of "x" in 2nd line.i did
not do anything to "x"
?
.
|
|
| User: "" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 11:13:18 AM |
|
|
On 31 Mar, 14:05, "arnuld" <geek.arn...@gmail.com> wrote:
this programme compiles but runs into some strange results, a semantic-
BUG is there.
void swap_p(int* x, int* y)
{
int temp_val = *x;
x = y;
Think carefully about what type x and y are. And compare this line
with all the other lines of this function.
std::cout << "------------------------------\n";
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
*y = temp_val;
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
std::cout << "------------------------------\n";
}
Hope that helps (it's only a hint, but you'll probably learn better if
you work the rest out for yourself).
Paul.
.
|
|
|
|
| User: "Manuel T" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 09:28:40 AM |
|
|
arnuld wrote:
void swap_p(int* x, int* y)
{
int temp_val = *x;
x = y;
std::cout << "------------------------------\n";
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
*y = temp_val;
std::cout << "x = " << *x << "\ty = " << *y <<'\n';
std::cout << "------------------------------\n";
}
void swap_r(int& x, int& y)
{
int temp_val = x;
x = y;
y = temp_val;
}
Logic errors lie in swap_X functions. Do a little trace on paper and you'll
discovery why this stuff don't work.
Hint: you are working with references/pointers(i.e. memory addresses) and
not with values.
--
Linux engine 2.6.17-11-generic i686 GNU/Linux
.
|
|
|
| User: "arnuld" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 11:43:16 AM |
|
|
On Mar 31, 7:28 pm, Manuel T <man...@t.gov> wrote:
Logic errors lie in swap_X functions. Do a little trace on paper and you'll
discovery why this stuff don't work.
Hint: you are working with references/pointers(i.e. memory addresses) and
not with values.
ok forget about references for the moment. just think about Pointers
only. i came up with this BUGGY solution. it has semantic-BUG. i can
not find out "why it has a bug" except that i have a lot of
*headache* after banging my head with it for 2 hours:
--------- PROGRAMME ------------
/* Stroustrup, 5.9 exercise 4
STATEMENT:
write a programme that swaps 2 integers. solve it
using 1st with pointers and then references.
METHOD:
1.) asks the user to input 2 integers
2.) swaps the value using pointers,
prints 2 statements to show what it is doing
3.) prints the SWAPPED values
4.) swaps the vale sagain using References
5.) so after swapping 2 times we get the original input values.
*/
#include<iostream>
void swap_p(int* x, int* y);
void swap_r(int& x, int& y);
int main()
{
using std::cout;
using std::cin;
using std::endl;
int i;
int j;
cout << "Enter 1st integer: ";
cin >> i;
cout << "enter 2nd integer: ";
cin >> j;
cout << "i = " << i << '\t'
<< "j = " << j << endl;
int* pi = &i;
int* pj = &j;
cout << "pi = " << &i << '\t'
<< "pj = " << &j << endl;
swap_p(pi, pj);
cout << "\nvalues swapped using Poiners\n"
<< "pi = " << pi << '\t'
<< "pj = " << pj << endl;
cout << "VALUES: "
<< "i = " << i << '\t'
<< "j = " << j << endl;
return 0;
}
void swap_p(int* x, int* y)
{
int** tmp_x = &y;
int** tmp_y = &x;
std::cout << "tmp_x = " << tmp_x << '\t'
<< "tmp_y = " << tmp_y << std::endl;
x = *tmp_y;
std::cout << "------------------------------\n";
std::cout << "x = " << x << "\ty = " << y << '\n';
y = *tmp_x;
std::cout << "x = " << x << "\ty = " << y << '\n';
std::cout << "------------------------------\n";
}
--------- OUTPUT -----------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra swap_p.cpp
[arch@voodo tc++pl]$ ./a.out
Enter 1st integer: 3
enter 2nd integer: -3
i = 3 j = -3
pi = 0xbfc4c320 pj = 0xbfc4c31c
tmp_x = 0xbfc4c314 tmp_y = 0xbfc4c310
------------------------------
x = 0xbfc4c320 y = 0xbfc4c31c
x = 0xbfc4c320 y = 0xbfc4c31c
------------------------------
values swapped using Poiners
pi = 0xbfc4c320 pj = 0xbfc4c31c
VALUES: i = 3 j = -3
[arch@voodo tc++pl]$
.
|
|
|
| User: "arnuld" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 11:44:17 AM |
|
|
On Mar 31, 9:43 pm, "arnuld" <geek.arn...@gmail.com> wrote:
On Mar 31, 7:28 pm, Manuel T <man...@t.gov> wrote:
Logic errors lie in swap_X functions. Do a little trace on paper and you'll
discovery why this stuff don't work.
Hint: you are working with references/pointers(i.e. memory addresses) and
not with values.
ok forget about references for the moment. just think about Pointers
only. i came up with this BUGGY solution. it has semantic-BUG. i can
not find out "why it has a bug" except that i have a lot of
*headache* after banging my head with it for 2 hours:
--------- PROGRAMME ------------
/* Stroustrup, 5.9 exercise 4
STATEMENT:
write a programme that swaps 2 integers. solve it
using 1st with pointers and then references.
METHOD:
1.) asks the user to input 2 integers
2.) swaps the value using pointers,
prints 2 statements to show what it is doing
3.) prints the SWAPPED values
4.) swaps the vale sagain using References
5.) so after swapping 2 times we get the original input values.
*/
#include<iostream>
void swap_p(int* x, int* y);
void swap_r(int& x, int& y);
int main()
{
using std::cout;
using std::cin;
using std::endl;
int i;
int j;
cout << "Enter 1st integer: ";
cin >> i;
cout << "enter 2nd integer: ";
cin >> j;
cout << "i = " << i << '\t'
<< "j = " << j << endl;
int* pi = &i;
int* pj = &j;
cout << "pi = " << &i << '\t'
<< "pj = " << &j << endl;
swap_p(pi, pj);
cout << "\nvalues swapped using Poiners\n"
<< "pi = " << pi << '\t'
<< "pj = " << pj << endl;
cout << "VALUES: "
<< "i = " << i << '\t'
<< "j = " << j << endl;
return 0;
}
void swap_p(int* x, int* y)
{
int** tmp_x = &y;
int** tmp_y = &x;
std::cout << "tmp_x = " << tmp_x << '\t'
<< "tmp_y = " << tmp_y << std::endl;
x = *tmp_y;
std::cout << "------------------------------\n";
std::cout << "x = " << x << "\ty = " << y << '\n';
y = *tmp_x;
std::cout << "x = " << x << "\ty = " << y << '\n';
std::cout << "------------------------------\n";
}
--------- OUTPUT -----------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra swap_p.cpp
[arch@voodo tc++pl]$ ./a.out
Enter 1st integer: 3
enter 2nd integer: -3
i = 3 j = -3
pi = 0xbfc4c320 pj = 0xbfc4c31c
tmp_x = 0xbfc4c314 tmp_y = 0xbfc4c310
------------------------------
x = 0xbfc4c320 y = 0xbfc4c31c
x = 0xbfc4c320 y = 0xbfc4c31c
------------------------------
values swapped using Poiners
pi = 0xbfc4c320 pj = 0xbfc4c31c
VALUES: i = 3 j = -3
[arch@voodo tc++pl]$
i think i better learn C first, as a prerequisite for C++
.
|
|
|
| User: "Manuel T" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 11:56:12 AM |
|
|
arnuld wrote:
i think i better learn C first, as a prerequisite for C++
I think it's useful. You just need to know what is a reference or a pointer.
Ask wikipedia for that.
--
Linux engine 2.6.17-11-generic i686 GNU/Linux
.
|
|
|
| User: "arnuld" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 12:12:43 PM |
|
|
On Mar 31, 9:56 pm, Manuel T <man...@t.gov> wrote:
arnuld wrote:
i think i better learn C first, as a prerequisite for C++
I think it's useful. You just need to know what is a reference or a pointer.
Ask wikipedia for that.
(without Wikipedia) i know this:
a Pointer is the memory-address of the thing it points to. e.g
int i = 3;
int* pi = &i;
int** ppi = π
"pi" will contain a value like "0xff733" or "0x2322" etc. which will
be the memory address of "integer i".
"ppi" will also contain address exactly like "pi", the only difference
is it has the address of "pi", the memory location where "pi" is
stored, after all we need some space to hold the pointer.
BUT my experience says knowing that does not give you any knowledge or
power to solve Stroustrup's exercises.
.
|
|
|
| User: "Manuel T" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 12:44:06 PM |
|
|
arnuld wrote:
(without Wikipedia) i know this:
http://en.wikipedia.org/wiki/Pointer
I suggest you to read it.
BUT my experience says knowing that does not give you any knowledge or
power to solve Stroustrup's exercises.
My experience says that you can build everything on knowledge, but nothing
on tutorials.
--
Linux engine 2.6.17-11-generic i686 GNU/Linux
.
|
|
|
|
|
|
|
|
| User: "arnuld" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
31 Mar 2007 11:50:04 PM |
|
|
On Mar 31, 7:28 pm, Manuel T <man...@t.gov> wrote:
Logic errors lie in swap_X functions. Do a little trace on paper and you'll
discovery why this stuff don't work.
OK, i banged my head with this programme on computer but it did not
make any sense. then as you advised, Manual, i wrote that programme on
paper, as a fresh programme, from scratch and it worked the first time
i typed it into Emacs:
---------- PROGRAMME ----------
#include<iostream>
void swap_p(int* x, int* y);
void swap_r(int& x, int& y);
int main()
{
using std::cout;
using std::cin;
using std::endl;
int i;
int j;
cout << "Enter 1st integer: ";
cin >> i;
cout << "enter 2nd integer: ";
cin >> j;
cout << "i = " << i << '\t'
<< "j = " << j << endl;
int* pi = &i;
int* pj = &j;
swap_p(pi, pj);
cout << "\nvalues swapped using Poiners\n"
<< "i = " << i << '\t'
<< "j = " << j << endl;
return 0;
}
void swap_p(int* x, int* y)
{
int tmp_x = *x;
int tmp_y = *y;
*x = tmp_y;
*y = tmp_x;
}
---------- OUTPUT ------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra swap_p.cpp
[arch@voodo tc++pl]$ ./a.out
Enter 1st integer: 3
enter 2nd integer: -3
i = 3 j = -3
values swapped using Poiners
i = -3 j = 3
[arch@voodo tc++pl]$
i wrote it on my own BUT still don't understand why it works:
*x = tmp_y
now
*x = 3
tmp_y = -3
hence, 3 = -3
this does not make any sense to me BUT it works :-(
.
|
|
|
| User: "Ian Collins" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
01 Apr 2007 12:01:53 AM |
|
|
arnuld wrote:
void swap_p(int* x, int* y)
{
int tmp_x = *x;
int tmp_y = *y;
*x = tmp_y;
*y = tmp_x;
}
You can get away without the second temporary (and it might be easier to
understand):
void swap_p(int* x, int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
--
Ian Collins.
.
|
|
|
| User: "arnuld" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
01 Apr 2007 12:51:08 AM |
|
|
On Apr 1, 10:01 am, Ian Collins <ian-n...@hotmail.com> wrote:
You can get away without the second temporary (and it might be easier to
understand):
void swap_p(int* x, int* y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
that looks good.
.
|
|
|
|
|
| User: "Default User" |
|
| Title: Re: Stroustrup 5.9, exercise 4 (page 105) |
01 Apr 2007 01:29:28 PM |
|
|
arnuld wrote:
int i;
int j;
cout << "i = " << i << '\t'
<< "j = " << j << endl;
int* pi = &i;
int* pj = &j;
swap_p(pi, pj);
pi and pj aren't really needed either, you could just call:
swap_p(&i, &j);
Brian
.
|
|
|
|
|
|

|
Related Articles |
|
|