How can I add space character only to the first & last column and row in an two dimensional array?












-1















I have probably a simple problem,
this is what i get:
output



My point is to have a table which contains space character in the first and the last column and in the first and the last row, and in all other places the random value which is "o" or space character.
Also, I want to "close" this board with the mark of "|" at the end of every row, just to make it look neat.



In function generujpole there are 4 lines (e.g. pole[i][0] = ' ';) which I thought would solve the problem, but it didn't work for the last column.



What is wrong and how should it be written properly?



The code:



#include <iostream>
#include <ctime>
using namespace std;

const int width = 62;
const int height = 22;

void generujpole(char pole[height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
pole[i][0] = ' ';
pole[i][61]= ' ';
pole[0][j] = ' ';
pole[21][j] = ' ';

int maluj = rand()%100;
if(maluj < 90) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}

void wypiszpole(char pole[height])
{
cout <<endl;
cout<< " GAME OF LIFE";
cout << endl << endl;
cout << "+------------------------------------------------------------+"
<<endl;
for(int i=0; i<height; i++)
{
cout << "|";
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << "|" << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
cout << endl;
cout << " R - reset, Q - quit, K - step " << endl;
}









share|improve this question

























  • Please don't post links to external resources. They might go down in the future making the resources inaccessible.

    – Hristijan Gjorshevski
    Nov 23 '18 at 20:21
















-1















I have probably a simple problem,
this is what i get:
output



My point is to have a table which contains space character in the first and the last column and in the first and the last row, and in all other places the random value which is "o" or space character.
Also, I want to "close" this board with the mark of "|" at the end of every row, just to make it look neat.



In function generujpole there are 4 lines (e.g. pole[i][0] = ' ';) which I thought would solve the problem, but it didn't work for the last column.



What is wrong and how should it be written properly?



The code:



#include <iostream>
#include <ctime>
using namespace std;

const int width = 62;
const int height = 22;

void generujpole(char pole[height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
pole[i][0] = ' ';
pole[i][61]= ' ';
pole[0][j] = ' ';
pole[21][j] = ' ';

int maluj = rand()%100;
if(maluj < 90) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}

void wypiszpole(char pole[height])
{
cout <<endl;
cout<< " GAME OF LIFE";
cout << endl << endl;
cout << "+------------------------------------------------------------+"
<<endl;
for(int i=0; i<height; i++)
{
cout << "|";
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << "|" << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
cout << endl;
cout << " R - reset, Q - quit, K - step " << endl;
}









share|improve this question

























  • Please don't post links to external resources. They might go down in the future making the resources inaccessible.

    – Hristijan Gjorshevski
    Nov 23 '18 at 20:21














-1












-1








-1








I have probably a simple problem,
this is what i get:
output



My point is to have a table which contains space character in the first and the last column and in the first and the last row, and in all other places the random value which is "o" or space character.
Also, I want to "close" this board with the mark of "|" at the end of every row, just to make it look neat.



In function generujpole there are 4 lines (e.g. pole[i][0] = ' ';) which I thought would solve the problem, but it didn't work for the last column.



What is wrong and how should it be written properly?



The code:



#include <iostream>
#include <ctime>
using namespace std;

const int width = 62;
const int height = 22;

void generujpole(char pole[height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
pole[i][0] = ' ';
pole[i][61]= ' ';
pole[0][j] = ' ';
pole[21][j] = ' ';

int maluj = rand()%100;
if(maluj < 90) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}

void wypiszpole(char pole[height])
{
cout <<endl;
cout<< " GAME OF LIFE";
cout << endl << endl;
cout << "+------------------------------------------------------------+"
<<endl;
for(int i=0; i<height; i++)
{
cout << "|";
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << "|" << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
cout << endl;
cout << " R - reset, Q - quit, K - step " << endl;
}









share|improve this question
















I have probably a simple problem,
this is what i get:
output



My point is to have a table which contains space character in the first and the last column and in the first and the last row, and in all other places the random value which is "o" or space character.
Also, I want to "close" this board with the mark of "|" at the end of every row, just to make it look neat.



In function generujpole there are 4 lines (e.g. pole[i][0] = ' ';) which I thought would solve the problem, but it didn't work for the last column.



What is wrong and how should it be written properly?



The code:



#include <iostream>
#include <ctime>
using namespace std;

const int width = 62;
const int height = 22;

void generujpole(char pole[height])
{
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
pole[i][0] = ' ';
pole[i][61]= ' ';
pole[0][j] = ' ';
pole[21][j] = ' ';

int maluj = rand()%100;
if(maluj < 90) pole[i][j] = ' ';
else pole[i][j] = 'o';
}
}
}

void wypiszpole(char pole[height])
{
cout <<endl;
cout<< " GAME OF LIFE";
cout << endl << endl;
cout << "+------------------------------------------------------------+"
<<endl;
for(int i=0; i<height; i++)
{
cout << "|";
for(int j=0; j<width; j++)
{
cout << pole[i][j];
}
cout << "|" << endl;
}
cout << "+------------------------------------------------------------+"<<endl;
cout << endl;
cout << " R - reset, Q - quit, K - step " << endl;
}






c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:46







obeeey

















asked Nov 23 '18 at 20:13









obeeeyobeeey

76




76













  • Please don't post links to external resources. They might go down in the future making the resources inaccessible.

    – Hristijan Gjorshevski
    Nov 23 '18 at 20:21



















  • Please don't post links to external resources. They might go down in the future making the resources inaccessible.

    – Hristijan Gjorshevski
    Nov 23 '18 at 20:21

















Please don't post links to external resources. They might go down in the future making the resources inaccessible.

– Hristijan Gjorshevski
Nov 23 '18 at 20:21





Please don't post links to external resources. They might go down in the future making the resources inaccessible.

– Hristijan Gjorshevski
Nov 23 '18 at 20:21












2 Answers
2






active

oldest

votes


















1














You can add two '-'s to your top and bottom bars, e.g.:



cout << "+--------------------------------------------------------------+"


instead of



cout << "+------------------------------------------------------------+"


But it seems short-sighted design to hard-code all of this. You may wish to change the size in the future, at which time you'd have to re-write much of your code.



Consider starting to approach design dynamically by drawing these bars with:



cout << '+';

for (int i = 0; i < width; i++)
{
cout << '-';
}

cout << '+' << endl;


As for border spacing along the right edge, it seems you're combining your border-printing desires with grid-generating logic. Keep the two separate: if you wish to have a border at the beginning and end of each row, print an extra space in wypiszpole rather than attempting to manipulate the game grid by hand (again using hard-coded values) in generujpole. Assuming this is Conway's Game of Life, you'll be overwriting these "border" elements after an iteration or using roundabout logic to avoid doing so.



Additionally, it's easy to get width and height mixed up: generally, the outer array is the height and the inner array is the width. Your code confuses the two, using width as the outer loop in wypiszpole but height as the outer loop in generujpole.



Below is a full example to illustrate the above points. Try changing height and width to see how it responds.



As a final note, you may consider using vectors and classes; this will improve encapsulation of functions and abstract away many array size and memory management concerns.



#include <iostream>
using namespace std;

void generujpole(int width, int height, char **pole)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
pole[i][j] = rand() % 100 < 90 ? ' ' : 'o';
}
}
}

void wypiszpole(int width, int height, char **pole)
{
cout << endl;

for (int i = 0; i < width / 2 - 4; i++)
{
cout << ' ';
}

cout << "GAME OF LIFE";
cout << endl << endl;

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl;

for (int i = 0; i < height; i++)
{
cout << "| ";

for (int j = 0; j < width; j++)
{
cout << pole[i][j];
}

cout << " |" << endl;
}

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl << endl;

for (int i = 0; i < width / 2 - 17; i++)
{
cout << ' ';
}

cout << "R - reset, Q - quit, K - step" << endl;
}

int main()
{
const int width = 40;
const int height = 15;
char** pole = new char*[height];

for (int i = 0; i < height; i++)
{
pole[i] = new char[width];
}

generujpole(width, height, pole);
wypiszpole(width, height, pole);

for (int i = 0; i < height; i++)
{
delete pole[i];
}

delete pole;
return 0;
}


Output for a few example dimensions:



  GAME OF LIFE

+--------------+
| o o |
| o |
| |
| o o |
+--------------+

R - reset, Q - quit, K - step


                GAME OF LIFE

+------------------------------------------+
| o o o o |
| o o o o |
| o o o o o |
| o o o |
| o o o o |
| o oo o o |
| o o |
| o o oo oo o |
| o o o o o |
| oo o o o |
| o o o o o o o |
| ooo o |
| oo o o o o |
| o o o o |
| o |
+------------------------------------------+

R - reset, Q - quit, K - step


                                    GAME OF LIFE

+----------------------------------------------------------------------------------+
| o o o o o o o o |
| o o o o o o o o |
| o o o o o oo o o |
| o o o o oo oo o |
| o o o o o oo o o o |
| o o o o o o o ooo o |
| oo o o o o o o o o |
| o o o ooo o o o |
| o o o o o o o o |
| o o o o o o |
| o oo o o o o |
| o o oo o o o o o o o |
| o o o o o o o o o o o o o |
| oo o o o o |
| o oo o o o o |
| o oo o o o |
| o o o o o o o o o |
| o o oo o o o o o oo o o |
| o o o o oo o o o o |
| o o o o o o oo |
| o o o o o o oo o o |
| o o o o oo o o o oo |
| o o o o o |
| o o o o o o o o o |
| o o o o o o o |
+----------------------------------------------------------------------------------+

R - reset, Q - quit, K - step


Try it!






share|improve this answer


























  • Thank you, but the last column isn't empty still. Do you know what else is wrong?

    – obeeey
    Nov 23 '18 at 20:30











  • Take a look at my update and let me know if I'm missing any additional concerns.

    – ggorlen
    Nov 23 '18 at 20:56











  • Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

    – obeeey
    Nov 24 '18 at 11:54













  • Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

    – ggorlen
    Nov 24 '18 at 17:31



















0














@obeeey: In your function generujpol, you have to split the for loo into two separate loops as follow:



// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}


An appropriate way to have a line filled with '+-----+' is to proceed as follow:



 // Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;





share|improve this answer
























  • Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

    – obeeey
    Nov 24 '18 at 11:59













  • Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

    – obeeey
    Nov 24 '18 at 13:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452482%2fhow-can-i-add-space-character-only-to-the-first-last-column-and-row-in-an-two%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can add two '-'s to your top and bottom bars, e.g.:



cout << "+--------------------------------------------------------------+"


instead of



cout << "+------------------------------------------------------------+"


But it seems short-sighted design to hard-code all of this. You may wish to change the size in the future, at which time you'd have to re-write much of your code.



Consider starting to approach design dynamically by drawing these bars with:



cout << '+';

for (int i = 0; i < width; i++)
{
cout << '-';
}

cout << '+' << endl;


As for border spacing along the right edge, it seems you're combining your border-printing desires with grid-generating logic. Keep the two separate: if you wish to have a border at the beginning and end of each row, print an extra space in wypiszpole rather than attempting to manipulate the game grid by hand (again using hard-coded values) in generujpole. Assuming this is Conway's Game of Life, you'll be overwriting these "border" elements after an iteration or using roundabout logic to avoid doing so.



Additionally, it's easy to get width and height mixed up: generally, the outer array is the height and the inner array is the width. Your code confuses the two, using width as the outer loop in wypiszpole but height as the outer loop in generujpole.



Below is a full example to illustrate the above points. Try changing height and width to see how it responds.



As a final note, you may consider using vectors and classes; this will improve encapsulation of functions and abstract away many array size and memory management concerns.



#include <iostream>
using namespace std;

void generujpole(int width, int height, char **pole)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
pole[i][j] = rand() % 100 < 90 ? ' ' : 'o';
}
}
}

