Outputting Values from a 2D Array to a Text File - c++
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
Related
take one column from a 2D array and store in 1D
I am trying to take this 9 x 3 and use only the 3rd column to store in its own 1D array: 3 5 8 6 3 9 7 5 12 0 5 5 1 2 3 8 2 10 8 3 11 9 3 12 4 1 5 This is what I have for a conversion: int index = 0; // 2D to 1D conversion for (int r = 0; r < N; r++) { for (int c = 0; c < 3; c++) { end[index++] = start[r][c]; } } But it is giving me the first 9 numbers in the whole matrix: 3 5 8 6 3 9 7 5 12 (but vertically) I need the 3rd column only and I don't know what I am doing wrong.
you can try this: int index = 0; // 2D to 1D conversion for (int r = 0; r < N; r++) { end[index++] = start[r][2]; }
How to relocate an element in one array in C++
I took this interview question and I failed, so I'm here to not fail again! I have an array of int with size 16 and a 5 < givenIndex < 10. I have to take the element in this index a print every possible array (there are 16) by moving the element at givenIndex through every position in array and pushing rest of elements. For example: int array[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int givenIndex = 6; Since array[givenIndex] = 7, I need to move 7 to every possible position and print that array. [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16] And that's for 16 cases. What I was trying was: for(int i = 0;i<16;i++){ array[i] = array[indexInsercion] if (i<indexInsert){ //right shift array[i] = array[i+1] }else if(i == indexInsert){ //no shift }else{ //left shift array[i] = array[i-1] } } Can I get some help?
We can only guess what the interviewer expected to see. If I was the interviewer I would like to see that you keep things simple. This is code I think one can expect to be written from scratch in an interview situation: #include <iostream> #include <array> template <size_t size> void print_replaced(const std::array<int,size>& x,size_t index){ for (int i=0;i<size;++i){ for (int j=0;j<i;++j) { if (j == index) continue; std::cout << x[j] << " "; } std::cout << x[index] << " "; for (int j=i;j<size;++j) { if (j == index) continue; std::cout << x[j] << " "; } std::cout << "\n"; } } int main() { std::array<int,16> x{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; print_replaced(x,6); } It is a first approach at the problem, with a loop that prints 16 different combinations of the array elements. Printing each line follows simple logic: We print all elements before the one that should be replaced, then the one that should be shuffled, then the remaining elements. It is simple, but wrong. Its output is: 7 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 7 2 3 4 5 6 8 9 10 11 12 13 14 15 16 1 2 7 3 4 5 6 8 9 10 11 12 13 14 15 16 1 2 3 7 4 5 6 8 9 10 11 12 13 14 15 16 1 2 3 4 7 5 6 8 9 10 11 12 13 14 15 16 1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 1 2 3 4 5 6 8 9 7 10 11 12 13 14 15 16 1 2 3 4 5 6 8 9 10 7 11 12 13 14 15 16 1 2 3 4 5 6 8 9 10 11 7 12 13 14 15 16 1 2 3 4 5 6 8 9 10 11 12 7 13 14 15 16 1 2 3 4 5 6 8 9 10 11 12 13 7 14 15 16 1 2 3 4 5 6 8 9 10 11 12 13 14 7 15 16 1 2 3 4 5 6 8 9 10 11 12 13 14 15 7 16 There is one line that appears twice and the last line is missing. As an interviewer I would not be surprised that the first attempt does not produce correct output. I don't care about that. Thats not a minus. What I would care about is how you react on that. Do you know the next steps? Do you have a strategy to fix the wrong output? Or do you just panic because you didn't manage to write the correct code on the first attempt? This is what I would like to check in an interview and then thats the end of the exercise. I want to ask more different questions rather than giving you the time to fix all mistakes and write correct well tested code, because I know that this takes more time than we have in the interview. I'll leave it to you to fix the above code ;)
Here's a quick stab at it. Basically just keep track of where the given index should go and print it there as well as skip the original position it would be in. #include <iostream> int main() { int array[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }; int givenIndex = 6; for (int p = 0; p <= 16; ++p) { if (p != givenIndex) { std::cout << "["; for (int i = 0; i < 16; ++i) { if (i == p) { if (i > 0) { std::cout << ","; } std::cout << array[givenIndex]; } if (array[i] != array[givenIndex]) { if (i > 0 || p == 0) { std::cout << ","; } std::cout << array[i]; } } if (p == 16) { std::cout << "," << array[givenIndex]; } std::cout << "]\n"; } } } Output: [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7]
If the expectation is to just print the elements of array in the given order: Keep the track of current index of array element to be print, say indx - If the position of current element processing is equal to row number then print the element at givenIndex. If indx is equal to givenIndex skip it and print indx + 1 element, otherwise print element at indx and increase indx by 1. Implementation: #include <iostream> #include <array> int main() { std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; std::size_t givenIndex = 6; for (std::size_t i = 0, indx = 0; i < array.size(); indx = 0, ++i) { std::cout << '['; for (std::size_t j = 0; j < array.size(); ++j) { if (j == i) { std::cout << array[givenIndex] << ','; continue; } if (indx == givenIndex) { ++indx; } std::cout << array[indx++] << ','; } std::cout << ']'; std::cout << '\n'; } return 0; } Output: # ./a.out [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,] If the expectation is to alter the order of elements in the array and then print the array: First move the element at givenIndex to the 0th index of array and then - Print array In every iteration swap the current element with its next element in the array and print it. Implementation: #include <iostream> #include <array> void print_array (std::array<int, 16>& array) { std::cout << '['; for (std::size_t indx = 0; indx < array.size(); ++indx) { std::cout << array[indx] << ','; } std::cout << ']'; std::cout << '\n'; } void rearrange_array_elem (std::array<int, 16>& array, std::size_t givenIndx) { // move the element at givneIndx to first position in array for (std::size_t j = givenIndx; j > 0; --j) { std::swap (array[j], array[j - 1]); } // print array print_array (array); for (std::size_t indx = 0; indx < array.size() - 1; ++indx) { // swap current element with its next element std::swap (array[indx], array[indx + 1]); print_array (array); } } int main() { std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; std::size_t givenIndex = 6; rearrange_array_elem (array, givenIndex); return 0; } Output: # ./a.out [7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,] [1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]
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
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↵
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
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(); }