2D array size specified by the user












2















So, I have a code when I prompt the user to enter a number, I want the size of the 2D array to be changed according to what the user entered, for example:



Enter a number between 1 and 12:
3



The 2D array then will change according to that size to become a 3x3 2D array, I hope I am as clear as I could be, I would really appreciate it if someone can please guide me to a solution, Thank you everyone !!



#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;



void displayOverview ();

void playOrQuit();

void promptNumber();

void fillUserArray(char grid);

int main(){

displayOverview();

playOrQuit();

promptNumber();


return 0;
}

void displayOverview(){

}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";
exit(0);
}if(playOrNot != "p" && playOrNot != "q"){
cout << "Invalid entry !!n";
exit(0);
}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
if(Umain <= 12){
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number: ";
cin >> Atemp;
}
}else{
cout << "Not within limit :(n";
}
}while (Answer == 'y');
}

void fillUserGrid (char grid){

for( int row = 0; row < Umain; row++ ) {
for(int col = 0; col < Umain; col++){
grid[row][col] = userInput.at( row * Umain + col );
}
}
}

void outputUserGrid(char grid){

for (int row = 0; row < Umain; row++){
for(int col = 0; col < Umain; col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}









share|improve this question




















  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Ken Y-N
    Nov 21 '18 at 4:47











  • @KenY-N I don't agree because OP didn't explicitly ask for new.

    – Swordfish
    Nov 21 '18 at 4:59






  • 1





    If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

    – Swordfish
    Nov 21 '18 at 5:01













  • @Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

    – Ken Y-N
    Nov 21 '18 at 5:03













  • This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

    – paddy
    Nov 21 '18 at 5:18


















2















So, I have a code when I prompt the user to enter a number, I want the size of the 2D array to be changed according to what the user entered, for example:



Enter a number between 1 and 12:
3



The 2D array then will change according to that size to become a 3x3 2D array, I hope I am as clear as I could be, I would really appreciate it if someone can please guide me to a solution, Thank you everyone !!



#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;



void displayOverview ();

void playOrQuit();

void promptNumber();

void fillUserArray(char grid);

int main(){

displayOverview();

playOrQuit();

promptNumber();


return 0;
}

void displayOverview(){

}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";
exit(0);
}if(playOrNot != "p" && playOrNot != "q"){
cout << "Invalid entry !!n";
exit(0);
}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
if(Umain <= 12){
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number: ";
cin >> Atemp;
}
}else{
cout << "Not within limit :(n";
}
}while (Answer == 'y');
}

void fillUserGrid (char grid){

for( int row = 0; row < Umain; row++ ) {
for(int col = 0; col < Umain; col++){
grid[row][col] = userInput.at( row * Umain + col );
}
}
}

void outputUserGrid(char grid){

for (int row = 0; row < Umain; row++){
for(int col = 0; col < Umain; col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}









share|improve this question




















  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Ken Y-N
    Nov 21 '18 at 4:47











  • @KenY-N I don't agree because OP didn't explicitly ask for new.

    – Swordfish
    Nov 21 '18 at 4:59






  • 1





    If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

    – Swordfish
    Nov 21 '18 at 5:01













  • @Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

    – Ken Y-N
    Nov 21 '18 at 5:03













  • This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

    – paddy
    Nov 21 '18 at 5:18
















2












2








2








So, I have a code when I prompt the user to enter a number, I want the size of the 2D array to be changed according to what the user entered, for example:



Enter a number between 1 and 12:
3



The 2D array then will change according to that size to become a 3x3 2D array, I hope I am as clear as I could be, I would really appreciate it if someone can please guide me to a solution, Thank you everyone !!



#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;



void displayOverview ();

void playOrQuit();

void promptNumber();

void fillUserArray(char grid);

int main(){

displayOverview();

playOrQuit();

promptNumber();


return 0;
}

void displayOverview(){

}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";
exit(0);
}if(playOrNot != "p" && playOrNot != "q"){
cout << "Invalid entry !!n";
exit(0);
}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
if(Umain <= 12){
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number: ";
cin >> Atemp;
}
}else{
cout << "Not within limit :(n";
}
}while (Answer == 'y');
}

void fillUserGrid (char grid){

for( int row = 0; row < Umain; row++ ) {
for(int col = 0; col < Umain; col++){
grid[row][col] = userInput.at( row * Umain + col );
}
}
}