void wypiszpole(int width, int height, char **pole)
{
cout << endl;

for (int i = 0; i < width / 2 - 4; i++)
{
cout << ' ';
}

cout << "GAME OF LIFE";
cout << endl << endl;

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl;

for (int i = 0; i < height; i++)
{
cout << "| ";

for (int j = 0; j < width; j++)
{
cout << pole[i][j];
}

cout << " |" << endl;
}

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl << endl;

for (int i = 0; i < width / 2 - 17; i++)
{
cout << ' ';
}

cout << "R - reset, Q - quit, K - step" << endl;
}

int main()
{
const int width = 40;
const int height = 15;
char** pole = new char*[height];

for (int i = 0; i < height; i++)
{
pole[i] = new char[width];
}

generujpole(width, height, pole);
wypiszpole(width, height, pole);

for (int i = 0; i < height; i++)
{
delete pole[i];
}

delete pole;
return 0;
}


Output for a few example dimensions:



  GAME OF LIFE

+--------------+
| o o |
| o |
| |
| o o |
+--------------+

R - reset, Q - quit, K - step


                GAME OF LIFE

+------------------------------------------+
| o o o o |
| o o o o |
| o o o o o |
| o o o |
| o o o o |
| o oo o o |
| o o |
| o o oo oo o |
| o o o o o |
| oo o o o |
| o o o o o o o |
| ooo o |
| oo o o o o |
| o o o o |
| o |
+------------------------------------------+

