Reputation: 301
I am trying to create an program that prints an array. I want the output of this program to be:
10000
00000
00000
00000
00000
This is not what happens. Instead, it just prints nothing. There are no compiling errors. I have Microsoft Visual Studio 2010.
#include "stdafx.h"
#include <iostream>
int main()
{
using namespace std;
int a [5] [5] = {0};
a [1] [1] = 1;
int xcount = 0;
int ycount = 0;
while(xcount < 6);
{
cout << a [xcount] [ycount];
xcount = xcount + 1;
if(xcount = 6)
{
ycount = ycount + 1;
xcount = xcount + 1;
if(ycount = 6)
{
exit(0);
}
}
}
return 0;
}
Any help would be appreciated.
Upvotes: 1
Views: 357
Reputation: 310930
If I have understood correctly you are trying to output the array by columns. Your code contains many bugs including the semicolon in the end of the while statement. The correct program could look the following way
#include "stdafx.h"
#include <iostream>
int main()
{
const size_t N = 5;
int a[N][N] = { 1 };
int xcount = 0;
int ycount = 0;
while ( true )
{
std::cout << a[xcount][ycount];
if ( ++xcount == N )
{
std::cout << std::endl;
xcount = 0;
if ( ++ycount == N )
{
break;
}
}
}
return 0;
}
Upvotes: 2
Reputation: 1
You should use a for loop instead of while Try this:
for(int xcount=0; xcount<5; xcount++)
{
for(int ycount=0; ycount<5; ycount++)
{
cout<< a[xcount][ycount];
}
cout<<endl;
}
a[0][0] = 1;
Upvotes: 0
Reputation: 779
Remove the semicolon:
while (xcount<6);
Aside that, algorithm is wrong. Replace second
xcount=xcount+1;
by
xcount=0;
P.S. for loop would be better here. It would be much cleaner since you know range of loops:
for (int ycount=0; ycount<6; ++ycount)
{
for (int xcount=0; xcount<6; ++xcount)
cout << a[xcount][ycount];
cout << endl;
}
Note there are no line deliminters in original solution.
P.P.S. use == in comparisons.
Upvotes: 0
Reputation: 10333
if (xcount=6) // This sets xcount to 6
change that to:
if (xcount==6) // this compares xcount with 6
Edit now that the ; is back in the question:
Having while(xcount < 6);
will loop infinately since nothing is changing xcount - remove the ;
Upvotes: 1