Hi guys I have a 2D array of char '-' in it and I want to print it out.
However I want to print it in a grid format so that the point (0,0) is on the bottom left of the array not the top left.
For example now it's printing:
0 1 2 3
1
2
3
and I want it to print in the way:
3
2
1
0 1 2 3
How can I print my array in order to act more like a graph than an array?
Thanks.
EDIT: This is what my printing function looks like
displayGrid(){
for(int column = 0; column<gridSize; column++){
for(int row = 0; row < gridSize; row++){
std::cout<<gridArray[row][column]<< " ";
}
std::cout<<std::endl;
}
}
This is how I fill the elements in my array:
createGrid(){
int gridSize = 50;
for(int column = 0; column<gridSize; column++){
for(int row=0; row<gridSize; row++){
gridArray[row][column] = empty;
}
}
//gridArray[0][1] = full; first value is x, second is y. Works.
}
Just let your first loop start at gridSize and decrement it.
Should look similar to this:
displayGrid(){
for(int column = gridSize; column>=0; column--){
for(int row = 0; row < gridSize; row++){
std::cout<<gridArray[row][column]<< " ";
}
std::cout<<std::endl;
}
}
Related
I have a pascal's triangle with max rows of 5 . Lets suppose I want to find the integration of the fourth row . How do I access the fourth row in the pascal's triangle. More precisely I want to know how to access a row in the pascal's triangle by entering the number n of the row
Code
#include <iostream>
using namespace std;
int main(){
int a1, a2, a3, a4, a5, pascal, columns;
const int rows = 5;
int **array = new int *[rows]; //generating array
for(int i = 0; i <= rows; i++)
array[i] = new int [columns];
for (int i = 0; i <= rows; i++){ //loop for te pascal's triangle
for (int j = 0; j <= i; j++){
if(j == 0 || i == 0){
pascal = 1; //first element of pascal's triangle
}
else{
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to integrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> a1;
cin >> a2;
cin >> a3;
cin >> a4;
cin >> a5;
}
1
1 1
1 2 1
1 3 3 1 ------> like of n = 4 i want to integrate the elements of this row
1 4 6 4 1
And the answer should be for 1,3,3,1 = 0, 1, 1.5, 1, 0.25
you should first fill the array with the elements then you can access them like so (EDIT: Make sure to initialize the columns variable, I set it to 5)
#include <iostream>
using namespace std;
int main() {
int row_nb, pascal, columns = 5; //Initialized columns with columns = 5
const int rows = 5;
int **array = new int *[rows]; //generating array
for (int i = 0; i <= rows; i++)
array[i] = new int[columns];
for (int i = 0; i <= rows; i++) { //loop for te pascal's triangle
for (int j = 0; j <= i; j++) {
if (j == 0 || i == 0) {
pascal = 1; //first element of pascal's triangle
}
else {
pascal = pascal *(i - j + 1) / j; //pascal's triangle formula
}
array[i][j] = pascal; //fill the array
cout << " " << pascal; // printing pascals triangle
}
cout << "\n";
}
cout << "enter which row to intergrate: ";
// here I want to directly access a row rather that entering the elements of the row
cin >> row_nb; //input the row you want to access
for (int i = 0; i <= row_nb; i++) { //access the elements in this row in the array
cout << array[row_nb][i] << " ";
}
return 0; // add the return statement since the return type of the main function is int
}
As I said when you last asked this :
You could simply store each row into a std::vector<int> as you calculate it (in addition to, or instead of, printing it), then keep a list of rows in a std::vector<std::vector<int>>. Then, to access the nth row after calculating the triangle, you just grab the nth element of the second vector.
I am getting issues. I want to create 2D array rows=3 cols=2
My code is as follows
int **ptr;
int row=3;
int col=2;
ptr=new int *[col];
for (int i=0;i<col;i++)
{
ptr[i]=new int [row];
}
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
cout<<ptr[i][j]<<" ";
}
cout<<endl;
}
But i am getting output (2*2)
0 0
0 0
When you work with 2D arrays you have the following:
HOLDS YOUR ROWS
|
|
[x0] ([x0_1][x0_2][x0_3]...[x0_n]) <- ROW ARRAY
[x1] ([x1_1][x1_2][x1_3]...[x1_n]) <- ROW ARRAY
[x2] ([x2_1][x2_2][x2_3]...[x2_n]) <- ROW ARRAY
. .
. .
. .
[xm] ([xm_1][xm_2][xm_3]...[xm_n]) <- ROW ARRAY
Which means that first you have to create each row:
for (int i=0;i<row;i++)
{
ptr[i]=new int[col]; // Each row has col number of cells
}
From the table at the beginning of my post this gets you every ([xP_1][xP_2][xP_3]...[xP_n]).
Next part of your code has to actually initialize the cells in each row so in your outer loop you have to iterate over your rows and then in the inner loop you have to iterate over you columns because each row has COL cells from ptr[i]=new int[COL];. So we get:
for (int i=0;i<row;i++)
{
for (int j=0;j<col;j++)
{
cout<<ptr[i][j]<<" ";
}
cout<<endl;
}
So at the end we have (I've replaced row with rows and col with cols in order to make things more readable for you...I hope :D):
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
int **ptr;
int rows = 3;
int cols = 2;
ptr = new int *[rows];
for (int row=0; row<rows; row++)
{
ptr[row]=new int [cols];
}
for(int row=0; row<rows; row++)
{
for(int col=0; col<cols; col++)
{
ptr[row][col] = 0;
cout << ptr[row][col] << " ";
}
cout << endl;
}
// Do something with the 2D array
// ...
// Delete all
for(int row = 0; row < rows; row++) delete[] ptr[row];
delete[] ptr;
return 0;
}
The output is:
0 0
0 0
0 0
Hope this helps. Also have in mind that you need to initialize your array's cells with some values. It's just not considered a good practice to leave it like you do - create and then simply go right to the displaying part without adding any values.
I am currently working on a dungeon crawl game where the user can move throughout a maze on the screen. I decided to use a 2d array for the maze. One problem, I have a function to print the maze although its not working. I want it to print all four of the rows (there are supposed to be 4 0's per row) but it only prints 4 0's in a single line.
int maze[4][4] = {(0,0,0,0),
(0,0,0,0),
(0,0,0,0),
(0,0,0,0)};
for (int i = 0; i < 4; i++)
{
cout <<maze[i][i];
}
You need two loops, one nested inside the other.
One to print the rows.
One to print each column in the current row.
You need nested loop for displaying 2D array.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout<
Try this.
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
cout <<maze[i][j];
}
cout << "\n";
}
I need a function that can take to according to the x-axis of symmetry of the matrix.
input (matrix[i][j]):
1 2 3
4 5 6
7 8 9
output (matrix[i][j]):
7 8 9
4 5 6
1 2 3
How can i do this on the same matrix?
How should I write the inverse function?
void inverse(.......)
{
..
..
..
}
int main(){
int **matrix, i, j, k, row, column;
cout << "Row and column:" ;
cin >> row >> column;
matrix = new int*[row];
for (i=0; i<row; ++i){
matrix[i] = new int[column];
}
cout << "input elements of matrix: " << endl;
for(i=0; i<row; i++){
for (j=0; j<column; j++){
cin >> *(*(matrix+i)+j);
}
}
inverse (........);
for(i=0; i<row; i++){
for (j=0; j<column; j++){
cout << *(*(matrix+i)+j);
}
cout << endl;
}
return 0;
}
In this particular case, you can modify the loop to print the matrix backwards:
for(i = 0; i < row; i++) {
->
for(i = row - 1; i >= 0; i--) {
But if you want to actually do something to the data, my suggestion would be to refactor this code to use std::vector.
std::vector<std::vector<int> > matrix(numberOfRows, std::vector<int>(numberOfColumns));
// You can use std::vector<std::valarray<int> > matrix; alternatively.
You would then flip it simply by using an STL function:
std::reverse(matrix.begin(), matrix.end());
Edit:
Well, if you don't want to use std::vector for the time being and for this particular case, what you would do is this:
void flipMatrix(int** matrix, int rows, int columns) {
int middle = rows/2; // I expect it to truncate for an odd number of rows.
// Temporary row for swapping
int* tempRow;
for (int i = 0; i < middle; i++) {
// swap rows
tempRow = matrix[i];
matrix[i] = matrix[rows - i - 1];
matrix[rows - i - 1] = tempRow;
}
}
I am stuck on the last few problems of an array exercise. Could anyone lend a hand?
Write C++ statements that do the following:
store 5 in the first column of an array and make sure that the value in each subsequent column is twice the value in the previous column.
print the array one row per line.
print the array one column per line.
I think this will work for question #2:
for (row = 0; row < 10; row++)
{
for (col = 0; col < 20; col++)
cout << alpha[row][col] << " ";
cout << endl;
}
but question 1 and 3 have me stumped. thanks
Here's what i came up with after your tips. thanks everyone
3.
for (col = 0; col < 20; ++col)
{
for (row = 0; row < 20; ++row)
cout << alpha[row][col] << " ";
cout << endl;
}
1.
for (row = 0; row < 10; row++)
alpha[row][0] = 5;
for (col = 1; col < 20; col++)
for (row = 0; row < 10; row++)
alpha[row][col]=alpha[row][col-1]*2;
For #1, run a loop that starts at zero and goes until the number of rows. In each iteration just assign 5 to array[row][0]=5 (since coloumn 0 is the first coloumn).
Now run a loop from 1 to the number of coloumns. Inside, run another loop for each row. just assign array[row][col]=array[row][col-1]*2.
For #3, simply reverse the order of the loops. We iterate over all coloumns, and for each coloumns we have to iterate over all rows and print a newline after that.
I would post code, but it is better for you to try to understand and write the code yourself.
for each row, insert 5 into the first column (index 0), then in a loop, iterate from 1 through the number required, and the value at the current column index = 2 * value at previous column index (i.e. col - 1).
re-arrange the row, col loops.
Well for 1, just take the previous col and multiple by 5. So when your going through a loop, it'd be like col[position your at now] = col[prev pos]*2
For question #3, just reverse the order of loops, as
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
cout << alpha[row][col] << " ";
cout << endl;
}
Isn't it simple?
For question #1, just use the same reverse order of loops, and do this
int value = 5;
for (col = 0; col < 20; col++)
{
for (row = 0; row < 10; row++)
alpha[row][col] = value;
value = 2 * value;
}