R - reset, Q - quit, K - step


                                    GAME OF LIFE

+----------------------------------------------------------------------------------+
| o o o o o o o o |
| o o o o o o o o |
| o o o o o oo o o |
| o o o o oo oo o |
| o o o o o oo o o o |
| o o o o o o o ooo o |
| oo o o o o o o o o |
| o o o ooo o o o |
| o o o o o o o o |
| o o o o o o |
| o oo o o o o |
| o o oo o o o o o o o |
| o o o o o o o o o o o o o |
| oo o o o o |
| o oo o o o o |
| o oo o o o |
| o o o o o o o o o |
| o o oo o o o o o oo o o |
| o o o o oo o o o o |
| o o o o o o oo |
| o o o o o o oo o o |
| o o o o oo o o o oo |
| o o o o o |
| o o o o o o o o o |
| o o o o o o o |
+----------------------------------------------------------------------------------+

R - reset, Q - quit, K - step


Try it!






share|improve this answer


























  • Thank you, but the last column isn't empty still. Do you know what else is wrong?

    – obeeey
    Nov 23 '18 at 20:30











  • Take a look at my update and let me know if I'm missing any additional concerns.

    – ggorlen
    Nov 23 '18 at 20:56











  • Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

    – obeeey
    Nov 24 '18 at 11:54













  • Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

    – ggorlen
    Nov 24 '18 at 17:31
















