I need an Algorithm that I will use to scan pixels out from the center. Problem is with different lengths and sizes, it sometimes can't get to the position (See Image below blue part).
To illustrate the problem more I will show the example output:
If you compare the pictures you will notice that it goes in a spiral and the outputs match with a regular for loop and obviously the problem that it doesn't print the blue part correctly
Here is the code:
#include<iostream>
#include<string>
#include<math.h>
int arr[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
int arrSize = sizeof(arr) / sizeof(arr[0]);
int width = 5;
int height = 3;
void normal2DArray() {
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
index++;
}
}
}
int convertToInex(int x, int y) {
int left = x * y; // elements to the left
int right = (width - x) * y; // elements to the right
return left + right + x;
}
void spiralArray() {
// calculate middle point, which is also the start point
int x = round((float)width / 2) - 1;
int y = round((float)height / 2) - 1;
int direction = 0; // 0=right, 1=up, 2=left, 3=down
int turnCounter = 1;
int numSteps = 1;
int step = 1;
int index;
while (true) {
index = convertToInex(x, y); // defines the index position in arr
std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
switch (direction) {
case 0: x++; break;
case 1: y--; break;
case 2: x--; break;
case 3: y++; break;
}
index = convertToInex(x, y);
if (step % numSteps == 0) {
direction = (direction + 1) % 4;
turnCounter++;
if (turnCounter % 2 == 0) numSteps++;
}
step++;
if (step > arrSize) break;
}
}
void main() {
std::cout << "Output of Normal 2D Array:\n";
normal2DArray();
std::cout << "\n"; // better spacing
std::cout << "Output of Spiral Array:\n";
spiralArray();
}
I tried to keep the code as simple and small as possible. It should be ready to import and use.
And yes I already searched for my answer online but I didn't find anything that covered up the problem here nor had a similar setup like I have(1D arr and combined 2D array WIDTH/HEIGHT) and for sure not in c++.
❗ Also I need a Solution that works with all widths and heights and arr sizes and also works for any side ❗
I hope you can provide me with helpful answers and would be grateful with good and fast algorithm implementations/optimizations
EDIT:
Thanks to the replies in this Thread. I decided to go with the solution from #ldog for now even though I'm not completely satisfied with it.
Here are the edited code parts:
int failcounter = 0;
while (true) {
index = convertToInex(x, y); // defines the index position in arr
if (index < 0 || index > arrSize) failcounter++;
else std::cout << std::to_string(x) << "," << std::to_string(y) << " = " << std::to_string(arr[index]) << "\n";
// unchanged code inbetween
if (step > arrSize + failcounter) break;
Based on your comment:
#Beta they don't need to connect. It just has to detect that it's outside the array size (in that case -1) and don't scan them and find the next continue point. So it would continue like this: 5, 1, 6, 11
it seems you don't care that the spiral goes "out-of-bounds". In this case, the trivial answer is, embed the shapes that have no spiral in one that is always guaranteed to have one.
Thus if your input rectangle is N x M, then embed it in a rectangle of size max(M,N) x max(M,N), solve the problem in the latter, and when printing just ignore non-existent numbers in the original problem. Your printed sequence then will always be unique up to how the embedding occurs. The most reasonable embedding would try to center the smaller rectangle as much as possible in the larger rectangle, but this is up to you.
In this case you don't need an algorithm as you can compute everything analytically if you care to do the book-keeping and figure out the formulas involved.
You can hit a dead end (meaning exit the grid) in four spots. In each case, jump to the next live pixel you would have reached, if any live cells remain.
You can do this fairly easily by keeping track of the four corners you've visited furthest from the starting pixel. Using compass coords and N for up, these are the NE, NW, SW, and SE extremes visited.
If you hit a dead end going N from the NE pixel, jump to the pixel one to the left of the NW pixel and set the movement direction to down. If that is also a dead end, jump to one below the SW pixel and set the movement direction to right. Etc... When all four corners and dead ends then you're done.
Related
So for my university homework we are supposed to make a simple game of a 2D map with entities etc.
So I've figured a way of printing a map through it's dimensions and text formatting yet in our lessons it wasn't mentioned how we print on specific parts of the terminal. I've checked same questions but can't seem to get a solution.
Here is the code I use to print the map and make it's array. BLUE_B,STANDARD_B,OUTLINE and GREEN_B are declared above for the sake of color enrichment. Also IF POSSIBLE I don't want to use OS specific commands unless it's completely necessary. I use VS Code for Windows, compile with g++ on WSL Ubuntu-20.04.
for (int row = 0; row < i; row++) {
cout << OUTLINE "##";
for (int column = 0; column < j; column++) {
int n = rand() % 10; // According to "rand()"'s value we print either green, blue, or trees
if (n >= 3) { // We've assigned more values to green, in order to be more possible to be printed
cout << GREEN_B " "
STANDARD_B;
map[row][column] = 1;
} else if (n == 0 || n == 1) {
cout << BLUE_B " "
STANDARD_B;
map[row][column] = 0;
} else if (n == 2) {
int tree = rand() % 2;
cout << TREES "<>"
STANDARD_B;
map[row][column] = 0;
}
}
cout << OUTLINE "##"
STANDARD_B << endl;
}
for (i = 0; i < j + 2; i++) { // Bottom map border printing
cout << OUTLINE "##"
STANDARD_B;
}
If I understand the question correctly, you might be looking for iomanip. It is just one way of doing it. You can use setw and setfill to position different text in different areas. You can set different options for different outputs.
To move the text cursor to a specific line and column you need a “gotoxy”-style function.
Here is something that will work on both Linux terminals and the Windows Terminal. (It will not work on Windows Console without additional initialization help.)
#include <iostream>
const char * CSI = "\033[";
void gotoxy( int x, int y )
{
std::cout << CSI << (y+1) << ";" << (x+1) << "H";
}
Coordinates are (0,0) for the UL corner of the terminal. Here is a working example of use:
// continuing from above
#include <string>
int main()
{
// Clear a 40 x 10 box
for (int y = 0; y < 10; y++)
{
gotoxy( 0, y );
std::cout << std::string( 40, ' ' );
}
// Draw our centered text
gotoxy( 14, 5 );
std::cout << "Hello there!";
// Go to bottom of box and terminate
gotoxy( 0, 10 );
std::cout.flush();
}
For your game
I suggest you move the cursor to HOME (0,0) and draw the changed parts of your gameboard each frame.
I suppose that if you are on a local computer and your gameboard is relatively simple, you could probably get away with a complete redraw each frame...
Are you sure there is no professor-supplied macro or command to move the cursor home? (...as he has supplied magic macros to change the output color)?
I am implementing a simple greedy merging algorithm that merges the two points which are closest to each other and averages their position. After merging two points at indices i and j, I need to replace one of them, say i, by the mean of the two points. Then, copy the last point in the array over the other point, say j, after which I can reduce the array size by 1 with all remaining points being within the new reduced range.
I need to repeat the above step until there are only 3 representative points left, each of which represents a group of merged points. I have written the following code, but I guess it is not able to update the array (pts). I would appreciate if anyone could help me figure out the mistake. Thanks in advance. This is my code:-
void merge_point(Point pts[], int &size) {
double a;
int x, y;
Point d;
while(size != 3) {
double min = get_distance(pts[0],pts[1]);
for (int i = 0; i < size; i++) {
for (int j = i+1; j < size; j++) {
get_distance(pts[i], pts[j]);
if ((a = get_distance(pts[i],pts[j])) <= min) {
x = i;
y = j;
}
a = get_distance(pts[i],pts[j]);
}
}
d = mean_point(pts[x],pts[y]);
pts[x] = d;
pts[y] = pts[size-1];
size = size - 1;
}
}
When I am entering the input array as :-
3 8 2
5.7 7.2 2.2
10.83 6.48 2.42
20.577 5.832 2.662
39.0963 5.2488 2.9282
74.283 4.72392 3.22102
141.138 4.25153 3.54312
268.162 3.82638 3.89743
509.507 3.44374 4.28718
968.063 3.09936 4.7159
My expected output should be:-
181.974 4.29686 3.57395
968.063 3.09936 4.7159
509.507 3.44374 4.28718
But, I am getting an output of:-
4.35 7.6 2.1
968.063 3.09936 4.7159
36.6506 5.8958 2.68145
Think I find out the problem, you don't update the min distance as soon as you find a new one during the cicle, try this:
cout << "distance between p[" << i << "] and " << "p[" << j << "]" << "is " << get_distance(pts[i], pts[j]) << '\n';
if ((a = get_distance(pts[i], pts[j])) <= min)
{
cout << "current min distance is between point[" << i << "]" << " and point[" << j << "]" << '\n';
min = a;
x = i;
y = j;
}
you've got to add this instruction:
min = a;
in order to update the min.
Otherwise it will works only for some edge cases.
I saw that there was another question almost just like this but the answer didn't do what I want it to.
This is for an assignment. I have a 4x4 grid and a users inputted starting and ending (x,y) coordinates. I need to send this info from main to a create_path function which calculates the shortest path then sends it to another function that prints the grid step by step of the markers location until it makes it to the wanted coordinate. I can't use arrays and I have to have main, create_path, and print_path. The marker can only go up, down, left, and right.
So I really have no clue what to do. I thought about creating a variable for each cell in the grid but didn't know where to go from there. If somebody knows a quick solution that only uses main and one other function that is okay because I am running out of time.
You don't need to see main because it just shows the user the grid and asks them for input then sends the input to this function:
void create_path(int xStart, int xEnd, int yStart, int yEnd)
{
}
As you have already pointed out yourself in the comments, the shortest path from (0,2) to (3,1) is "right 3 down 1" in other words: right 3-0=3 and down 2-1=1
And that's already pretty much the answer...
In general, how do find the shortest path from (xStart, yStart) to (xEnd, yEnd)? You just do the same thing as before again. It is "right xEnd-xStart, down yEnd-yStart".
So everything that the print_path function requires is just "where do I start" and "how much do I go right/left and how much do I go up/down?"
So you could use two variable in create_path
int right = xEnd-xStart;
int down = yEnd-yStart;
and you send these to print_path. You have not provided the signature of print_path, but it could look like this:
void print_path(int xStart, int yStart, int right, int down)
{
}
Within this function you just do two loops:
int i = xStart;
int xend = xStart + right; // observe: if right is negative, it's just subtraction
bool right = (right >= 0); // are we going right or left?
while(i != xend) {
std::cout << "next waypoint: x = " << i << ", y = " << yStart << std::endl;
if (right) {
i++;
} else {
i--;
}
}
And now you do the same thing for the y coordinate
int j = yStart;
int yend = yStart + down;
bool down = (down >= 0); // are we going down or up?
while(j != yend) {
std::cout << "next waypoint: x = " << xend << ", y = " << j << std::endl;
if (down) {
j++;
} else {
j--;
}
}
create_path has to be a recursive function.
conditions given should be :
First check if the points are in the boundary or not
Then check if the point is the desired point, if yes then return
Otherwise recurse for up, down, left, right and diagonal points.
I've got this project I'm working on and it is an rpg game running in the cmd. The player navigates his character through the keyboard and fights enemies to level up and so on. I'm using a 2D array as a grid/map and in the demo version everything is working OK.
Problem: Now, in the more advanced version, I have a class which is used to load game/start new game. The function, that starts a new game, basicly creates .txt save files in which the information is stored. The problem is in the function that generates an enemy list. The enemy characteristics that are being generated, and where the problem is, are the X and Y coordinates. Here is a little bit of code showing the process:
void enemyGenerator(int level)
/* Declare the random generator */
std::default_random_engine generator((unsigned)time(0));
std::uniform_int_distribution<int> coordsX(1, 28); //There are 30 rows, from which the first and the last are border
std::uniform_int_distribution<int> coordsY(1, 48); //50 columns; first and last are border
/* Declare some variables */
int x, y;
bool ready = "False";
/* Check what level is demanded, for example level 1 */
if (level == 1)
{
while(true)
{
//Generate X and Y
x = coordsX(generator);
y = coordsY(generator);
//Now where the problem appears to be
//There will be 600 enemies = 1200 coordinates, so I have declared an array in the .h file
//Called coordinates[1200] = {}; now I want to check it bottom to top if
//the newly generated coordinates are already existing, so:
for (int i = 0; i < 1200; i += 2) //1200 = array size; += 2, because we're checking x & y at once
{
if (x != coordinates[i] && y != coordinates[i + 1] && x + y != 2) //x + y can't be 2, because this is where the player starts (1, 1)
{
if (i == 1198) //If these are the last x and y in the array
{
ready = "True";
break;
//Break the for loop with status ready
}
else
{
continue;
//If it isn't the end of the array simply continue checking
}
}
else
{
ready = "False";
break;
//If the x and y match with the ones in the array, then break with status not ready
}
}
if (ready)
{
break;
//If status is ready then break the loop and assign the rest of the stats
}
else
{
continue;
//If status is not ready then continue generating random values
}
}
//Here I define the values of the stats in private variables of the class
eX = x;
eY = y;
eLVL = 1;
//etc...
}
This is the generating code. And here is how I use it:
void newGame()
....
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
Everything seems logical to me, the only illogical thing is that it is not working. The output in enemyList.txt I get is for example 3 5 1 (other stats) 3 5 1 (other stats) 3 5 1 (other stats), you get it.
Question: Can you spot any error? Can you show me the right way? If more of the code is required I can even send you the source file, just for the sake of curing my headache.
The problem there is with your random generator.
You are setting the seed of the generator everytime enemyGenerator() is called with the current time. But since you call enemyGenerator multiple times in the same fraction of a second, the time value is the same, hence the random generator seed is the same everytime, which will give you the same random pattern each successive call.
Either use the same generator for all the calls
...
std::default_random_engine random_generator((unsigned)time(0));
//I've reached to the point where I want to deploy for example 10 enemies of level 1
for (int i = 0; i < 10; i++)
{
enemyGenerator(random_generator, 1);
//I have an already defined fileWriter (std::fstream; std::ios::out)
fileWriter << eX << " " << eY << " " << eLVL; //<< " " etc...
}
....
with the enemyGenerator function is defined as
void enemyGenerator(std::default_random_engine& generator, int level)
or seed your generator with a different value each time.
Edit:
Well it seems it isn't the cause of your problem but you should still consider what I wrote.
I have tried writing this code to output an odd-order magic square based on user input of an odd number. When I enter 1 or 3, it works fine. Whenever I enter anything above that such as 5, 7, 9, 11, etc the program crashes the moment I press enter. I've reviewed my code and I can't pinpoint where the problem is. I get no error messages.
Small note: if you know what a magic square is, my algorithm here (given to us by the professor in English to translate to C++) does not output the correct values since they don't all add up to the same number.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n; //n = order
cout << "Enter an odd integer for the order of the Magic Square: ";
cin >> n;
cout << endl;
if(n%2 == 0) //only allows program to accept odd numbers
{
cout << "The number you have entered is not odd" << endl;
return 0;
}
int x, y; //x and y access the columns and rows of the following matrix
int magicsquare[n][n]; //creates a n by n matrix to set up magic square
int counter, square = n*n; //square is upper boundary
for(x=0; x<n; x++) //initialize all spaces in matrix with zeros
{
for(y=0; y<n; y++)
magicsquare[x][y] = 0;
}
/*Beginning of the magic square algorithm*/
x = 0, y = n/2; //initialize algorithm at the middle column of the top row
for (counter = 1; counter <= square; counter++) //magic square will contain the integers from 1 to n squared
{
magicsquare[x][y] = counter; //places current counter number at current position in the matrix or square
x--; //moves position diagonally up
y++; //and to the right
/*If a move takes you above the top row in the jth column, move to the bottom of the jth column*/
if(x<0)
x = n - 1;
/*If a move takes you outside to the right of the square in the ith row, move to the left side of the ith row*/
else if(y==n)
y = 0;
/*If a move takes you to an already filled square or if you move out of the square at the upper right
hand corner, move immediately below position of previous number*/
else if((magicsquare[x][y] != 0) || (x<0 && y==n))
{
y--; //move one space to the left back into the square
x = x+2; //move two spots down into the square and below previous number
}
}
for(x=0; x<n; x++)
{
for(y=0; y<n; y++)
cout << setw(5) << magicsquare[x][y];
cout << endl;
}
return 0;
}
I can't follow the logic in my head to know if this can ever actually happen, but in this code:
if(x<0)
x = n - 1;
/*If a move takes you outside to the right of the square in the ith row, move to the left side of the ith row*/
else if(y==n)
y = 0;
If both conditions would have been true, you won't fix up y and the next iteration you'll run off the end of the matrix.
Also note that int magicsquare[n][n]; is a compiler extension and not supported by the C++ standard, since n is not a compile time constant. You almost certainly want to use vector instead.
The following is illegal:
int magicsquare[n][n];
Did you ignore errors, or are you using a compiler that doesn't give errors at all? I suggest you to use an IDE that hints you when a mistake is made, so you can easily see your mistake. Please do not use notepad to write C++, that is horrible.
Fixed version:
int** magicsquare = new int*[n]; //creates a n by n matrix to set up magic square
for(int i = 0; i < n+1; ++i)
magicsquare[i] = new int[n];
Now, together with Mark B's hint, you will get this running up in no time.
Do not forget to cleanup magicsquare by the way using delete.
So I don't really know anything about magic squares. But I think that this is the behavior that you are trying to achieve:
for (int counter = 1, x = 0, y = (n / 2); counter <= n * n; ++counter){
magicsquare[x][y] = counter; //places current counter number at current position in the matrix or square
if (counter % n == 0){ //moves down into the square and below previous number
x = (x + 1) % n;
}
else //moves position diagonally up and to the right
{
x = (x + n - 1) % n;
y = (y + 1) % n;
}
}
Two additional points:
Until we can use the Array Extensions Technical Specification I think you should avoid declaring C99's runtime-sized arrays in your code. Even though gcc will allow it. You might look into doing something like: vector<vector<int>> magicsquare(n, vector<int>(n));
This doesn't match the behavior illustrated by Wikipedia's article but you can get there by tweaking the start values and order of indexing.