I am just trying to print out my array of binary values from 0 to 15. The output I get is wrong, very very wrong.

Can someone take a look at my code and enlighten me?

#include <iostream>
#include <conio.h>
using namespace std;
int binA[16][4] = {
  (0,0,0,0),(0,0,0,1),(0,0,1,0),(0,0,1,1),
  (0,1,0,0),(0,1,0,1),(0,1,1,0),(0,1,1,1),
  (1,0,0,0),(1,0,0,1),(1,0,1,0),(1,0,1,1),
  (1,1,0,0),(1,1,0,1),(1,1,1,0),(1,1,1,1)}; 
int main()
{
  cout<<"This is the bin array:"<<endl;
  for (int r=0; r<16; r++)
  {
      for (int v=0; v<4; v++)
      {
        int t=v%4;
        cout<<binA[r][v];
        if (v==3)
          cout<<" ";
      }
  } 
getch();
return 0;
}

The result I get is...

0101 0101 0101 0101 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

What's going on:?:

It's because you are declaring your array like this:

int binA[16][4] = {
  (0,0,0,0),(0,0,0,1),(0,0,1,0),(0,0,1,1),
  (0,1,0,0),(0,1,0,1),(0,1,1,0),(0,1,1,1),
  (1,0,0,0),(1,0,0,1),(1,0,1,0),(1,0,1,1),
  (1,1,0,0),(1,1,0,1),(1,1,1,0),(1,1,1,1)};

When you should only be using braces:

int binA[16][4] = {
  {0,0,0,0},{0,0,0,1},{0,0,1,0},{0,0,1,1},
  {0,1,0,0},{0,1,0,1},{0,1,1,0},{0,1,1,1},
  {1,0,0,0},{1,0,0,1},{1,0,1,0},{1,0,1,1},
  {1,1,0,0},{1,1,0,1},{1,1,1,0},{1,1,1,1}};

It worked! Thanks so much:!:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.