Hi guys....
It has been a long time since i left this forum..
Now i'm actually back studying c++ and here is the little problems that i got with the 2D
array question.

I need to create this pattern using the 2D array:

1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1

and this is one that i've tried by my own:

#include <iostream>

using namespace std;

int main(){

const int COLUMNS=5;

int number[5][COLUMNS];

    for(int i=0; i<5; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
            if((number[i][j])%2==0){
                cout<<"1";
            }else cout<<"0";
            }
            cout<<endl;
        }

}

but it appears that my code is not right by displaying this kind of output

11111
11111
11111
11111
11110

i'm not sure whats wrong and yet, i still not familiar with 2d arrays..So i hope someone could help me out here by telling me what should i do for the codes that i've written.

Thank You :)

I'd consider adding another constant for the Rows instead of using a magic number. This will make future modifications to the program easier and is generally considered good practice.

As far as assigning your values, I would suggest you base your if statement on the current value of j rather than the current value of the element you are currently looking at.

Also, I would consider populating the array, not just outputting some number to the screen. As currently written, you are not saving anything to your array. You're simply iterating over the elements to see if they are initialized.

Since this is an assignment, I'm guessing you need to use a loop to populate the array, then another to display it. Otherwise, I would suggest you simply use initialization lists.

#include <iostream>

using namespace std;

int main(){

const int COLUMNS=5;

int number[5][COLUMNS];

    for(int i=0; i<5; i++)
    {
        for(int j=0; j<COLUMNS; j++)
        {
                if(j==0||j%2==0)
    {
                cout<<"1";
                }
    else 
    {

     cout<<"0";
    }

            cout<<endl;
        }

}
commented: Use code tags and don't give complete answers to homework +0

Much easier way :

string seq = "1 0 1 0 1";

for i = 0 to N
  print seq;

#include <iostream>

using namespace std;

int main(){

<..snipped..>
}

Um... Have you read the forum rules and posting guidlines?

Not only did you not use [code] ...code tags... [/code], you did exactly what you shouldn't have. We discuss what is wrong with the code and make suggestions for correcting errors. We do not simply paste a complete correction. It is preferable to post example code demonstrating a concept rather than post a corrected version of another user's code.

Much easier way :

string seq = "1 0 1 0 1";

for i = 0 to N
  print seq;

That would definitely work, but the OP needs to use a 2D array (at least that's how they make it sound).

Hi guys....

if((number[i][j])%2==0){

This is where your program goes wrong. You are testing whether the value of the "matrix" at i,j is even or odd. Since you haven't initialized any of your values in the array, this will produce undefined behavior. When you allocate memory for an array (dynamically or statically), the program simply reserves the space. It does not set any of the values in the memory. So, there will be random bytes in the allocated space that may have been meaningful to whatever objects were stored there before. As Fbody said, you need to test your indices for evenness (in particular the j index).

huh..thank you for your reply..
and overall,it helps me to figure out a bit for this 2d arrays thing..
but there is something that bothering me now..

this thread that i've made actually came from this question:

How do you create the following matrix in terms of 2d arrays?

1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1
1 0 1 0 1

Follow this guideline:
i. Define the array with the above size in rows and columns.
ii. Check the pattern of 0 and 1 in terms of rows and columns.
iii. Assign the value to the 2D array element based on the logic pattern you had just created using an inner loop.
iv. Display the element of 2d array as above.

my question is, if the answer is going to be something like what bkvinaybhs has written which is:

#include <iostream>

using namespace std;

int main(){

const int COLUMNS=5;

int number[5][COLUMNS];

for(int i=0; i<5; i++)
{
for(int j=0; j<COLUMNS; j++)
{
if(j==0||j%2==0)
{
cout<<"1";
}
else
{

cout<<"0";
}

cout<<endl;
}

}

where is the logic of using the 2D arrays here?
the code could simply be overwritten like this one right? :

#include <iostream>

using namespace std;

int main(){

int COLUMNS=5;
int row=5;

for(int i=0; i<row; i++)
{
  for(int j=0; j<COLUMNS; j++){
    if(j==0||j%2==0){
       cout<<"1"<<" ";
    }else{
       cout<<"0"<<" ";
    }
  }

cout<<endl;
}

}

sorry, but i still not sure about this 2d array things..so,is this the only way to solve this problem or there is the other way of using the 2d array methods?

Thank You..

The code that bkvinaybhs provided suffers from the same issue as I mentioned to you earlier. It is simply a display function, as you wisely observed these are both similar to your second piece of code. The objective of the assignment is to learn to traverse and edit 2-d arrays. As such, the inner loop should store either a 1 or 0 to the appropriate array element, then display from the array, not display directly.

Your array-populating loop should be something more like this:

for (int rowVar = 0; rowVar < ROWS; ++rowVar) {
  for (int colVar = 0; colVar < COLUMNS; ++colVar) {
    if ((colVar == 0) || ((colVar % 2) == 0)) {
      array[rowVar][colVar] = 1;
    } else {
      array[rowVar][colVar] = 0;
    }
  }
}

Then you would create a nested loop structure that works similarly.

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.