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] = ' ';
}
}
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;
}
}
I'm playing around to get the grip of multi dimensional arrays.
I have managed to have the array record the input from the user..
I'm trying to use 2 FOR loops to print with the idea that it should print 4 rows of 3 characters each
i know i can solve this if i manually type what to print, but for sure there is a way to have a loop do that for me...
here is the input and output code that i wrote:
cout << "Enter characters" << endl;
for (int i = 0; i < 4; i++)
{
for (int x = 0; x < 3; x++)
{
cin >> charArr[x][i];
}
}
cout << "Printing the array now" << endl;
for (int i = 0; i < 4; i++)
{
for (int x = 0; x < 3; x++)
{
cout << charArr[x][i];
}
cout << endl;
}
i don't understand why some letters are gone and why it doesn't print in order...
where i is row and j is a column. for loop should come like this only.
in this order only we can store the input.
In your case, you are swapping the orders.
Solution :
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> charArr[i][j];
}
}
cout << "Printing the array now" << endl;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
cout << charArr[i][j];
}
cout << endl;
}
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;
}
I am trying to search a 2D vector for a char, namely a '?' and replace it with 'x'.
I have no issues doing this task with a single vector but keep having issues with the a 2D vector implementation, see below for code.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// An empty vector of vectors
vector<vector<char> > v2d;
// Create a vector with 5 elements
vector<char> v2(5, '?');
// Create a vector of 3 elements.
vector<vector<char> > v2d2(3, v2);
// Print out the elements
cout << "Before Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
cout << "" << endl;
/* Does not work as expected
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i] == '?');
(v2d[i] = 'x');
}
}
*/
cout << "" << endl;
cout << "After Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
system("pause > NULL");
return 0;
}
I receive the error message below when I try and compile the code.
IntelliSense: no operator "==" matches these operands operand types are: std::vector>, std::allocator>>> == char Project3\Source.cpp 77 16 Project3
I believe it is an issue with the container in updating the proper row and column. Any help would be much appreciated.
Thank you
There were a few bugs, please compare with your original code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// An empty vector of vectors
vector<vector<char> > v2d;
// Create a vector with 5 elements
vector<char> v2(5, '?');
// Create a vector of 3 elements.
vector<vector<char> > v2d2(3, v2);
// Print out the elements
cout << "Before Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
cout << v2d2[i][j] << " ";
cout << endl;
}
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
if (v2d2[i][j] == '?')
v2d2[i][j] = 'x';
}
cout << "After Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
std::cout << v2d2[i][j] << " ";
cout << endl;
}
return 0;
}
Take a look at your commented code:
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i] == '?'); // <------
(v2d[i] = 'x');
}
}
There is a semi-colon after the if statement, meaning that nothing will be executed if the statement is true. Rookie mistake.
Many consider it a bad practice not to always use parentheses with if-statements and other similar situations that require them, because of issues like this one
You should have :
if (v2d[i][j] == '?'){
v2d[i][j] = 'x';
}
cout << "Vector Update" << endl;
for (int i = 0; i < v2d2.size(); i++) {
for (int j = 0; j < v2d2[i].size(); j++)
{
if (v2d[i][j] == '?')
v2d[i][j] = 'x';
}
}
You were not accessing the 2d vector correctly, should have used v2d[i][j] instead.
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.