1














You can add two '-'s to your top and bottom bars, e.g.:



cout << "+--------------------------------------------------------------+"


instead of



cout << "+------------------------------------------------------------+"


But it seems short-sighted design to hard-code all of this. You may wish to change the size in the future, at which time you'd have to re-write much of your code.



Consider starting to approach design dynamically by drawing these bars with:



cout << '+';

for (int i = 0; i < width; i++)
{
cout << '-';
}

cout << '+' << endl;


As for border spacing along the right edge, it seems you're combining your border-printing desires with grid-generating logic. Keep the two separate: if you wish to have a border at the beginning and end of each row, print an extra space in wypiszpole rather than attempting to manipulate the game grid by hand (again using hard-coded values) in generujpole. Assuming this is Conway's Game of Life, you'll be overwriting these "border" elements after an iteration or using roundabout logic to avoid doing so.



Additionally, it's easy to get width and height mixed up: generally, the outer array is the height and the inner array is the width. Your code confuses the two, using width as the outer loop in wypiszpole but height as the outer loop in generujpole.



Below is a full example to illustrate the above points. Try changing height and width to see how it responds.



As a final note, you may consider using vectors and classes; this will improve encapsulation of functions and abstract away many array size and memory management concerns.



#include <iostream>
using namespace std;

void generujpole(int width, int height, char **pole)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
pole[i][j] = rand() % 100 < 90 ? ' ' : 'o';
}
}
}

