0

Okay so I'm tryna create a program that: (1) swaps my array
(2) performs caesar cipher substitution on the swapped array (3) convert the array from (2) that is in decimal form into 8-bit binary form

And so far I've successfully done the first 2 parts but I'm facing problem with converting the array from decimal to binary form.

And this is my coding of what I've tried

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
void swapfrontback(int a[], int n);
int main()
{
    int a[10], i, n;
    cout << "enter size" << endl;
    cin >> n;
    if (n == 0)
    {
        cout << "Array is empty!\n";
    }
    else
    {
        cout << "p = " << endl;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
     } 
     swapfrontback(a,n);
     
     //caesar cipher
     
     int shift = 0;
     cout << "input shift: ";
     cin >> shift;
     
     int modulus = 0;
     cout << "input modulus: ";
     cin >> modulus;
     
     cout << "p''=" << endl;
     
    for (i = 0; i < n; i++)
    {
        a[i] = (a[i] + shift) % modulus;
        cout << a[i] << endl;
    }
    
    // Function that convert Decimal to binary
     
    int b;
    b = 8;
    
    cout<< "p'''=" << endl;
    
    for (i = 0; i < n; i++)
    {
        

    for(int i=b-1;i>=0;i--)
    {
        
        if( a[i] & ( 1 << i ) ) cout<<1;
        else cout<<0;  
    }
    }
    
     return 0;
     
}
void swapfrontback(int a[], int n)
{
    int i, temp;
    for (i = 0; i < n / 2; i++)
    {
        temp = a[i];
        a[i] = a[n - i-1];
        a[n - i-1] = temp;
    }
    
    cout << "p' = '" << endl;
    
    for (i = 0; i < n; i++)
    {
        cout << a[i] << endl;
    }
}

the problem is that instead of converting the array of decimal from the 2nd part which is the caesar cipher into its binary form, I'm getting 000000010000000100000001 . My initial array is

3
18
25

Shift 8 and modulo 26. If anyone knows how to fix this please do help me.

  • First of all please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Then think about what happens if `n` is larger than `10`. – Some programmer dude Dec 16 '22 at 19:26
  • Off topic, but your first 3 lines of code are all bad practices. Where are you learning C++ from? And in C++ swapfrontback takes a std::vector& as parameter. Not a "C" style array with seperate size (that's more a "C" thing). And for swap there is std::swap – Pepijn Kramer Dec 16 '22 at 19:26
  • 2
    `if( a[i] & ( 1 << i ) ) cout<<1;` Why make life difficult using 2 variables with the same name? Use a different name for the inner loop. – 001 Dec 16 '22 at 19:35
  • I fixed the bug that Johnny Mopp described, ran your code and it worked. – john Dec 16 '22 at 19:50
  • Disagree slightly with one of the previous comments. `#include` isn't bad practice. It's just pointless when used in conjunction with `#include` and harmed by the use of `using namespace std;`. – user4581301 Dec 16 '22 at 19:53
  • Okay so I tried what Johnny Mopp described and yeah it works but it prints in a row. Does anyone know how to separate each binary so that it prints in a column rather than a row? – user20772188 Dec 17 '22 at 06:58

1 Answers1

1

Well, there seems to be something that may be an issue in the future (like the n being larger than 10, but, regarding your question, this nested for sentence is wrong.

for (i = 0; i < n; i++)
{
    

for(int i=b-1;i>=0;i--) //here you are using the variable 'i' twice
{
    
    if( a[i] & ( 1 << i ) ) cout<<1; //i starts at 7, which binary representation in 4 bits is 0111
    else cout<<0;  
}
}

When you're using nested for sentences, it is a good idea to not repeat their iterating variables' names since they can affect each other and create nasty things like infinite loops or something like that. Try to use a different variable name instead to avoid confusion and issues:

for(int j=b-1;j>=0;j--) //this is an example

Finally, the idea behind transforming a base 10 number to its binary representation (is to use the & operator with the number 1 to know if a given bit position is a 1 (true) or 0 (false)) for example, imagine that you want to convert 14 to its binary form (00001110), the idea is to start making the & operation with the number 1, an continue with powers of 2 (since them will always be a number with a single 1 and trailing 0s) 1-1 2-10 4-100 8-1000, etc.

So you start with j = 1 and you apply the & operation between it and your number (14 in this case) so: 00000001 & 00001110 is 0 because there is not a given index in which both numbers have a '1' bit in common, so the first bit of 14 is 0, then you either multiply j by two (j*=2), or shift their bits to the left once (j = 1<<j) to move your bit one position to the left, now j = 2 (00000010), and 2 & 14 is 2 because they both have the second bit at '1', so, since the result is not 0, we know that the second bit of 14 is '1', the algorithm is something like:

int j = 128; 128 because this is the number with a '1' in the 8th bit (your 8 bit limit)
int mynumber = 14;

while(j){ // when the j value is 0, it will be the same as false

if(mynumber & j) cout<<1;
else cout<<0;

j=j>>1;
} 

Hope you understand, please ensure that your numbers fit in 8 bits (255 max).

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
LloydNA
  • 11
  • 2
  • I tried what u told about using a different variable name instead and it works but then I'm getting the binary in a row. Do u know how I can separate each of the binary in a column instead of a row? – user20772188 Dec 17 '22 at 07:07
  • At the end of your first for sentence, print a line break like this: cout<<‘\n’ – LloydNA Dec 18 '22 at 14:36