Stroustrup 5.9, exercise 4 (page 105)

A

arnuld

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"

?
 
M

Manuel T

arnuld said:
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.
 
G

gw7rib

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.
 
A

arnuld

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]$
 
A

arnuld

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++
 
M

Manuel T

arnuld said:
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.
 
A

arnuld

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;

"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.
 
A

arnuld

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 :-(
 
I

Ian Collins

arnuld said:
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;
}
 
A

arnuld

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top