void wypiszpole(int width, int height, char **pole)
{
cout << endl;

for (int i = 0; i < width / 2 - 4; i++)
{
cout << ' ';
}

cout << "GAME OF LIFE";
cout << endl << endl;

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl;

for (int i = 0; i < height; i++)
{
cout << "| ";

for (int j = 0; j < width; j++)
{
cout << pole[i][j];
}

cout << " |" << endl;
}

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl << endl;

for (int i = 0; i < width / 2 - 17; i++)
{
cout << ' ';
}

cout << "R - reset, Q - quit, K - step" << endl;
}

int main()
{
const int width = 40;
const int height = 15;
char** pole = new char*[height];

for (int i = 0; i < height; i++)
{
pole[i] = new char[width];
}

generujpole(width, height, pole);
wypiszpole(width, height, pole);

for (int i = 0; i < height; i++)
{
delete pole[i];
}

delete pole;
return 0;
}


Output for a few example dimensions:



  GAME OF LIFE

+--------------+
| o o |
| o |
| |
| o o |
+--------------+

R - reset, Q - quit, K - step


                GAME OF LIFE

+------------------------------------------+
| o o o o |
| o o o o |
| o o o o o |
| o o o |
| o o o o |
| o oo o o |
| o o |
| o o oo oo o |
| o o o o o |
| oo o o o |
| o o o o o o o |
| ooo o |
| oo o o o o |
| o o o o |
| o |
+------------------------------------------+

R - reset, Q - quit, K - step


                                    GAME OF LIFE

+----------------------------------------------------------------------------------+
| o o o o o o o o |
| o o o o o o o o |
| o o o o o oo o o |
| o o o o oo oo o |
| o o o o o oo o o o |
| o o o o o o o ooo o |
| oo o o o o o o o o |
| o o o ooo o o o |
| o o o o o o o o |
| o o o o o o |
| o oo o o o o |
| o o oo o o o o o o o |
| o o o o o o o o o o o o o |
| oo o o o o |
| o oo o o o o |
| o oo o o o |
| o o o o o o o o o |
| o o oo o o o o o oo o o |
| o o o o oo o o o o |
| o o o o o o oo |
| o o o o o o oo o o |
| o o o o oo o o o oo |
| o o o o o |
| o o o o o o o o o |
| o o o o o o o |
+----------------------------------------------------------------------------------+

R - reset, Q - quit, K - step


Try it!






share|improve this answer


























  • Thank you, but the last column isn't empty still. Do you know what else is wrong?

    – obeeey
    Nov 23 '18 at 20:30











  • Take a look at my update and let me know if I'm missing any additional concerns.

    – ggorlen
    Nov 23 '18 at 20:56











  • Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

    – obeeey
    Nov 24 '18 at 11:54













  • Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

    – ggorlen
    Nov 24 '18 at 17:31














1












1








1







You can add two '-'s to your top and bottom bars, e.g.:



cout << "+--------------------------------------------------------------+"


instead of



cout << "+------------------------------------------------------------+"


But it seems short-sighted design to hard-code all of this. You may wish to change the size in the future, at which time you'd have to re-write much of your code.



Consider starting to approach design dynamically by drawing these bars with:



cout << '+';

for (int i = 0; i < width; i++)
{
cout << '-';
}

cout << '+' << endl;


