2017-03-29 36 views
0

嗨,我试图找到数组的每个元素的二进制值,并将其设置在二维数组中。 这里是我的代码:如何将int数组转换为每个数组数的二进制数值的二维数组?

#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; 
    } 
} 

在这里,我的输出:

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

头三行就是我一直在寻找,但我不知道什么是休息。 有人可以帮我解决这个问题。并给我解释为什么会发生这种情况或链接到解释。

回答

2

你最后的循环(即打印的)应该只有i<3为条件。现在i被允许去7,但是然后d[i][j]超出界限并打印垃圾。

+0

OMG你说得对。我怎么没看见那个。 X( –

+0

发生在我身上:)保持你的眼睛去皮matey! – Zexus