Dylan Yazdani
Dylan Yazdani

Reputation: 19

How to output horizontal letters in an array table

I am making a 6x6 dice game and I need to be able to put ROWS on the y-axis of the table but I do not understand how to do that with my code.

Create a 6x6 2D-Table that holds the sum of the rows and columns using values 1 to 6.

I have been reading through the ninth edition of "Starting out with C++ From Control Structures through Objects" by Tony Gaddis and I just cannot find anything about what I am looking for.

//System Libraries
#include <iostream>  //Input/Output Library
#include <iomanip>   //Format Library
using namespace std;

//User Libraries

//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const int COLS=7;

//Function Prototypes
void fillTbl(int [][COLS],int);
void prntTbl(const int [][COLS],int);

//Execution Begins Here!
int main(int argc, char** argv) {
    //Declare Variables
    const int ROWS=6;
    int tablSum[ROWS][COLS] ={{1,2,3,4,5,6,7},
                              {2,3,4,5,6,7,8},
                              {3,4,5,6,7,8,9},
                              {4,5,6,7,8,9,10},
                              {5,6,7,8,9,10,11},
                              {6,7,8,9,10,11,12}};


    //Initialize or input i.e. set variable values
    fillTbl(tablSum,ROWS);
                    cout<<"Think of this as the Sum of Dice Table\n";
                    cout<<"           C o l u m n s\n";
                    cout<<"     |   1   2   3   4   5   6\n";
                    cout<<"----------------------------------\n";
    //Display the outputs
    prntTbl(tablSum,ROWS);


    //Exit stage right or left!
    return 0;
}
void fillTbl(int tablSum [][COLS],int ROWS)
{
    cout<<"";
}
void prntTbl(const int tablSum [][COLS],int ROWS)
{
    for(int x = 0; x < ROWS; x++)
    {
        for(int y = 0; y < COLS; y++)
        {

                cout<<setw(4)<<tablSum[x][y];
        }
        cout<<endl;
}
}

Your Output

Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1···2···3···4···5···6···7↵
···2···3···4···5···6···7···8↵
···3···4···5···6···7···8···9↵
···4···5···6···7···8···9··10↵
···5···6···7···8···9··10··11↵
···6···7···8···9··10··11··12↵

Expected Output

Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1·|···2···3···4···5···6···7↵
R··2·|···3···4···5···6···7···8↵
O··3·|···4···5···6···7···8···9↵
W··4·|···5···6···7···8···9··10↵
S··5·|···6···7···8···9··10··11↵
···6·|···7···8···9··10··11··12↵

Upvotes: 0

Views: 169

Answers (1)

defq0n
defq0n

Reputation: 79

We can change your prntTbl function to have a string literal containing the rows string with: char* rows = " ROWS "; Then before every inner loop iteration, we can print the character at the index of the string using our first loop index, as well as the row value and any spacing using: cout << rows[x] << " " << x + 1 << " |";

Our ending prntTbl method looks like:

void prntTbl(const int tablSum [][COLS],int ROWS)
{
    char* rows = " ROWS ";
    for(int x = 0; x < ROWS; x++)
    {
        cout << rows[x] <<  "  " << x + 1 << " |";
        for(int y = 0; y < COLS; y++)
        {

                cout<<setw(4)<<tablSum[x][y];
        }
        cout<<endl;
}

and the output:

           C o l u m n s                                                                
     |   1   2   3   4   5   6                                                          
----------------------------------                                                      
   1 |   1   2   3   4   5   6   7                                                      
R  2 |   2   3   4   5   6   7   8                                                      
O  3 |   3   4   5   6   7   8   9                                                      
W  4 |   4   5   6   7   8   9  10                                                      
S  5 |   5   6   7   8   9  10  11                                                      
   6 |   6   7   8   9  10  11  12

Upvotes: 1

Related Questions