DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   What's Wrong with my Array? (http://www.daniweb.com/forums/thread61227.html)

compshooter Nov 10th, 2006 7:38 pm
What's Wrong with my Array?
 
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:?:

John A Nov 10th, 2006 8:10 pm
Re: What's Wrong with my Array?
 
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}};

compshooter Nov 10th, 2006 8:40 pm
Re: What's Wrong with my Array?
 
It worked! Thanks so much:!:


All times are GMT -4. The time now is 12:17 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC