I have this two-dimensial array array[y][x] (where x is horizontal and y vertical):
3 2 0 0 0 0 0 0 0 0
1 4 3 0 0 0 0 0 0 0
2 4 0 0 0 0 0 0 0 0
2 4 0 0 0 0 0 0 0 0
1 3 0 0 0 0 0 0 0 0
4 2 5 1 0 0 0 0 0 0
1 3 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0
2 3 0 0 0 0 0 0 0 0
And I need to print it like this:
3 1 2 2 1 4 1 2 2
2 4 4 4 3 2 3 3 3
3 5
1
How would I do this using c++?
Note that there are no empty lines. If the whole column only has zero's in them, there shouldn't be an endl
You need to iterate over and print out each element. You can flip the elements around by swapping the indices used to get the value out of the array.
#include<iostream>
#include<iomanip>
int gridWidth = 10;
int gridHeight = 10;
int cellWidth = 2;
for (int i = 0; i < gridHeight; i++){
bool anyVals = false;
for (int j = 0; j < gridWidth; j++){
int val = array[i][j]; //Swap i and j to change the orientation of the output
if(val == 0){
std::cout << std::setw(cellWidth) << " ";
}
else{
anyVals = true;
std::cout << std::setw(cellWidth) << val;
}
}
if(anyVals)
std::cout << std::endl;
}
Remember that if you swap i and j then you will need to swap gridWidth and gridHeight.
Just to avoid confusion std::setw(cellWidth) thing is a convenient way to print fixed-width text (like text that must always be two characters long). It takes whatever you print out and adds spaces to it to make it the right length.
Take the transpose of your matrix , make 2 loops(outer and inner ) and print only if the no is greater than zero and print space for every zero. when you go back again to the outer loop ,print new line .
Something like this should help you.
for (int i = 0; i < y; i++)
for (int j = 0; j < x; j++)
if (array[i][j] != 0)
cout << array[i][j];
else
cout << " ";
cout << endl;
Related
I am trying to implement a binary image matching algorithm. And I need to generate the C matrix given below.
Given a small pattern image A, I need to match it to large image row-by-row and column-by-column, to find the location where that pattern matches the most.
Given M x M sized pattern A:
0 0 1
0 1 1
1 1 1
and N x N sized input image B:
0 0 0 0 0 1 0 0
0 0 0 0 1 1 0 0
0 0 0 1 1 1 0 0
0 0 0 0 0 0 0 0
1 1 1 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 1 1 1 1 0
0 0 0 0 0 0 0 0
the N x N sized output image C is the similarity of A with B at each row and column of B. Therefore C:
0 0 0 0 0 0 0 0
0 3 4 6 9 4 2 0
0 3 4 6 4 1 1 0
0 6 6 4 2 2 3 0
0 4 3 2 3 5 5 0
0 2 2 4 6 8 5 0
0 3 4 5 4 5 2 0
0 0 0 0 0 0 0 0
I am at stuck at the point where I need to compare the matrix A with B. I have made them as 2D arrays.
This is what I have done so far
for (int i = 0; i <3 ; i++)
{
for (int j = 0; j<3; j++)
{
for (int k = 0; k < 8; i++)
{
for (int s = 0; s < 8; j++)
{
if (A[i][j] == B[k][s])
{
count++;
}
}
}
}
whats your code or algorithm? You've 9 in the matrix, so
0 0 1
0 1 1
1 1 1
matches exactly. There's the coordinate pair you must be searching for.
typedef int** matrix;
struct position
{
int x;
int y;
position(int _x,int _y){ x = _x ; y= _y; }
}
// M <= N
//checks all MxM submatrices in NxN matrix B
//and compares them with NxN matrix A
position f(matrix A, int M , matrix B , int N)
{
int best_match_first_row = -1;
int best_match_first_column = -1;
int best_match_counted = -1;
for(int i=0; i <= N-M ; ++i)// iterate through the first elements of every
for(int j=0; j <= N-M ; ++j)// MxM submatrix
{
int match_count = 0;
for(int k=0 ; k < M ; ++k)//iterate through the submatrix
for(int l=0 ; l < M ; ++l)//and compare elements with matrix A
if( A[k][l] == B[i+k][j+l] ) ++match_count; //count if it matches
if(match_count > best_match_counted) //if we have a better match than before
{
best_match_counted = match_count; //store the new count as best.
best_match_first_row = i; //store the position of the
best_match_first_column = j; //first element in the submatrix
}
}
//returns the position of the first element of the most matching submatrix
return position( best_match_first_row , best_match_first_column )
}
Does int myarray[7][7] not create a box with 8x8 locations of 0-7 rows and columns in C++?
When I run:
int board[7][7] = {0};
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
cout << board[i][j] << " ";
}
cout << endl;
}
I get output:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 146858616 1 0 0 146858832 1 1978920048
So the 8 columns seem to work, but not the 8 rows.
If I change it to int board[8][7] = {0}; it works on mac CodeRunner IDE, but on linux Codeblocks I get:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 1503452472
Not sure what's going on here.
Two dimensional arrays are not different to the one dimensional ones in this regard: Just as
int a[7];
can be indexed from 0 to 6,
int a2[7][7];
can be indexed from 0 to 6 in both dimensions, index 7 is out of bounds. In particular: a2 has 7 columns and rows, not 8.
int board[7][7]; will only allocate 7x7, not 8x8. When it's allocated, you specify how many, but indexes start at 0 and run to the size - 1.
So based on your source, I would say you really want int board[8][8].
int board[7][7] = {0}; creates a 7x7 array. You are going out of bounds in your loop. Change it to int board[8][8] = {0};
int board[8][7] = {0};
When you do as above, you created only 8 rows and 7 columns.
So your loop condition should be as follows:
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 7; j++)
{
If you try as follows system will print garbage values from 8th columns
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Araay starts from zero means that it will have n-1 elements not n+1 elements
try
int a[8][8] = {}
i = 0
j = 0
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
a[i][j] = 0;
}
}
EDITED
What I am trying to do is (after reading from file and putting the information in 2d array defined in a struct,that part works) call a method that finds out if there is any zeroes in the array and if so change it and prints it again. I know I´m missing pointers but I don't know where. Thanks in advance.
struct matrix{
const static int N=9;
int Ar[N][N];
};
void iprint(matrix s){ //my method to print the array
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
cout << (s).Ar[i][j] << ' ';
}
cout << endl;
}
}
bool annotation(matrix s, int row, int column, int num){
if(s.Ar[row][column] == 0){
(s).Ar[row][column] = num;
return true;
}
else if(s.Ar[row][column] != 0){
cout << "NO" << endl;
return false;
} else {
cout << "No" << endl;
return false;
}
iprint(s);
}
The array in first place :
0 0 6 5 0 0 1 0 0
4 0 0 0 0 2 0 0 9
0 0 0 0 3 0 0 0 8
0 7 0 1 0 0 5 0 0
0 8 0 0 0 0 0 6 0
0 0 3 0 9 0 0 4 0
2 0 0 0 4 0 0 0 0
9 0 0 7 0 0 0 0 3
0 0 5 0 0 8 2 0 0
The output that I get after those methods (calling the method annotation(s,1,1,2); )
2686428 0 0 2686524 8989288 4733208 0 0 -17974607
1 0 4201360 4662484 0 8989288 8989340 9005760 0
.
.
.
Im reading the array from a file,and the method is
bool readMatrix(matrix s){
ifstream f;
f.open("nuMatrix.txt");
if (f.is_open()) {
while(!f.eof()){
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
f>>(s).Ar[i][j];
}
}
}
f.close();
iprint(s);
return true;
}
else {
cerr << "NO";
return false;
}
}`
The matrix you're passing to readMatrix and annotation isn't modified by the functions.
You're passing the matrix by value, so you're only modifying a copy of it.
Change your functions to take a reference to a matrix:
bool annotation(matrix& s, int row, int column, int num)
bool readMatrix(matrix& s)
I should open with the fact that I am very new to c++.
I am attempting to display a constantly updating 20x20 matrix of chars. Currently, I am displaying the the matrix using for loops as cout's (code below) but that is incredibly flickery- I'm looking for something smoother.
Is there a way to convert this char matrix into an image and display that?
This is my first question here, so I apologize if I did something wrong!
Code so far:
#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
using namespace std;
int main()
{
int randInt;
//Initialize matrix and location
int matrix[20][20];
int location[2] = {0,0};
for (int i=0; i<20; i++)
{
for (int j=0; j<20; j++)
{
matrix[i][j] = 1;
}
}
//move the X around
for (int i=0; i<100; i++)
{
cout << string(50, '\n');
//Change the X's location
randInt = rand() % 4;
switch (randInt)
{
case 0:
if(location[1] > 0)
location[1] = location[1]-1;
break;
case 1:
if(location[0] < 20)
location[0] = location[0]+1;
break;
case 2:
if(location[1] < 20)
location[1] = location[1]+1;
break;
case 3:
if(location[0] > 0)
location[0] = location[0]-1;
break;
default:
cout << "Switch statement problem";
}
//Display the matrix
for (int x=0; x<20; x++)
{
for (int y=0; y<20; y++)
{
if(x==location[0] && y==location[1])
cout << "X";
else
cout << matrix[x][y];
}
cout << endl;
}
Sleep(100);
}
system ("pause");
return 0;
}
You should rename the location[2] to something like `struct { int x,y; } location for readability.
Then you can build an array of characters in RAM and put out it at once.
int _tmain(int argc, _TCHAR* argv[])
{
char matrix[20][20];
char image[21][21];
struct { int x, y; } location;
int x = 0;
int y = 0;
location.x = 7;
location.y = 3;
// fill the matrix
for (x = 0; x < 20; ++x)
{
for (y = 0; y < 20; ++y)
{
matrix[y][x] = 'a' + x + y;
}
}
// prepare the image
y = 0;
while (y < 20)
{
memcpy(image[y], matrix[y], 20);
image[y][20] = '\n';
++y;
}
// add the cross
image[location.y][location.x] = 'X';
image[20][0] = '\0';
// use the image
puts((char*)image);
}
Please add you random functionality as needed.
If you want to convert the char to a image and see the color means, write the char value as pixel in simple pgm format.
Write a file in this sample format
P2
# feep.pgm
24 7
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0
0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0
0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
check this link http://netpbm.sourceforge.net/doc/pgm.html for pgm format
I have a 2 dimensional array filled with 0s and 1s. I have to display that array in way that:
- 0s are always shown
- 1s are shown one at the time.
It suppose to look like a maze where 0 is a wall and 1 is a current position.
How can I do that in c++?
EDIT:
I came up with a solution but maybe there is simpler one. What if I'd create copy of my _array and copy 0s and blank spaces instead of 1s to it. Then in loop I'd assign one of _array "1" to second array then display whole array and then make swap 1 back with blank space?
EDIT2:
int _tmain(int argc, _TCHAR* argv[])
{
file();
int k=0,l=0;
for(int i=0;i<num_rows;i++)
{
for(int j=0;j<num_chars;j++)
{
if(_array[i][j] == 1)
{
k=i;
l=j;
break;
}
}
}
while(1)
{
for(int i=0;i<num_rows;i++)
{
for(int j=0;j<num_chars;j++)
{
if(_array[i][j] == 0) printf("%d",_array[i][j]);
else if(_array[i][j]==1)
{
if(k==i && l==j)
{
printf("1");
}
else printf(" ");
}
l++;
if(l>num_chars) break;
}
k++;
l=0;
printf("\n");
}
k=0;
system("cls");
}
return 0;
}
I wrote something like that but still i don't know how to clear screen in right moment. Function file() reads from file to 2D array.
Assuming you want something like that
000000
0 0
0000 0
0 1 0
0 0000
000000
You could print a 0 whenever it occurs and a blank space if not. To handle the current position you could use two additional variables like posX, posY. Now everytime you find a 1 in your array you check if (j == posX && i = posY) and print 1 if so...
As you just need to visualize the maze at different possible positions I'd propose a simple display function. DisplayMaze(int x, int y) is printing the maze in the required format to the screen. If _array[y][x] == 1 there is also printed a single 1...
void DisplayMaze(int x, int y)
{
for (int row = 0; row < num_rows; row++)
{
for (int col = 0; col < num_chars; col++)
{
if (_array[row][col] == 0)
std::cout << "0 ";
else if (row == y && col == x)
std::cout << "1 ";
else
std::cout << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
In order to display all possible positions you have to iterate over all of them and check if the current position is marked with 1 in the array (otherwise displaying would't make sense)
for (int y = 0; y < num_rows; y++)
{
for (int x = 0; x < num_chars; x++)
{
if (_array[y][x] == 1)
{
DisplayMaze(x, y);
}
}
}
The output should look like:
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 1 0
0 0 0 0 0
0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0
0 0 0 0 1 0
0 0
0 0 0 0 0
0 0 0 0 0 0
...
However, i'd recommend a more C++ like approach as a maze could be implemented as a class. This class could bring it's own display-method and would encapsulate the internal data. It could basically look like:
class Maze
{
public:
// generate empty maze with given size
Maze(int width, int height);
// destructor
~Maze();
// print maze if the given position is marked with 1
void printPosition(int x, int y) const;
// takes a cstring as input to initialize the maze from
Maze& operator<<(const char* input);
// returns true if the given position is marked with 1
bool isValidPosition(int x, int y) const;
private:
// this is the actual representation of the maze
std::vector<std::vector<int> > grid_;
};
it would be used as followes:
Maze myMaze(num_chars, num_rows);
myMaze << "000000"
"011110"
"000010"
"011110"
"010000"
"000000";
for (int y = 0; y < num_rows; y++)
{
for (int x = 0; x < num_chars; x++)
{
if (myMaze.isValidPosition(x,y))
{
myMaze.printPosition(x,y);
}
}
}
hire you go*[solved]*
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
char map[x][y];
memset(map, 'a', sizeof(map));
int y_pos = 0;
for (int x_pos = 0; x_pos < x * y; x_pos++){
if (x_pos == x){
x_pos = 0;
y_pos = y_pos + 1;
cout<<endl;
}
if (y_pos == y){
system("pause");
return 0;
}
cout<<map[x_pos][y_pos];
}