As for border spacing along the right edge, it seems you're combining your border-printing desires with grid-generating logic. Keep the two separate: if you wish to have a border at the beginning and end of each row, print an extra space in wypiszpole rather than attempting to manipulate the game grid by hand (again using hard-coded values) in generujpole. Assuming this is Conway's Game of Life, you'll be overwriting these "border" elements after an iteration or using roundabout logic to avoid doing so.



Additionally, it's easy to get width and height mixed up: generally, the outer array is the height and the inner array is the width. Your code confuses the two, using width as the outer loop in wypiszpole but height as the outer loop in generujpole.



Below is a full example to illustrate the above points. Try changing height and width to see how it responds.



As a final note, you may consider using vectors and classes; this will improve encapsulation of functions and abstract away many array size and memory management concerns.



#include <iostream>
using namespace std;

void generujpole(int width, int height, char **pole)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
pole[i][j] = rand() % 100 < 90 ? ' ' : 'o';
}
}
}

void wypiszpole(int width, int height, char **pole)
{
cout << endl;

for (int i = 0; i < width / 2 - 4; i++)
{
cout << ' ';
}

cout << "GAME OF LIFE";
cout << endl << endl;

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl;

for (int i = 0; i < height; i++)
{
cout << "| ";

for (int j = 0; j < width; j++)
{
cout << pole[i][j];
}

cout << " |" << endl;
}

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl << endl;

for (int i = 0; i < width / 2 - 17; i++)
{
cout << ' ';
}

cout << "R - reset, Q - quit, K - step" << endl;
}

int main()
{
const int width = 40;
const int height = 15;
char** pole = new char*[height];

for (int i = 0; i < height; i++)
{
pole[i] = new char[width];
}

generujpole(width, height, pole);
wypiszpole(width, height, pole);

for (int i = 0; i < height; i++)
{
delete pole[i];
}

delete pole;
return 0;
}


Output for a few example dimensions:



  GAME OF LIFE

+--------------+
| o o |
| o |
| |
| o o |
+--------------+

R - reset, Q - quit, K - step


                GAME OF LIFE

+------------------------------------------+
| o o o o |
| o o o o |
| o o o o o |
| o o o |
| o o o o |
| o oo o o |
| o o |
| o o oo oo o |
| o o o o o |
| oo o o o |
| o o o o o o o |
| ooo o |
| oo o o o o |
| o o o o |
| o |
+------------------------------------------+

R - reset, Q - quit, K - step


                                    GAME OF LIFE

+----------------------------------------------------------------------------------+
| o o o o o o o o |
| o o o o o o o o |
| o o o o o oo o o |
| o o o o oo oo o |
| o o o o o oo o o o |
| o o o o o o o ooo o |
| oo o o o o o o o o |
| o o o ooo o o o |
| o o o o o o o o |
| o o o o o o |
| o oo o o o o |
| o o oo o o o o o o o |
| o o o o o o o o o o o o o |
| oo o o o o |
| o oo o o o o |
| o oo o o o |
| o o o o o o o o o |
| o o oo o o o o o oo o o |
| o o o o oo o o o o |
| o o o o o o oo |
| o o o o o o oo o o |
| o o o o oo o o o oo |
| o o o o o |
| o o o o o o o o o |
| o o o o o o o |
+----------------------------------------------------------------------------------+

R - reset, Q - quit, K - step


Try it!






share|improve this answer















You can add two '-'s to your top and bottom bars, e.g.:



cout << "+--------------------------------------------------------------+"


instead of



cout << "+------------------------------------------------------------+"


But it seems short-sighted design to hard-code all of this. You may wish to change the size in the future, at which time you'd have to re-write much of your code.



Consider starting to approach design dynamically by drawing these bars with:



cout << '+';

for (int i = 0; i < width; i++)
{
cout << '-';
}

cout << '+' << endl;


