trying to do a 2D array of a square sorta shape, and want to display it in the format 1-12
in the order

1 2 3 4
5 6 7 8
9 10 11 12

im an absolute novice if you cant tell, just wondering how i go about this.trying to look and find tutorials as i go.
thanks

Recommended Answers

All 14 Replies

do you already know how to store values in a 2d 4x4 array?
the trick in printing is using a nested for loop that prints the contents using the first index as the counter for the first one and the second index for the second loop while printing a newline for every iteration of the first loop.

know i don't unfortunately, looking through examples etc. to help me out

#include<iostream>
#include<fstream>

using namespace std;

int input(istream& in=cin)
{
    int x;
    in >> x;
    return x;
}

int main()
{
    int board[3][4]; //creates a 9*9 matrix or a 2d array.

    for(int i=0; i<3; i++)    //This loops on the rows.
    {
        for(int j=0; j<4; j++) //This loops on the columns
        {
            board[i][j] = input(); //you can also connect to the file
            //and place the name of your ifstream in the input after opening the file will
            //let you read from the file.
        }
    }

    for(int i=0; i<3; i++)    //This loops on the rows.
    {
        for(int j=0; j<4; j++) //This loops on the columns
        {
            cout << board[i][j]  << "  ";
        }
        cout << endl;
    }
}

this is what ive done so far, but i have to create the matrix myself, as opposed gettinf to fill in from 1-12 in the format 1 2 3 4, underneath that 5 6 7 8 then 9 10 11 12. any help?

I'm not sure whether this is what you're after:

#include<iostream>
#include <iomanip>
//#include<fstream>

using namespace std;


int main()
{
    int input = 1;
    int board[3][4];

    for(int i=0; i<3; i++)    //This loops on the rows.
    {
        for(int j=0; j<4; j++) //This loops on the columns
        {
            board[i][j] = input; 
            input++;
        }
    }

    for(int i=0; i<3; i++)    //This loops on the rows.
    {
        for(int j=0; j<4; j++) //This loops on the columns
        {
            cout << setw(2) << board[i][j]  << "  "; // setw(2) for alignment
        }
        cout << endl;
    }

    cin.get();

    return 0;
}

any helpers? thanks

Seeing as my previous post didn't help, could you please clarify exactly what you're wanting to do?

nullptr - apolgies there,never even seen your comment,it totally bypassed me there! lol
thats exactly what i was looking, thats the first part of that problem done, theres a bit i still have to do for the follow on. gonna give it a try and if i have any problems, i'l ask here and hopefully get an idea or 2 from people. thanks again

next part of this is another array (B,X or whatever) that will hold the elements of the transpose of the first array (1-12 shape) and display it on the command prompt.
probably wednesday before i make a start to this, but if anyone in advance wants to do it, work away

cant get the transpose 1 for  1 5 9
                              2 6 10
                              3 7 11
                              4 8 12

can get the 4 X 3 displayed, but dont know what to do from there to get the one above.

Do the following to transpose...
1. Change outer loop to loop on columns
2. Change inner loop to loop on rows
3. Swap your row and column variables for your 2D array when printing it from the inside loop

sorry bobs0327 got it sorted there,sorry for the reply so late. the next part of this is to define a pointer that you can use to access the
Arrays 1 and 2. and then be able to print out elements x(1,3) and y(2,1) , these being examples.

?

the next part of this is to define a pointer that you can use to access the
Arrays 1 and 2. and then be able to print out elements x(1,3) and y(2,1) , these being examples.

I'm not sure I understand this statement. But pointers can be used as follows:

board[1][3] would be *(*(board+1)+3)

going to go back to this part later,but afterwards an array C must be made,and it has to be big enough to store two copies of array A and copy array A into C twice.

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.