Baraujo85
Baraujo85

Reputation: 157

How can I put a 4x4 2d array inside a column in c++?

How can I put a 4x4 2d array aside a column in c++?

Desired output:

             1995 1996 1997 1998 
John          74   45   13   57
Mark          16   12   48   75
Wilhelm       90   64   12   45
Mikhail       35   12   90   95

Keeping in mind that

 74   45   13   57  
 16   12   48   75
 90   64   12   45
 35   12   90   95

is a 4x4 matrix.

The problem is that I can't set the names column aside the printed matrix, nor can I declare the names as I did with the matrix numbers.

Current code:

int matrix[4][4] = {{ 74 , 45 , 13 , 57 },
            { 16 , 12 , 48 , 75 },
            { 90 , 64 , 12 , 45 },
            { 35 , 12 , 90 , 95 }};

int main()
{
    while (true)
    {   
        for ( int x = 0; x < 4; x++)
        {
            for ( int y = 0; y < 4; y++)
            {
                cout << "\t";
                cout << matrix[x][y];
            }
            cout << endl;
        }
        cin.get();
    }
    return 0;
}

Upvotes: 1

Views: 2220

Answers (3)

Deepesh Thapa
Deepesh Thapa

Reputation: 1789

using a vector inside a vector will work. Also known as multidimensional vector allows you to create a 2d vector.

vector< vector<data> > vec;

what this will do is it will insert a vector data inside the first position of the main vector.

So, v[0] = data inside v1

if you want to access the exact position of the 2d array then v[0][1] will return 45 and so on

Upvotes: 0

Altainia
Altainia

Reputation: 1597

You could use a std::map to help manage this for you:

std::map<std::string, std::vector<int>> table =
{
  {"",        {1995, 1996, 1997, 1998}},
  {"John",    {74,   45,   13,   57  }},
  {"Mark",    {16,   12,   48,   75  }},
  {"Wilhelm", {90,   64,   12,   45  }},
  {"Mikhail", {35,   12,   90,   95  }}
};

Then, in your code:

for(auto iter : table)
{
  std::cout << iter.first;
  for(int number : iter.second)
  {
    std::cout << '\t' << number;
  }
  std::cout << std::endl;
}

Note: You may have to do some other formatting to make sure the spaces after the names allow the beginning of the numbers to all line up.

Upvotes: 3

UpAndAdam
UpAndAdam

Reputation: 5467

In this question the answer is DONT. Create a SEPERATE array for the column headers and row headers.

The other alternative would be to create a different data structure entirely.

i.e.

std::string col_headers[4] = { "1995", "1996", "1997", "1998" };
std::string row_headers[4] = { "John" , "Mark", "Wilhelm", "Mikhail" };

now just refer to this in your original code

int main()
{
    while (true)
    {   

        for ( int c = 0; c < 4; c++ )
        {
            cout << "\t" << col_headers[c];
        }
        cout << "\n";

        for ( int x = 0; x < 4; x++)
        {
            cout << row_headers[x];
            for ( int y = 0; y < 4; y++)
            {
                cout << "\t";
                cout << matrix[x][y];
            }
            cout << endl;
        }
        cin.get();
    }
    return 0;
}

Upvotes: 4

Related Questions