How to output horizontal letters in an array table - c++

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↵

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

Related

Traverse 2D Matrix diagonally omitting first row and first column

I am trying to traverse a 2D matrix diagonally and the function below prints all elements in a diagonal.I want to skip the first row and first column elements and start the diagonal traversal from matrix[1][1] because the values in the 0th row and 0th column are not required.So it is like slicing the matrix from the top and starting from [1][1] but not making any changes to the bottom of the matrix.
void diagonalOrder(int matrix[][COL])
{
for(int line = 1;
line <= (ROW + COL - 1);
line++)
{
int start_col = max(0, line - ROW);
int count = min(line, (COL - start_col), ROW);
/* Print elements of this line */
for(int j = 0; j < count; j++)
cout << setw(5) <<
matrix[minu(ROW, line) - j - 1][start_col + j];
cout << "\n";
}
I will update my question with an example to make it clear.Consider the following matrix.
0 1 2 3 4
matrix[5][5] = 1 8 5 3 1
2 4 5 7 1
3 6 4 3 2
4 3 4 5 6
The above function will print the values of this diagonally.
Output:
0
1 1
2 8 2
3 4 5 3
4 6 5 3 4
3 4 7 1
4 3 1
5 2
6
I want to skip the elements of the first row and the first column and starting at matrix[1][1] want to traverse the matrix diagonally.
Desired Output:
8
4 5
6 5 3
3 4 7 1
4 3 1
5 2
6
From your example it looks like you want to print antidiagonals not diagonals, ie third line is 3 4 5 3 not 3 5 4 3.
To get started keep things simple: Indices (i,j) along an antidiagonal are those i and j where i+j == some_constant. Hence this is a simple (not efficient) way to print elements along one antidiagonal:
void print_antidiagonal(int matrix[5][5],int x){
for (int i=4;i >= 0; --i) {
for (int j=0;j < 5; ++j) {
if (i+j == x) std::cout << matrix[i][j] << " ";
}
}
std::cout << "\n";
}
Further there are nrows + (ncols-1) antidiagonals, hence you can print them all via:
for (int i=0;i < 5+4; ++i) {
print_antidiagonal(matrix,i);
}
The function above isnt very efficient, but it is simple. It is obvious how to skip the first row and first column:
for (int i=4;i >= 1; --i) { // loop till 1 instead of 0
for (int j=1;j < 5; ++j) { // loop from 1 instead of 0
This is sufficient to produce desired output (https://godbolt.org/z/7KWjb7qh7). However, not only is the above rather inefficient, but also the code is not very clear about its intent. print_antidiagonal prints elements along a single anti-diagonal, hence iterating all matrix elements is a bad surprise.
I suggest to print the indices rather than the matrix elements to get a better picture of the pattern (https://godbolt.org/z/TnrbbY4jM):
1,1
2,1 1,2
3,1 2,2 1,3
4,1 3,2 2,3 1,4
4,2 3,3 2,4
4,3 3,4
4,4
Again, in each line i+j is a constant. And that constant increments by 1 in each line. In each line i decrements while j increments, until either i == 1 or j==4. The first element is such that i is maximum and j = constant - i.
Hence:
void print_antidiagonal(int matrix[5][5],int x){
int i = std::min(x-1,4);
int j = x - i;
while (i >= 1 && j <= 4) {
std::cout << matrix[i][j] << " ";
--i;
++j;
}
std::cout << "\n";
}
Live Example.
PS: I used hardcoded indices, because I considered it simpler to follow the logic. For a more realistic solution the matrix size and offset should be parametrized of course.
I don't really get what your code is trying to do but just going by the description you need to iterate over the array items with equal row and column indices until there either are no more rows or no more columns i.e.
void print_tail_of_diagonal(int matrix[ROWS][COLS])
{
int n = std::min(ROWS, COLS);
for (int i = 1; i < n; ++i) {
std::cout << matrix[i][i] << " ";
}
std::cout << "\n";
}

Outputting Values from a 2D Array to a Text File

I am writing some C++ code and i currently have a function that reads in some numbers from a text file and stores them into a 2D array. I now need to output the same numbers i have stored into the 2D array back out into a new text file. I currently have some code in a function that can output the numbers however they are not in the same format as the input file. As you can see below.
Input File Format (space between each number)
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8
Output File Format Currently
123456789234567891345678912456789123567891234678912345789123456891234567912345678 (this is all on one line)
My function to read in from the text file.
void Grid::LoadGrid(const char filename[])
{
ifstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file >> m_grid[x][y];
}
}
file.close();
}
My function to read out to the text file. (m_grid is the 2D array)
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y];
}
}
file.close();
}
If anyone can help me output it to the text file so it will appear the same as the input i'd be very grateful.
Edit: Question has been answered.
After your inner loop completes
file << endl;
Also in your inner loop might want to..
file << m_grid[x][y] << " ";
Update the way you write to the file like this:
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
cout << endl;
for (int x = 0; x < 9; x++)
{
cout << m_grid[x][y];
cout << " ";
}
}
file.close();
}
In your output function you should write the spaces after each character. And the newline character when you reach the end of a row.
I believe this would work
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y] << " ";//write a space after the character
if(x ==8) //at x ==8 is where this for loop would exit
{
file << "\n"
}
}
file.close();
}
The output you are seeing is exactly what you told the compiler to do. You are displaying each of your NxN matrix elements one right after another. You never added any white space to your output stream. To fix this, it is quite simple. Adjust your function as follows:
void Grid::SaveGrid(const char filename[])
{
ofstream file(filename);
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
{
file << m_grid[x][y] << " "; // and a space after each element
}
file << '\n'; // add a new line character after each row has been printed.
}
file.close();
}
Here's a simple working example that uses a std::vector<int> of your numbers and displays it to the console. The only difference here is that I'm using a 2D to 1D mapping for the indexing into the vector. Once you understand how this affects the formatting it should be trivial to convert it from an std::vector to a multidimensional array and to convert the iostream to a fstream output.
#include <iostream>
#include <vector>
int main() {
const std::vector<int> values{ 1,2,3,4,5,6,7,8,9,
2,3,4,5,6,7,8,9,1,
3,4,5,6,7,8,9,1,2,
4,5,6,7,8,9,1,2,3,
5,6,7,8,9,1,2,3,4,
6,7,8,9,1,2,3,4,5,
7,8,9,1,2,3,4,5,6,
8,9,1,2,3,4,5,6,7,
9,1,2,3,4,5,6,7,8
};
const int size = 9;
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
std::cout << values[col + size*row] << " ";
}
std::cout << '\n';
}
return 0;
}
-Ouput-
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9 1
3 4 5 6 7 8 9 1 2
4 5 6 7 8 9 1 2 3
5 6 7 8 9 1 2 3 4
6 7 8 9 1 2 3 4 5
7 8 9 1 2 3 4 5 6
8 9 1 2 3 4 5 6 7
9 1 2 3 4 5 6 7 8

