0

Hi i'm trying to find binary value of every element of array and to set it in 2d array. Here is my code:

#include <iostream>

void tobin(int a,int b[]);

int main()
{
    int i,j;
    int b[8];//bin array
    int d[3][8]; //2d array
    int c[3] = {6,15,24}; //int array

    //My code for transformation from array to 2d array
    for(i=0;i<3;i++)
        {
            tobin(c[i],b);
            for(j=0;j<8;j++)
            {
                d[i][j]=b[j];

            }
        }

    //Printing of 2d array
    for(i=0;i<8;i++)
        {
            for(j=0;j<8;j++)
                std::cout<<d[i][j];
            std::cout<<std::endl;
        }
}

//Function for convert int to bin
void tobin(int a,int b[])
{
    int i;
    for(i=0;i<8;i++)
    {
        b[i]=a%2;
        a/=2;
    }
}

And here my output:

01100000
11110000
00011000
00-16437331793270200193648149632765
014196397000-640061051-1985511146
419616001936481488327650000
16416403251985832910-46743922719940467470000
004196848019364814963276510

First 3 lines is what i was looking for, but i don't know what is the rest. Can someone help me to fix this. And to give me explanation why is this happening or link to explanation.

1 Answer 1

2

Your last for-loop (the one that prints) should only have i<3 as its condition. Right now i is allowed to go to 7, but then d[i][j] is out of bounds and prints garbage.

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

1 Comment

happens to me all the time :) keep yer eyes peeled matey!

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.