| | |
What's Wrong with my Array?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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?
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
Can someone take a look at my code and enlighten me?
C++ Syntax (Toggle Plain Text)
#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:
When you should only be using braces:
C++ Syntax (Toggle Plain Text)
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)};
C++ Syntax (Toggle Plain Text)
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}};
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- Using operator[] with an ArrayList (Java)
- unserialize an array! spitting out jibberish (PHP)
- letter and word counter (C)
- retrieving ids from dynamically created controls in asp.net (ASP)
- for loop error (C)
- having trouble with arrays (C++)
- Run-time Error when printing Array Contents. (C)
- how to increase the size of an array? (C)
- question about array (C++)
- I would kill for this, just a little bit (C)
Other Threads in the C++ Forum
- Previous Thread: Permutations of an N element array
- Next Thread: int main() or void main() ??!!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