As for border spacing along the right edge, it seems you're combining your border-printing desires with grid-generating logic. Keep the two separate: if you wish to have a border at the beginning and end of each row, print an extra space in wypiszpole rather than attempting to manipulate the game grid by hand (again using hard-coded values) in generujpole. Assuming this is Conway's Game of Life, you'll be overwriting these "border" elements after an iteration or using roundabout logic to avoid doing so.



Additionally, it's easy to get width and height mixed up: generally, the outer array is the height and the inner array is the width. Your code confuses the two, using width as the outer loop in wypiszpole but height as the outer loop in generujpole.



Below is a full example to illustrate the above points. Try changing height and width to see how it responds.



As a final note, you may consider using vectors and classes; this will improve encapsulation of functions and abstract away many array size and memory management concerns.



#include <iostream>
using namespace std;

void generujpole(int width, int height, char **pole)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
pole[i][j] = rand() % 100 < 90 ? ' ' : 'o';
}
}
}

void wypiszpole(int width, int height, char **pole)
{
cout << endl;

for (int i = 0; i < width / 2 - 4; i++)
{
cout << ' ';
}

cout << "GAME OF LIFE";
cout << endl << endl;

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl;

for (int i = 0; i < height; i++)
{
cout << "| ";

for (int j = 0; j < width; j++)
{
cout << pole[i][j];
}

cout << " |" << endl;
}

cout << '+';

for (int i = 0; i < width + 2; i++)
{
cout << '-';
}

cout << '+' << endl << endl;

for (int i = 0; i < width / 2 - 17; i++)
{
cout << ' ';
}

cout << "R - reset, Q - quit, K - step" << endl;
}

int main()
{
const int width = 40;
const int height = 15;
char** pole = new char*[height];

for (int i = 0; i < height; i++)
{
pole[i] = new char[width];
}

generujpole(width, height, pole);
wypiszpole(width, height, pole);

for (int i = 0; i < height; i++)
{
delete pole[i];
}

delete pole;
return 0;
}


Output for a few example dimensions:



  GAME OF LIFE

+--------------+
| o o |
| o |
| |
| o o |
+--------------+

R - reset, Q - quit, K - step


                GAME OF LIFE

+------------------------------------------+
| o o o o |
| o o o o |
| o o o o o |
| o o o |
| o o o o |
| o oo o o |
| o o |
| o o oo oo o |
| o o o o o |
| oo o o o |
| o o o o o o o |
| ooo o |
| oo o o o o |
| o o o o |
| o |
+------------------------------------------+

R - reset, Q - quit, K - step


                                    GAME OF LIFE

+----------------------------------------------------------------------------------+
| o o o o o o o o |
| o o o o o o o o |
| o o o o o oo o o |
| o o o o oo oo o |
| o o o o o oo o o o |
| o o o o o o o ooo o |
| oo o o o o o o o o |
| o o o ooo o o o |
| o o o o o o o o |
| o o o o o o |
| o oo o o o o |
| o o oo o o o o o o o |
| o o o o o o o o o o o o o |
| oo o o o o |
| o oo o o o o |
| o oo o o o |
| o o o o o o o o o |
| o o oo o o o o o oo o o |
| o o o o oo o o o o |
| o o o o o o oo |
| o o o o o o oo o o |
| o o o o oo o o o oo |
| o o o o o |
| o o o o o o o o o |
| o o o o o o o |
+----------------------------------------------------------------------------------+

R - reset, Q - quit, K - step


Try it!







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 23:51

























answered Nov 23 '18 at 20:25









ggorlenggorlen

7,1883825




7,1883825













  • Thank you, but the last column isn't empty still. Do you know what else is wrong?

    – obeeey
    Nov 23 '18 at 20:30











  • Take a look at my update and let me know if I'm missing any additional concerns.

    – ggorlen
    Nov 23 '18 at 20:56











  • Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

    – obeeey
    Nov 24 '18 at 11:54













  • Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

    – ggorlen
    Nov 24 '18 at 17:31



















  • Thank you, but the last column isn't empty still. Do you know what else is wrong?

    – obeeey
    Nov 23 '18 at 20:30











  • Take a look at my update and let me know if I'm missing any additional concerns.

    – ggorlen
    Nov 23 '18 at 20:56











  • Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

    – obeeey
    Nov 24 '18 at 11:54













  • Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

    – ggorlen
    Nov 24 '18 at 17:31

















