Here's my code of the displayBoard function using pointers:
void tictactoe::displayBoard(){
board = new char* [SIZE];
for(int r = 0; r < SIZE; r ++){
board[r] = new char[SIZE];
}
cout << endl << endl;
for (int i = 0; i < COL_WIDTH - 1; i++)
cout << SPACE;
for (int i = 0; i < SIZE; i++)
cout << setw(COL_WIDTH) << i;
cout << endl;
for (int r = 0; r < SIZE; r++){
cout << setw(COL_WIDTH) << r;
for (int c = 0; c < SIZE; c++)
//cout << SPACE << board[r][c] << SPACE << VERTICAL;
cout << SPACE << board[r][c] << VERTICAL;
cout << endl;
for (int i = 0; i < SIZE; i++)
cout << SPACE;
for (int d = 0; d < SIZE * COL_WIDTH; d++)
cout << DASH;
cout << endl;
}
}
Somehow it doesn't display correctly. but when I run it without using pointers, it display correctly.
And also when I run it, I enters the row, and col to place my piece, no matter what I enter, it couldn't accept it. Io I am wondering the problem is coming from my displayBoard function.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have written next code but 3 functions must be replaced by 1 and I don't know how to.
The program creates 3 arrays but only 1 function must calculate negative numbers of each column and find the max element in each column. Here's the code:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int n = 0;
const int m = 3, k = 3, b = 4, u = 5;
int i, j;
void calc(float** array, int i, int j );
void calc1(float** array, int i, int j);
void calc2(float** array, int i, int j);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, i, j); // FUNCTION !!!
float** arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, i, j); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc2(ar, i, j); // FUNCTION !!!
}
void calc(float** array, int i, int j) {
int max = array[0][0];
for (int j = 0; j < k; j++)
{
max = array[0][0];
for (int i = 0; i < k; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc1(float** arr, int i, int j) {
int max = arr[0][0];
for (int j = 0; j < b; j++)
{
max = arr[0][0];
for (int i = 0; i < b; i++) {
if (arr[i][j] > max)
max = arr[i][j];
if (arr[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
void calc2(float** ar, int i, int j) {
int max = ar[0][0];
for (int j = 0; j < u; j++)
{
max = ar[0][0];
for (int i = 0; i < u; i++) {
if (ar[i][j] > max)
max = ar[i][j];
if (ar[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
The parameters to calc() should be the number of rows and columns in the array. Then it should use these as the limits in the for loops.
Also, since you're calculating total negative and maximum for each column, you must reset these variables each time through the column loop.
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
const int m = 3, k = 3, b = 4, u = 5;
void calc(float** array, int rows, int cols);
int main()
{
float** array = new float* [m];
for (int l = 0; l < m; l++) {
array[l] = new float[k];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < k; j++) {
array[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < k; j++) {
cout << setprecision(2) << setw(4) << array[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(array, m, k); // FUNCTION !!!
float *arr = new float* [b];
for (int l = 0; l < b; l++) {
arr[l] = new float[b];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < b; i++) {
for (int j = 0; j < b; j++) {
arr[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < b; i++)
{
for (int j = 0; j < b; j++) {
cout << setprecision(2) << setw(4) << arr[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(arr, b, b); // FUNCTION !!!
float** ar = new float* [u];
for (int l = 0; l < u; l++) {
ar[l] = new float[u];
}
// заполнение массива
srand(time(0));
for (int i = 0; i < u; i++) {
for (int j = 0; j < u; j++) {
ar[i][j] = rand() % 21 - 10;
}
}
cout << "The initial array is: " << endl << endl;
for (int i = 0; i < u; i++)
{
for (int j = 0; j < u; j++) {
cout << setprecision(2) << setw(4) << ar[i][j] << " ";
}
cout << endl;
}
cout << endl << "The amount of negative elements in each column: ";
calc(ar, u, u); // FUNCTION !!!
}
void calc(float** array, int rows, int cols) {
for (int j = 0; j < cols; j++)
{
int n = 0;
int max = array[0][j];
for (int i = 1; i < rows; i++) {
if (array[i][j] > max)
max = array[i][j];
if (array[i][j] < 0) {
n += 1;
}
}
cout << endl << "IN the [" << j + 1 << "] column is " << n << " negative elements" << endl << endl; n = 0;
cout << "IN the [" << j + 1 << "] column is " << max << " maximal element" << endl;
}
}
When I run the program, I get lines in between the elements of my 2d array.
How do I get rid of them? I meant to have empty values
cout << " ";
for (int i = 0; i < 10; i++)
cout << i << " ";
cout << endl;
for (int i = 0; i < 10; i++)
{
cout << i << " ";
for (int j = 0; j < 10; j++)
{
cout << grid[i][j] << " ";
}
cout << endl;
}
Doing char grid[MAX_ROWS][MAX_COLS]; is not an initialization, it just creates the grid with garbage inside it. If you want to have empty spaces you have to do something like:
for (size_t i = 0; i < MAX_ROWS; i++) {
for(size_t j = 0; j < MAX_COLS; j++) {
grid[i][j] = ' ';
}
}
I am having a problem with incrementing a single item in an array. It ends up incrementing another array.. How does it do this? this is what I have:
string simulateFIFO(darray<int> requests, int frameSize) {
string results;
int currentPages[frameSize];
int timer[frameSize];
int hits = 0;
int faults = 0;
cout << "STARTING FIFO" << endl;
for (int i = 0; i < requests.size(); i++) {
cout << requests[i] << " ";
}
cout << endl;
cout << endl;
for (int i = 0; i < frameSize; i++) {
currentPages[i] = -1;
timer[i] = 0;
}
// Start Calculations
for (int i = 0; i < requests.size(); i++) {
bool requestSet = false;
for (int j = 0; j < frameSize; j++) {
if (currentPages[j] < 0) {
// Current frame does not have a page
cout << "FRAME IS EMPTY" << endl;
currentPages[j] = requests[i];
requestSet = true;
faults++;
j = frameSize;
}
else if (currentPages[j] == requests[i]) {
cout << "FRAME IS SAME AS REQUEST" << endl;
// Current Frame is set the the page being requested
timer[j] = 0;
requestSet = true;
hits++;
j = frameSize;
}
cout << currentPages[j] << endl;
timer[j]++;
cout << currentPages[j] << endl;
}
// Check to see if a request was set or not
if (requestSet == false) {
cout << "FRAME NEEDS REPLACED" << endl;
// The request wasnt set. Determine which frame to replace with the new request
int lastUsed = 0;
int lastUsedIndex = 0;
for (int j = 0; j < frameSize; j++) {
if (timer[j] > lastUsed) {
lastUsed = timer[j];
lastUsedIndex = j;
}
}
currentPages[lastUsedIndex] = requests[i];
//timer[lastUsedIndex] = 0;
faults++;
}
cout << "HEY 3: " << currentPages[0] << endl;
cout << "NEW FRAME: ";
for (int j = 0; j < frameSize; j++) {
cout << currentPages[j] << " ";
}
cout << endl;
cout << endl;
}
cout << "FIFO" << endl;
cout << faults << endl;
cout << hits << endl;
cout << endl;
return results;
}
My Output ends up being
0
1
Why does increasing one array actually increase the other as well?
Your code includes a possible path of execution:
j = frameSize;
followed by
timer[j]++;
This accesses out of bounds: for an array of dimension frameSize, the valid indices are 0 through frameSize-1.
I guess you actually want to exit the loop; if so then replace j = frameSize; with break; .
NB. int timer[frameSize]; is not permitted in ISO C++; array bounds must be known at compile-time. Your code is relying on a compiler extension.
I'm printing random numbers enclosed in ascii boxes in a 6x6 grid. Having issues with printing out the grid.
Instead of having 6 columns, all my boxes and numbers are being printed out in 1 column. Been troubleshooting but cant seem to find the issue. Below is the code. Appreciate your assistance.
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy [6][6];
srand((unsigned)time(0));
int lowest=1111, highest=9999;
int range=(highest-lowest)+1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
cout << char(218);
for (int y=0; y< 4; y++)
{
cout << char(196);
}
cout << char(191) <<endl;
cout << char(179) << arrayxy[i][j] << char(179) << endl;
cout << char(192);
for (int z=0; z< 4; z++)
{
cout << char(196);
}
cout << char(217) <<endl;
}
cout << endl;
}
cout << endl;
}
You should endl after all the columns get printed out, NOT before.
This is how I fixed your code that gives, hopefully, what you wanted to have.
For the record, I specifically changed those ASCII to * and & to make the console result more readable. You can change them back to what you want those characters to be again.
void WriteFrontLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
void WriteEndLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy[6][6];
srand((unsigned)time(0));
int lowest = 1111, highest = 9999;
int range = (highest - lowest) + 1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest + int(range*rand() / (RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
WriteFrontLine(6);
for (int j = 0; j < 6; ++j)
{
cout << '*' << arrayxy[i][j] << '*';
}
cout << endl;
WriteEndLine(6);
cout << endl;
}
cout << endl;
}
The figures in the walkthrough portion of this article detail the algorithm fairly well.
I've developed the following short program:
#include <iostream>
using namespace std;
int main() {
int arr['K' - 'A'][10 - 0] = {0};
int largestShip;
//grid
cout << " ";
for(int i = 0; i < 10; i++) {
cout << (i + 1) % 10;
}
cout << endl << " ____________" << endl;;
for(char c = 'A'; c < 'K'; c++) {
cout << c << " |";
for(int i = 0; i < 10; i++) {
cout << arr[c - 'A'][i];
}
cout << "|" << endl;
}
cout << endl;
//endgrid
//heatmap
largestShip = 5;
for(char c = 'A'; c < 'K'; c++) {
for(int i = 0; (i + largestShip - 1) < 10; i++) {
for(int j = 0; j < largestShip; j++) {
arr[c - 'A'][j + i]++;
}
}
}
for(char c = 'A'; (c + largestShip - 1) < 'K'; c++) {
for(int i = 0; i < 10; i++) {
for(int j = 0; j < largestShip; j++) {
arr[c - 'A' + j][i]++;
}
}
}
//endheatmap
//showheatmap
cout << " ";
for(int i = 0; i < 10; i++) {
cout << (i + 1) % 10;
}
cout << endl << " ____________" << endl;;
for(char c = 'A'; c < 'K'; c++) {
cout << c << " |";
for(int i = 0; i < 10; i++) {
cout << arr[c - 'A'][i] % 10;
}
cout << "|" << endl;
}
cout << endl;
//endshowheatmap
return 0;
}
It sets the initial probability of the squares whose precedence is set by the size of its corresponding integer values in the array of values.
However, I'm not very well versed in probability theory, and am uncertain how to approach and understand the necessary methods for manipulating the probability of the squares (and by association, their values) in order to accurately assign the probability at any given square after some shot is taken at that square.
An explanation of the effects of a shot on the probability values of the squares within the grid would be helpful.