1

Here is my code without any for loops and conditions:

int decryptedCode[3] = {0};
int encryptedCode[3] = {0};

printf("Enter a four digit code. i.e (1234): ");
scanf("%d%d%d%d", &decryptedCode[0], &decryptedCode[1], &decryptedCode[2], &decryptedCode[3]);

encryptedCode[0] = (decryptedCode[0] + 7); //Encrypting Code
encryptedCode[1] = (decryptedCode[1] + 7); //Encrypting Code
encryptedCode[2] = (decryptedCode[2] + 7); //Encrypting Code        
encryptedCode[3] = (decryptedCode[3] + 7); //Encrypting Code

        if ( encryptedCode[0] > 9 ) encryptedCode[0] = encryptedCode[0]%10;
        if ( encryptedCode[1] > 9 ) encryptedCode[1] = encryptedCode[1]%10;
        if ( encryptedCode[2] > 9 ) encryptedCode[2] = encryptedCode[2]%10;
        if ( encryptedCode[3] > 9 ) encryptedCode[3] = encryptedCode[3]%10;

encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[2] ^= encryptedCode[0]; //swap values
encryptedCode[0] ^= encryptedCode[2]; //swap values

encryptedCode[1] ^= encryptedCode[3]; //swap values
encryptedCode[3] ^= encryptedCode[1]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values

printf("Encrypted four digit code: %d%d%d%d", encryptedCode[0], encryptedCode[1], encryptedCode[2], encryptedCode[3]);

here in the code below i added for loop n if-else condition to make code clean n efficient:

int counter;
int decryptedCode[3] = {0};
int encryptedCode[3] = {0};

printf("Enter a four digit code. i.e (1234): ");
scanf("%d%d%d%d", &decryptedCode[0], &decryptedCode[1], &decryptedCode[2], &decryptedCode[3]);

for ( counter = 0; counter<= 3; counter++ ) {
    encryptedCode[counter] = (decryptedCode[counter] + 7);
    if ( encryptedCode[counter] > 9 ) {
        encryptedCode[counter] = encryptedCode[counter]%10;
    }
}

encryptedCode[0] ^= encryptedCode[2]; //swap values
encryptedCode[2] ^= encryptedCode[0]; //swap values
encryptedCode[0] ^= encryptedCode[2]; //swap values

encryptedCode[1] ^= encryptedCode[3]; //swap values
encryptedCode[3] ^= encryptedCode[1]; //swap values
encryptedCode[1] ^= encryptedCode[3]; //swap values

printf("Encrypted four digit code: %d%d%d%d", encryptedCode[0], encryptedCode[1], encryptedCode[2], encryptedCode[3]);

but it is not working and i have no idea why, i tried to change many things all day long but no success.

5
  • does it work if you use counter < 4 in the for statemant instead of counter <= 3? Commented Nov 28, 2013 at 14:05
  • no it does not work with counter < 4 either Commented Nov 28, 2013 at 14:10
  • One problem is that encryptedCode and decryptedCode are declared as arrays of 3 elements, but you are putting 4 elements into each one. They should be declared as int decryptedCode[4] and int encryptedCode[4]. Commented Nov 28, 2013 at 14:13
  • 1
    i thought when i declare array[3], it means i can add 4 elements in that array starting from 0 all the way up to 3. Commented Nov 28, 2013 at 14:20
  • 1
    I had 1 more question, actually its not that important but am asking just for my information when i compile this code it asks me to input 4 digit value but i can only place values like this: 1 2 3 4. notice spaces between digits. can i change that behaviour so i could input values like 1234? Commented Nov 28, 2013 at 14:47

2 Answers 2

2

You declared your arrays incorrectly: you should change them to have four items.

int decryptedCode[4] = {0};
int encryptedCode[4] = {0};

Currently, your code has undefined behavior. When your loop is "unrolled", the compiler could do a different thing, that's why the two pieces of code exhibit different behavior (although in both cases it is undefined, because you reference memory past the end of the array).

Sign up to request clarification or add additional context in comments.

4 Comments

this worked, although i couldn't get why. could you please explain so i won't face this problem again. and thanks for quick reply
@MansoorAkram When you declare an array int a[N], it has N elements. Valid indexes are zero..N-1, inclusive of both ends. You declared an array int decryptedCode[3], so valid indexes are 0, 1, and 2. When you access decryptedCode[3], it gets an undefined value (it's very likely going to be the initial element of encryptedCode, but not necessarily: it's undefined). Same goes for your encryptedCode[3] access.
I had 1 more question, actually its not that important but am asking just for my information when i compile this code it asks me to input 4 digit value but i can only place values like this: 1 2 3 4. notice spaces between digits. can i change that behaviour so i could input values like 1234?
@MansoorAkram Yes, you can, but the code would be less intuitive. You can change scanf format to "%c%c%c%c", but then you would need to subtract '0' (note the single quotes - very important) from each entered number: scanf("%c%c%c%c", &decryptedCode[0], ...); decryptedCode[0] -= '0'; decryptedCode[1] -= '0'; ...
1

In C, for int a[n], array indexing starts from 0 and ends at n-1. To access decryptedCode[3] and encryptedCode[3] you have to declare your arrays as

int decryptedCode[4] = {0};
int encryptedCode[4] = {0};  

otherwise it will invoke undefined behavior and you will get anything, may be your expected value too (if you are (un)lucky).

n1570: J.2 Undefined behavior:

The behavior is undefined in the following circumstances:
......

— An array subscript is out of range, even if an object is apparently accessible with the given subscript..

3 Comments

but when i am not using loop and compiled the 1st version then it worked with size [3] array too. does this behaviour appears only when using loops?
@MansoorAkram No, it's not dependent on the presence or absence of loops. Good chances are, the optimizer "optimized out" both arrays, treating them as four independent variables in the process of optimizing the expressions on them.
No. I said accessing arrays out of bound will invoke UB regardless you are using loop or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.