Thank you, but the last column isn't empty still. Do you know what else is wrong?

– obeeey
Nov 23 '18 at 20:30





Thank you, but the last column isn't empty still. Do you know what else is wrong?

– obeeey
Nov 23 '18 at 20:30













Take a look at my update and let me know if I'm missing any additional concerns.

– ggorlen
Nov 23 '18 at 20:56





Take a look at my update and let me know if I'm missing any additional concerns.

– ggorlen
Nov 23 '18 at 20:56













Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

– obeeey
Nov 24 '18 at 11:54







Thank you so much for your answer. It's really helpful, it simplified what I've already had. The problem is that I do not use pointers and dynamic arrays yet so this is quite confusing for me. Is there a way to do this without this? Thanks again.

– obeeey
Nov 24 '18 at 11:54















Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

– ggorlen
Nov 24 '18 at 17:31





Sure, you can always hard-code the dimensions, use the first part of my answer and the added spaces in the printing routine and keep the dynamic memory stuff in mind for the future. Feel free to accept the answer if it solves your problem.

– ggorlen
Nov 24 '18 at 17:31













0














@obeeey: In your function generujpol, you have to split the for loo into two separate loops as follow:



// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}


An appropriate way to have a line filled with '+-----+' is to proceed as follow:



 // Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;





share|improve this answer
























  • Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

    – obeeey
    Nov 24 '18 at 11:59













  • Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

    – obeeey
    Nov 24 '18 at 13:32
















0














@obeeey: In your function generujpol, you have to split the for loo into two separate loops as follow:



// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}


An appropriate way to have a line filled with '+-----+' is to proceed as follow:



 // Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;





share|improve this answer
























  • Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

    – obeeey
    Nov 24 '18 at 11:59













  • Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

    – obeeey
    Nov 24 '18 at 13:32














0












0








0







@obeeey: In your function generujpol, you have to split the for loo into two separate loops as follow:



// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}


An appropriate way to have a line filled with '+-----+' is to proceed as follow:



 // Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;





share|improve this answer













@obeeey: In your function generujpol, you have to split the for loo into two separate loops as follow:



// We fill the first and last rows with 'space' character
for(int i=0; i < HEIGHT; i++)
{
pole[0][i] = ' ';
pole[width-1][i] = ' ';
}
//Now, we fill the first and last column with 'space character
for(int i=0; i < width; i++)
{
pole[i][0] = ' ';
pole[i][height-1] = ' ';
}


An appropriate way to have a line filled with '+-----+' is to proceed as follow:



 // Create a string of length 'height' filled with '-'
string line(height, '-');
// then add '+' at the back and front
line = "+" + line + "+";
// now you can comfortably write.
cout << line << endl;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 21:07









eapetchoeapetcho

42927




42927













  • Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

    – obeeey
    Nov 24 '18 at 11:59













  • Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

    – obeeey
    Nov 24 '18 at 13:32



















  • Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

    – obeeey
    Nov 24 '18 at 11:59













  • Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

    – obeeey
    Nov 24 '18 at 13:32

















Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

– obeeey
Nov 24 '18 at 11:59







Thanks! That's a simple solution for those lines, really helpful. Unfortunalety, adding two for loops didn't help or I'm doing this wrong. Could you expand this idea? How should I connect those loops with the random values?

– obeeey
Nov 24 '18 at 11:59















Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

– obeeey
Nov 24 '18 at 13:32





Update: I've added those two loops in function generujpole after loop generating random values to the array and now it works perfectly.

– obeeey
Nov 24 '18 at 13:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452482%2fhow-can-i-add-space-character-only-to-the-first-last-column-and-row-in-an-two%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft