Hi another exercise which im having problems with. In this challenge im given a four digit integer and encrypt each digit as follows. Replace each digit by the sum of that digit plus seven mod 10. Then swap 1st and 3rd digit places and 2nd with 4th. Heres what i got so far. I havent figured out yet how to swap them. Any thoughts on that ? No arrays allowed.

//Deitel problem 2.38
//Encrypted integer

#include <iostream>


using namespace std;

int main(){

	int iCounter, iNumber = 4596,
		iRemainder = 0, iCopy;
	

		iCopy = iNumber;

		for ( iCounter = 0; iCounter <= 1; iCounter++) {

			do {
			
				iRemainder *= 10;

				if ( iCounter == 0) 				
					iRemainder += ((iNumber % 10) + 7) % 10;		
				else
					iRemainder += iNumber % 10;

				iNumber /= 10;				
			
			}while( iNumber != 0 );

			iNumber = iRemainder;
			iRemainder = 0;
		}

		cout << " Old number " << iCopy << " new number " << iNumber << endl;

	return 0;
}

sorry for cross post. I just want to get as much input as possible

if anyone was interested this is what i came up with. Please critique.

//Deitel problem 2.38
//Encrypted integer
#include <iostream>


using namespace std;

int main(){

	int iCounter, iNumber = 4596, iDigit,
		iRemainder = 0, iCopy, iRemOfOriginal = 0;
	

		iCopy = iNumber;

		for ( iCounter = 0; iCounter <= 1; iCounter++) {

			do {
			
				iRemainder *= 10;

				if ( iCounter == 0) 				
					iRemainder += ((iNumber % 10) + 7) % 10;		
				else
					iRemainder += iNumber % 10;

				iNumber /= 10;				
			
			}while( iNumber != 0 );

			iNumber = iRemainder;
			iRemainder = 0;
		}

		//This is where i swap digit places.
		//Find third digit
		
		iDigit = (iNumber % 100) / 10;
	
		do{

			iRemainder = iNumber % 10;
			iRemainder *= 1000;
			iRemainder = iRemainder + iNumber / 10;
			iNumber = iRemainder;

		}while ((iNumber / 1000)  != iDigit );


		cout << " Old number " << iCopy << " new number " << iNumber << endl;

	return 0;
}

My critique would be the following. The code is difficult to read, at least to me. iCounter, iNumber, iRemOfOriginal, etc., these are all variable names that are not obvious to me so it's hard for me to follow the logic.

The task seems fairly straightforward.

  1. Isolate the digits.
  2. Manipulate the digits.
  3. Swap the digits.

Hence a digits[] array seems in order. Isolate them first before you do anything.

int digits[4];
for(int i = 3; i >= 0; i--)
{
    digits[i] = number % 10;
    number = number / 10;
}

Now do any manipulation or swapping with the digits[] array. I imagine all of your code does that, but you seem to have more variables than necessary and I can't match the variable name to the variable's purpose.

Hi, i appreciate the response , but arrays are not allowed to do any manipulations. I thought my variables was meaningful as iNumber stood for the number given to be encrypted and jCounter is how many times i had to go through the cycle, i should have made more comments to explain what takes place where.

>>but arrays are not allowed to do any manipulations.

Not sure what you mean by this. Not allowed by who? Is that a stipulation of the exercise?

for(int i = 0; i < 4; i++)
{
    digits[i] = (digits[i] + 7) % 10;
}

Yes , its a part of the exercise. Since arrays are not covered till later i cant use them.

a=4596;
b=(((a/100)%10)+((a/1000)*10)+((a%10)*100)+(((a/10)%10)*1000));

may work but arrays or strings would be easier.

meant to add

a=b;

can you use vectors?

i'm guessing if he can't use arrays then vectors are off the table.

i'm guessing if he can't use arrays then vectors are off the table.

not necessarily - i got taught vectors before an array

True, vectors are off the table. Anyhow , my code works and produces correct results. However, i over analyzed the exercise. This is what author expected readers to do. To swap the numbers he just wanted us to create 4 variables and use % and / and then just swap the numbers and put it back together. Thanks for the help. I wrote to the author and that was his response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.