How do I save all the numbers from a string into a multi-dimensional array in c++?

I have to write a program that takes a completed sudoku board, saves only the numbers (meaning all the symbols used between the numbers to separate them such as '-', '|' etc cant be saved) into a two-dimensional array.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int input[11] = { 0 };
int sudoku[9][9] = { 0 };
for (int line = 0; line <= 10; line++)
{
cin >> input[line];
}
system("PAUSE");
return 0;
}
This is the only working code I've got so far. I've tried different kinds of for loops to get this done but I can't figure why it doesn't work.
So I wanted to ask, is it even possible save all the numbers of a string into a multi-dimensional array? And if it's not, where is my approach wrong or how could I solve this task?
One example of the input would be:
.5.1.4.|.8.6.9.|.7.2.3
.8.7.2.|.3.4.5.|.6.1.9
.9.6.3.|.2.1.7.|.5.4.8
-------|-------|-------
.6.2.8.|.1.3.4.|.9.5.7
.1.9.7.|.6.5.2.|.8.3.4
.4.3.5.|.7.9.8.|.1.6.2
-------|-------|-------
.2.4.6.|.9.7.1.|.3.8.5
.7.5.1.|.4.8.3.|.2.9.6
.3.8.9.|.5.2.6.|.4.7.1
One approach is to use regular expressions. This way the formatting of the sudoku board can change but your will still be able to parse out the numbers.
The reason I broke it into two for loops was to easily ignore the row that has no numbers in it.
#include <iostream>
#include <regex>
#include <string>
#include <vector>
int main()
{
std::string line;
// this regular expression matches a single digit
std::regex exp("(\\d)");
std::smatch res;
int sudoku[9][9] = {{0}};
int row = 0;
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
// get a line of the board
std::getline(std::cin, line);
// search for the next digit in the line
for (int k = 0; std::regex_search(line, res, exp); ++k)
{
// convert the digit into an integer and store it in the board
sudoku[row][k] = std::stoi(res[0]);
// the rest of the line after the first match becomes the new
// line so that we can search for the next digit
line = res.suffix();
}
row += 1;
}
// ignore every third row that is used to separate the board sections
std::getline(std::cin, line);
}
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
std::cout << sudoku[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
For your example board, it produces this output:
5 1 4 8 6 9 7 2 3
8 7 2 3 4 5 6 1 9
9 6 3 2 1 7 5 4 8
6 2 8 1 3 4 9 5 7
1 9 7 6 5 2 8 3 4
4 3 5 7 9 8 1 6 2
2 4 6 9 7 1 3 8 5
7 5 1 4 8 3 2 9 6
3 8 9 5 2 6 4 7 1

Weird Input output in cpp

I was solving subarray with given sum,Where we have to print the starting and ending index of array if subarray with sum is found , when I tried with two test cases simultaneously i got wrong result
But when I was tried one at a time I got right answer in both.
You please also check in your IDE this is happening in every IDE.
Testcase (Simultaneously)
2
5 12
1 2 3 7 5
10 15
1 2 3 4 5 6 7 8 9 10
Output
2 4 (expected 2 4)
2 5 (But expected 1 5)
But when I tried like this for second test cases
1
10 15
1 2 3 4 5 6 7 8 9 10
Output : 1 5(As expected)
I got correct answer ,why my program this kind of weird behaviour ?
#include<iostream>
#include<vector>
#include<queue>
#include<unordered_map>
using namespace std;
vector<int>a;
unordered_map<int, int>seen;
int main()
{
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
a.resize(n);
int sum = 0;
seen[0] = -1;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
if (seen.find(sum - s) != seen.end()) {
int x;
x = seen[sum - s] + 2;
cout << x << " " << i + 1 << endl;
break;
}
else {
seen[sum] = i;
}
}
seen.clear();
a.clear();
//cout<<endl;
}
return 0;
}