void outputUserGrid(char grid){

for (int row = 0; row < Umain; row++){
for(int col = 0; col < Umain; col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}









share|improve this question
















So, I have a code when I prompt the user to enter a number, I want the size of the 2D array to be changed according to what the user entered, for example:



Enter a number between 1 and 12:
3



The 2D array then will change according to that size to become a 3x3 2D array, I hope I am as clear as I could be, I would really appreciate it if someone can please guide me to a solution, Thank you everyone !!



#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>

using namespace std;

double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';
int Umain;



void displayOverview ();

void playOrQuit();

void promptNumber();

void fillUserArray(char grid);

int main(){

displayOverview();

playOrQuit();

promptNumber();


return 0;
}

void displayOverview(){

}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";
exit(0);
}if(playOrNot != "p" && playOrNot != "q"){
cout << "Invalid entry !!n";
exit(0);
}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
if(Umain <= 12){
for (Utemp = Umain; Utemp > 0; Utemp--){
cout << "Please enter a number: ";
cin >> Atemp;
}
}else{
cout << "Not within limit :(n";
}
}while (Answer == 'y');
}

void fillUserGrid (char grid){

for( int row = 0; row < Umain; row++ ) {
for(int col = 0; col < Umain; col++){
grid[row][col] = userInput.at( row * Umain + col );
}
}
}

void outputUserGrid(char grid){

for (int row = 0; row < Umain; row++){
for(int col = 0; col < Umain; col++){
cout << grid[row][col]<<" ";
}
cout << endl;
}
}






c++ arrays multidimensional-array






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 4:49







DanielAA9

















asked Nov 21 '18 at 4:40









DanielAA9DanielAA9

375




375








  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Ken Y-N
    Nov 21 '18 at 4:47











  • @KenY-N I don't agree because OP didn't explicitly ask for new.

    – Swordfish
    Nov 21 '18 at 4:59






  • 1





    If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

    – Swordfish
    Nov 21 '18 at 5:01













  • @Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

    – Ken Y-N
    Nov 21 '18 at 5:03













  • This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

    – paddy
    Nov 21 '18 at 5:18
















  • 1





    Possible duplicate of How do I declare a 2d array in C++ using new?

    – Ken Y-N
    Nov 21 '18 at 4:47











  • @KenY-N I don't agree because OP didn't explicitly ask for new.

    – Swordfish
    Nov 21 '18 at 4:59






  • 1





    If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

    – Swordfish
    Nov 21 '18 at 5:01













  • @Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

    – Ken Y-N
    Nov 21 '18 at 5:03













  • This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

    – paddy
    Nov 21 '18 at 5:18










1




1





Possible duplicate of How do I declare a 2d array in C++ using new?

– Ken Y-N
Nov 21 '18 at 4:47





Possible duplicate of How do I declare a 2d array in C++ using new?

– Ken Y-N
Nov 21 '18 at 4:47













@KenY-N I don't agree because OP didn't explicitly ask for new.

– Swordfish
Nov 21 '18 at 4:59





@KenY-N I don't agree because OP didn't explicitly ask for new.

– Swordfish
Nov 21 '18 at 4:59




1




1





If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

– Swordfish
Nov 21 '18 at 5:01







If you want an array-like thingy that you can resize, please use std::vector<char> grid;, set its size to rows * columns and access it using grid[y * columns + x];. Thanks.

– Swordfish
Nov 21 '18 at 5:01















@Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

– Ken Y-N
Nov 21 '18 at 5:03







@Swordfish and neither did they ask for a 1D std::vector - they have grid[row][col] (although that function's parameter is wrong...).

– Ken Y-N
Nov 21 '18 at 5:03















This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

– paddy
Nov 21 '18 at 5:18







This feels like XY Problem. I've answered accordingly. If it turns out you absolutely need a data structure that is the exact size you need, then the duplicate question mentioned earlier provides solutions... But scroll down to some of the lesser-voted answers to find the good solutions.

– paddy
Nov 21 '18 at 5:18














1 Answer
1






active

oldest

votes


















2














It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.



The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.






share|improve this answer
























  • I see, thank you for helping me out

    – DanielAA9
    Nov 21 '18 at 5:17











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%2f53405365%2f2d-array-size-specified-by-the-user%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.



The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.






share|improve this answer
























  • I see, thank you for helping me out

    – DanielAA9
    Nov 21 '18 at 5:17
















2














It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.



The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.






share|improve this answer
























  • I see, thank you for helping me out

    – DanielAA9
    Nov 21 '18 at 5:17














2












2








2







It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.



The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.






share|improve this answer













It's quite legitimate to just use a 12x12 array for your game. When the user wants to play on a smaller grid, you can just use part of it.



The only times this might be an issue are if you're running into L1/L2 cache issues for performance-critical programs, or if you're storing lots of these grids and need to reduce memory use. I would be surprised if either of these scenarios applies to you.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 5:08









paddypaddy

42.7k53176




42.7k53176













  • I see, thank you for helping me out

    – DanielAA9
    Nov 21 '18 at 5:17



















  • I see, thank you for helping me out

    – DanielAA9
    Nov 21 '18 at 5:17

















I see, thank you for helping me out

– DanielAA9
Nov 21 '18 at 5:17





I see, thank you for helping me out

– DanielAA9
Nov 21 '18 at 5:17


















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%2f53405365%2f2d-array-size-specified-by-the-user%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?