Please to check the following solution.

Aug 24, 2018 at 1:19am

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*HEllo everybody. i would like to see your recommendations and your quotes.

how can i optimize this code?
what is wrong into the code?
what can it be improved?

this is the problem
(Cryptography) A company wants to transmit information across the telephone, but it worries their telephones could  be controlled. All the information is transmitted as points of four digits. The company has asked you to write a program that codes its information, so that these could be transmitted by more safet. Your program must read a number of four digits introduced by the user and to code it of the following way: replace every digit (the result of adding 7 to the digit) module 10. Then exchange the first digit with the third , and exchange the second digit with the quarter. 
Print the ciphered point.
*/

/*this is my solution*/
///Criptografía, ejercicio 4.34... Codificar
///Luis Fernando Pinzon.
///14/08/2018

#include <iostream>

using std::cout;
using std::cin;
using std::endl;


int main(int argc, char** argv)
{
	//variables
	
	int digitOne = 0;
	int digitTwo = 0;
	int digitThree = 0;
	int digitFour = 0;
	int digitAmount = 7;
	int amountOne = 0;
	int amountTwo = 0;
	int amountThree = 0;
	int amountFour = 0;
	
	//positions or locations
	int locationOne = 0;
	int locationTwo = 0;
	int locationThree = 0;
	int locationFour = 0;
	
	//Access identifier
	int number;
	
	//to enter
	cout << "Enter a number to encode: ";
	cin >> number;
	cout << "\n";
	
	if(number - 1000 >= 0) //to validate only four digits MINIMUM
	{
		if(number - 10000 < 0)// to validate only four digits	MAXIMUM
		{
			
			//to split each digit of the number
			digitOne = number / 1000;
			digitTwo = ((number / 100)%10);
			digitThree = ((number / 10) % 10);
			digitFour = number % 10 ;
			
			//to store the amount
			amountOne = digitOne + digitAmount;
			amountTwo = digitTwo + digitAmount;
			amountThree = digitThree + digitAmount;
			amountFour = digitFour + digitAmount;
			
			//to store the operation between the amount plus residue split 10
			digitOne = (amountOne * (amountOne%10));
			digitTwo = (amountTwo * (amountTwo%10));
			digitThree = (amountThree * (amountThree%10));
			digitFour = (amountFour * (amountFour%10));
			
			//to be located  in their respective positions
			locationOne = digitThree;
			locationTwo = digitFour;
			locationThree = digitOne;
			locationFour = digitTwo;
			
			cout << "the encoded number is:  " << locationOne << "" << locationTwo << "" << locationThree << "" << locationFour;
		}
		else{
			
			cout << "Introduce a number with integer digits only 4. ";
		}
		
	}
	else
	{
		
		cout << "Introduce a number with integer digits only 4. ";
		
	}
	
	return 0;
}
Aug 24, 2018 at 3:11am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

int main()
{
    std::cout << "enter a number to encode (exactly four digits): " ;
    int number ;

    if( std::cin >> number ) // if the user entered a number
    {
        if( number > 999 && number < 10'000 ) // if it has exactly four digits
        {
            // extract the four digits
            int digit_4 = number%10 ;
            int digit_3 = (number/10) % 10 ;
            int digit_2 = (number/100) % 10 ;
            int digit_1 = (number/1'000) % 10 ;

            // replace every digit with the result of adding 7 to the digit module 10.
            const int addend = 7 ;
            digit_4 = (digit_4+addend) % 10 ;
            digit_3 = (digit_3+addend) % 10 ;
            digit_2 = (digit_2+addend) % 10 ;
            digit_1 = (digit_1+addend) % 10 ;

            // the encoded number is formed by exchanging the first digit with the third,
            // and the second digit with the fourth.
            std::cout << "the encoded number is: " << digit_3 << digit_4 << digit_1 << digit_2 << '\n' ;
        }

        else std::cout << "error: not a four digit number\n" ;
    }

    else std::cout << "error: non-numeric input\n" ;
}
Aug 24, 2018 at 3:37am
JLBorges thank you so much i've noticed were is the mistake i did by missundertanding the exercise.


1
2
3
4
5
//correct interpretation
  digit_4 = (digit_4+addend) % 10 ;
            digit_3 = (digit_3+addend) % 10 ;
            digit_2 = (digit_2+addend) % 10 ;
            digit_1 = (digit_1+addend) % 10 ;



thank you very much Mr.
Last edited on Aug 24, 2018 at 3:43am
Topic archived. No new replies allowed.