print a table of decremented numbers

I want to write a code that print a table like this:
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
I wrote a code to print a table as said above, but it just print 5's.
I know that I have to use a condition to print such a table. What's the condition to print it ?
int main () {
int number = 5;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (condition)
...
else
cout << number << " ";
}
cout << endl;
}
return 0;
}
As I mentioned in comments, what you want to print is the Chebyshev distance to the center +1. I dont know what condition can make your code work, but instead I would use a simple formula to calculate each value:
#include <iostream>
using namespace std;
int main() {
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
cout << std::max(abs(i-4),abs(j-4)) +1 << " " ;
}
cout << endl;
}
}
/*To make boxes and inside box and so on
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[100][100],n,i,j,k=0,l;
clrscr();
cout<<"Enter the outside No. n \n";
//like for your answer it is 5;
//i and j are loop element for 2D array
//k is used for making n boxes
cin>>n;
l=n;
n=2*n-1;
while(k<n)
{
if(k%2==0)
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
else
{
for(i=k;i<n-k;i++)
{
a[0+k][i]=l;
a[i][0+k]=l;
a[n-1-k][i]=l;
a[i][n-1-k]=l;
}
k++;l--;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout << a[i][j];
if(a[i][j]>9)
cout<<" ";
else
cout<<" ";
}
cout<<endl;
}
getch();
}