C++ Programming Basics tutorials, C++ Programs Examples, Variables, Operators, Comments and Data Types in C++, Keywords in C++, C++ Expressions, Control Structures, Decision Making Structures, Loops(for loop, while loop, Do-while-Loop) in C++ all in cpp programming tutorials.

Interesting exercise. Just to make sure people aren't led astray, I'd like to point out why it should never, ever be done in a "real" program:

1. It will only work with reasonably small integers. If you get an overflow (i.e., var1+var2 >= 2^31), the result is undefined. 2. It won't work for float or double, because floating point addition and subtraction is not perfectly accurate. 3. It won't work for other types. 4. It is hard to read. 5. It is inefficient.

You Are Absolutely Right But This Program IS For Absolute Beginners That Is Why Program Coded In Very Simple Format With Simple Logic So Anyone Can Get The Basic Idea Of Swapping.

I was going to suggest the xor trick (for integer types), because it doesn't have the overflow problem, but after some thought and reading of the spec, I realized:

1. For unsigned integer types, overflow is impossible (because the C++ standard mandates that unsigned arithmetic be done modulo 2**n), so both the addition-subtraction method, and the xor method, will work.

2. For signed types, with some representations it might be possible for the xor method to create invalid values (for example, in signed magnitude or 1s complement representations, depending on how positive and negative zeros are treated) resulting in undefined behavior. With 2s complement representation, both the addition-subtraction method and the xor method will probably work (because the easiest way to implement 2s complement arithmetic is to have it wrap around).

Anyway, anonymous is right, std::swap is always the right answer!

I want a program to search an element in an array and replace every occurrence with 0. Example: Input is 1232472. The places where 2 is present must be made 0 so that we will get 1030470 and it must be swapped as 1347000. How to write a program for that?

Interesting exercise. Just to make sure people aren't led astray, I'd like to point out why it should never, ever be done in a "real" program:

ReplyDelete1. It will only work with reasonably small integers. If you get an overflow (i.e., var1+var2 >= 2^31), the result is undefined.

2. It won't work for float or double, because floating point addition and subtraction is not perfectly accurate.

3. It won't work for other types.

4. It is hard to read.

5. It is inefficient.

Instead:

using std::swap;

swap(var1, var2);

You Are Absolutely Right

DeleteBut This Program IS For Absolute Beginners That Is Why Program Coded In Very Simple Format With Simple Logic So Anyone Can Get The Basic Idea Of Swapping.

Your Points Are Valid For Advanced Level.

Thanks A Lot.. For Giving Your Time.

I was going to suggest the xor trick (for integer types), because it doesn't have the overflow problem, but after some thought and reading of the spec, I realized:

ReplyDelete1. For unsigned integer types, overflow is impossible (because the C++ standard mandates that unsigned arithmetic be done modulo 2**n), so both the addition-subtraction method, and the xor method, will work.

2. For signed types, with some representations it might be possible for the xor method to create invalid values (for example, in signed magnitude or 1s complement representations, depending on how positive and negative zeros are treated) resulting in undefined behavior. With 2s complement representation, both the addition-subtraction method and the xor method will probably work (because the easiest way to implement 2s complement arithmetic is to have it wrap around).

Anyway, anonymous is right, std::swap is always the right answer!

I want a program to search an element in an array and replace every occurrence with 0.

ReplyDeleteExample: Input is 1232472. The places where 2 is present must be made 0 so that we will get 1030470 and it must be swapped as 1347000. How to write a program for that?

#include

Deleteusing namespace std;

void main(void)

{

int x, i,j;

int arr[] = { 1, 2, 3, 2, 4, 7, 2 };

for (i = 0; i < 7; i++)

{

if (arr[i] == 2) arr[i] = 0;

}

for (i = 0; i < 7; i++)

cout << arr[i];

cout << endl;

for (i = 0; i < 7; i++)

{

if (arr[i] == 0)

{

for (j = i; j < 7; j++){

if (arr[j] =! 0){

arr[i] = arr[j];

arr[j] = 0;

break;

}

}

}

}

for (i = 0; i < 7; i++)

cout << arr[i];

system("pause");

}

its not working but you get the idea!!!!!!!!!!!!

Thanks for the program.

ReplyDeleteA very good article. Thanks a lot

ReplyDeleteThanks for such a nice article .

ReplyDeleteswapping of two numbers

Thanks a lot fr d effort sir can i hav more n more exercise on basis of data structure ?

ReplyDelete