1,105,402 Community Members

2d array replace value

Member Avatar
tKc
Newbie Poster
23 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i have my program outputting a number grid that is nxn. i have it using a 2d array to output my grid but the issue im having is that when the user wants to input a number that is in the nxn grid, the value is not being replaced by either an x or and o. essentially its a big tic tac toe board that takes 5 to win and not 3. so lets say it was a 10x10 board, my program can output this
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
and now it requests the user for a number, lets say 74, but when they enter the number it does not replace the letter 74 on the grid
My thinking is that i have to make another 2d array so that it stores the users input and then outputs the new grid but with the user's input as an x or an o over said number.(my code thus far and i know it doesn't do much else other than output a grid, any help/suggestions on how to fix it would be appreciated tho)

#include <iostream>
using namespace std;
int main()
{
double n;
do {
cout << "Inpute Board size between 10 and 50" << endl;
cin >> n;
}while(n < 10 || n>50);

do{
int point;
int game_board[99][99];
int value(1);
for(int x=0; x<n; x++) {
for(int y=0; y<n; y++) {
game_board[x][y] = value++;
}
}
for(int x=0; x<n; x++) {
cout << endl;
for(int y=0; y<n; y++) {
if(game_board[x][y] <= 9){
cout << game_board[x][y]<< " ";
}
else{
cout<<game_board[x][y]<<" ";
}
}
}
cout << endl;
cin >> point;
}while(true);

system("PAUSE");
return 0;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

First, format your code so we can follow it.

Replace the value in the array with -1 for X and -2 for O. Then during output, display the letters instead of numbers for negative values.

Member Avatar
tKc
Newbie Poster
23 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

First, format your code so we can follow it.

Replace the value in the array with -1 for X and -2 for O. Then during output, display the letters instead of numbers for negative values.

okay so i managed to have it output a -1 when the user enters a number in the grid but the problem im having now is that the inputed values are not staying from one array to another. like when the user enters, lets say 74, it puts a -1 in that spot,it then outputs the grid again and requests another input (which it should) but this grid has the number 74 and not a -1, so the replacements are not staying. also this isnt a concern atm but when i try and make my program output X's instead of -1 it outputs a 88, anyone know the reasoning for this, or how it might be fixed? (code thus far)

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n, point;
    int position_x, position_y;
        
    do{
          cout << "Inpute Board size between 10 and 50" << endl;
               cin >> n;
}while(n < 10 || n>50);

do{         
   cin >> point;

   position_y = ceil(point/n);
   position_x =(point%n)-1;
   

    int game_board[99][99];
    int value(1);
    
    for(int y=0; y<n; y++){
        for(int x=0; x<n; x++){
            game_board[x][y] = value++;
            game_board[position_x][position_y] = -1;
        }
    }
    for(int y=0; y<n; y++){
            cout << endl;
                 for(int x=0; x<n; x++){
                         if(game_board[x][y] <= 9){
                               cout << game_board[x][y]<< "  ";

}
else{
            cout<<game_board[x][y]<<" ";
                                      }
            }
}
cout << endl;

}while(true);

    system("PAUSE");
	return 0;
}
Member Avatar
tKc
Newbie Poster
23 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

okay so i managed to have it output a -1 when the user enters a number in the grid but the problem im having now is that the inputed values are not staying from one array to another. like when the user enters, lets say 74, it puts a -1 in that spot,it then outputs the grid again and requests another input (which it should) but this grid has the number 74 and not a -1, so the replacements are not staying. also this isnt a concern atm but when i try and make my program output X's instead of -1 it outputs a 88, anyone know the reasoning for this, or how it might be fixed? (code thus far and i know there are errors such as with line 18 and such but my main concern atm is keeping the values for the 2d array)

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    int n, point;
    int position_x, position_y;
        
    do{
    cout << "Inpute Board size between 10 and 50" << endl;
    cin >> n;
}while(n < 10 || n>50);

do{         
   cin >> point;

   position_y = ceil(point/n);
   position_x =(point%n)-1;

    int game_board[99][99];
    int value(1);
    
    for(int y=0; y<n; y++){
        for(int x=0; x<n; x++){
            game_board[x][y] = value++;
            game_board[position_x][position_y] = -1;
        }
    }
    for(int y=0; y<n; y++){
            cout << endl;
                 for(int x=0; x<n; x++){
                         if(game_board[x][y] <= 9){
                               cout << game_board[x][y]<< "  ";

}
else{
            cout<<game_board[x][y]<<" ";
                                      }
            }
}
cout << endl;

}while(true);

    system("PAUSE");
	return 0;
}

okay so lets say we have a 10x10 grid and the user enter 74

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 -1 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

now it askes for the users input again
input:33

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 -1 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

however the -1 on the 74 has been changed back to 74, i wish it to remain -1, like dis

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 -1 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 -1 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100

hope this clears things up a bit

Member Avatar
sfuo
Master Poster
713 posts since Jul 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 0 [?]
 
0
 

You can do 2 things for this.
#1 save all the points into a container and plot them each time
#2 make the board once outside of the infinite loop and plot the points on within the loop

The following code shows #2 because I think it is the best choice in this case

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

int main()
{
    int n, point;
    int position_x, position_y;

    do
    {
	cout << "Inpute Board size between 10 and 50" << endl;
	cin >> n;
    } while(n < 10 || n>50);

    int game_board[99][99];
    int value = 1;
    //create the board once
    for( int y = 0; y < n; y++ )
    {
        for( int x = 0; x < n; x++ )
        {
            game_board[x][y] = value++;
        }
    }

    do
    {
        cout << "Enter a point: ";
	cin >> point;
	position_y = point/n;
	position_x = (point%n)-1;

	game_board[position_x][position_y] = -1; //set the point on the board


	for( int y = 0; y < n; y++ )
	{
	    cout << endl;
	    for( int x = 0; x < n; x++ )
	    {
		if( game_board[x][y] <= 9 )
		{
	            cout << game_board[x][y] << " ";
		}
		else
		{
		    cout << game_board[x][y] << " ";
		}
	    }
	}
	cout << endl;
    } while(true);

    system("PAUSE");
    return 0;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 
do{         
   cin >> point;

   position_y = ceil(point/n);
   position_x =(point%n)-1;

    int game_board[99][99];
    int value(1);

// Why are you regenerating the matrix each and every time you input a
// value?  This erases any previous points you've changed.  Move the 
// following loop above this DO loop. Be sure to think what else needs 
// to move and what what needs to stay inside the DO loop.    
    for(int y=0; y<n; y++){
        for(int x=0; x<n; x++){
            game_board[x][y] = value++;
            game_board[position_x][position_y] = -1;
        }
    }
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

So how many threads do you want to keep track of? Do NOT create new threads for the same question, keep posting in the same thread!

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: