Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
i'm having some issues with this bit of code. basically "k" doesn't increment more than 1. I've already tried to declare it outside the loop but doesn't fix it. basically what the code does is generating a grid of crystals.
this is a uni assessment and at the moment I'am a newbie with console functions, especially managing the cursor. as you can see, at every iteration i add +2 to pos.x. it seems to work, but when it starts again, pos.x returns to the start value and instead is pos.y to increment(?).
void gridGeneration(Crystal simbols[][Columns])
{
COORD pos = {10, 55};
for (int i = 0; i < Rows; i++)
for (int k = 0; k < Columns; k++)
{
WriteCrystalAt(simbols[i][k].crystal, pos.X, pos.Y, simbols[i][k].color= rand() % light_yellow + light_blue);
pos.X += 2;
if (k = 1)
{
pos.X = 10;
pos.Y += 2;
}
}
}
It would increment further than 1, but you keep setting it to 1 again:
if (k = 1)
You should use == for comparisons.
Your compiler should have issued a warning about this. If it did not, review your warning settings. If it did, stop ignoring warnings.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I writed this code below, and I got bad results, can anyone help me, and tell me what is wrong ? I writed this in Qt. . If Anyone can help me it would be great.
my Matrix is just some random integral 0 or 1
while( x < obraz.width())
{
while( y < obraz.height())
{
piksel2 = obraz.pixel(x, y);
kolor2 = QColor::fromRgb(piksel2);
minR = kolor2.red();
minG = kolor2.green();
minB = kolor2.blue();
for(i = 0; i < w; i++)
{
for(j = 0; j < h; j++)
{
if (matrix[i][j] == 1 && x - o + i >= 0 && y - u + j >= 0 && x - o + i < obraz.width() && y - u + j < obraz.height())
{
piksel = obraz.pixel(x - o + i, y - u + j);
kolor = QColor::fromRgb(piksel);
if (kolor.blue() < minB)
{
minB = kolor.blue();
}
if(kolor.green() < minG)
{
minG = kolor.green();
}
if(kolor.red() < minR)
{
minR = kolor.red();
}
}
}
}
obraz.setPixel(x, y, qRgb(minR, minG, minB));
y++;
}
y=1;
x++;
}
Input file:
Output file:
The main problem with the code is that it writes the result for each pixel into the input image. This result will be used when computing the min value for the next pixel. Thus, the dark patch at the top-left of the image gets propagated across the whole image.
It is important for this type of algorithm to write into a separate output buffer, leaving the input unchanged until the whole image has been processed.
Do note also that the erosion is well defined for gray-value images, but not for color images. You seem to want to apply marginal ordering, which is equivalent to computing the erosion for each channel independently. Be advised that this method will introduce new colors to the image. There are better approaches, but they all have some sort of downside. I wrote a small overview about this some years ago on my blog.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
you might remember me or running a kind of a 'Lightroom' panel, using C++ and Qt for GUI.
Today I was reading about implementing a unit testing for my main classes, but my question is, how can I test a function that does not return anything?
for example, I got that function:
void ImgProcessing::processMaster(cv::Mat& img, cv::Mat& tmp, int brightness, int red, int green, int blue, double contrast){
for(int i = 0; i < img.rows; i++)
for(int j = 0; j < img.cols; j++)
for(int k = 0; k < 3; k++){
if(k == 0) //_R
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + red )*(259 * (contrast + 255) / (255 * (259 - contrast))));
if(k == 1) //_G
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + green )*(259 * (contrast + 255) / (255 * (259 - contrast))));
if(k == 2) //_B
tmp.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((img.at<cv::Vec3b>(i,j)[k] + brightness + blue )*(259 * (contrast + 255) / (255 * (259 - contrast))));
}
this function just take the obj 'mat img', and modify the 'mat tmp' obj.
than I update the UI for display the modified image, by using another dedicated function in my gui class.
Has someone already encounter something like that?
It does not make a difference if it returns a value the regular way or via an output parameter. The procedure is the same anyway. Run the function and check that the output parameter has the expected value.
This is C code, but it does not make a difference for understanding the concept. Consider these functions:
int addOne1(int x) { return x+1; }
void addOne2(int x, int* ret) { *ret = x+1; }
These can now be tested in this way:
const int x = 3;
int ret1, ret2;
ret1 = addOne1(x);
addOne2(x, &ret2);
assert(ret1 == 4);
assert(ret2 == 4);
If the output parameter also is an input parameter, then you of course need to make sure that you know the initial value.
void inc(int *x) { (*x)++; }
int x=3;
inc(&x);
assert(x == 4);
Technically, modifying a parameter IS considered a side effect. But as long as you are careful it's not a big issue. The difference compared to using a member variable is huge. And if you start modifying globals you will soon make it REALLY hard to test the code.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So, I have a pretty good idea of how to implement the majority of the program. However, I am having a hard time coming up with an algorithm to add the hints of array locations adjacent to mines. The real trouble I am seeing is that the edge cases almost make it like you have two functions to deal with it (I have 20 line max on all functions). I know that from the position of the mine we want a loop to check row - 1 to row +1 and col -1 to col +1, but is it possible to do this in one function with the code I have for the game? If so, some advice would be great!
EDIT!
SO I think I have come up with the algorithm that works for all cases, but it is outputting bad info. I am pretty sure it is due to improper casting, but I am unable to see what's wrong.
Here are the two functions I wrote to add the hints:
void add_hints_chk(char ** game_board, int cur_row, int cur_col, int
rows, int cols)
{
int row_start = 0, row_end = 0, col_start = 0, col_end = 0;
if (cur_row - 1 < 0)
{
//Top edge case
row_start = 0;
}
else
{
row_start = cur_row - 1;
}
if (cur_row + 1 > rows - 1)
{
//bottom edge case
row_end = rows - 1;
}
else
{
row_end = cur_row + 1;
}
if (cur_col - 1 < 0)
{
//Left edge case
col_start = 0;
}
else
{
col_start = cur_col - 1;
}
if (cur_col - 1 > cols - 1)
{
//Right edge case
col_end = cols - 1;
}
else
{
col_end = cur_col + 1;
}
add_hints(game_board, row_start, row_end, col_start, col_end);
}
void add_hints(char **board, int row_start, int row_end, int col_start,
int col_end)
{
int tmp_int = 0;
for (int i = row_start; i <= row_end; i++)
{
for (int j = col_start; j <= col_end; j++)
{
if (board[i][j] != '*')
{
if (board[i][j] == ' ')
{
tmp_int = 1;
board[i][j] = (char)tmp_int;
}
else
{
tmp_int = (int)board[i][j];
tmp_int++;
board[i][j] += (char)tmp_int;
}
}
}
}
}
So, when I print the array, I get the little box with a q-mark in it. Am I converting tmp_int back to a char incorrectly?
There are different strategies to handle this. One simple strategy is creating a larger grid (add one line on each side) that is initialized with no bombs; make the board a view that hides the borders. With this strategy you know that you can step out of the game board without causing issues (since the data structure has an additional row).
Alternatively you can test whether the coordinates are within the valid range before calling the function that tests, or as the first step within that function.
Also you can consider precalculating the values for all of the map, whenever you add a bomb to the board during the pre-game phase, increment the counter of bombs in the vicinity for all of the surrounding positions. You can use either of the above approaches to handle the border conditions.
For any cell, C, there are 8 possible locations to check:
# # #
# C #
# # #
Before extracting data from the array, each outer location must be boundary checked.
You may be able to generalize, for example, if the value (column - 1) is out of bounds, you don't need to check 3 locations.
In your case, I would go with the brute force method and check each outer cell for boundary before accessing it. If profiling identifies this as the primary bottleneck, the come back and optimize it. Otherwise move on.
Edit 1: Being blunt
int C_left = C_column - 1;
int C_right = C_column + 1;
if (C_left >= 0)
{
// The left column can be accessed.
}
if (C_right < MAXIMUM_COLUMNS)
{
// The right columns can be accessed.
}
// Similarly for the rows.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a grid with tetragons and i want to save all the vertices in an array. I wrote ths code:
int counter=0;
int i = 0;
for(i=0; i<=600; i+=40){
verticePosition[counter] = i;
verticePosition[counter+1] = i;
verticePosition[counter+2] = i+40;
verticePosition[counter+3] = i;
verticePosition[counter+4] = i;
verticePosition[counter+5] = i+40;
verticePosition[counter+6] = i+40;
verticePosition[counter+7] = i+40;
counter += 8;
}
I want to save four-four vertices in the table and then i call a function to fill every tetragon with a different color but im getting an error in this for loop:
prog.c:13:1: error: expected identifier or ‘(’ before ‘for’
for(xpos=0; xpox<=600; xpos+=40){
^
and also another error:
prog.c:13:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<=’ token
for(xpos=0; xpox<=600; xpos+=40){
^
I cant find what is wrong with my loop.
The variable xpos is used but not declared, you must declare and initialize it:
for (int xpos = 0; xpos <= 600; xpos += 40) {
Or declare it before the loop:
int xpos;
for (xpos = 0; xpos <= 600; xpos += 40) {
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Does this make any sense?
I got stuck in here with 4 errors and it is because I didn't declared the ints q,d,n,p. But if I do so it'll keep sending me more errors.
There might be something about having mixed ints and floats.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
{
printf("O hai! ");
}
float valueTotal, quarter, valueQuarter, dime, valueDime,nickel, valueNickel, penny, valuePenny;
do
{
printf("How much change is owed?\n");
valueTotal = GetFloat();
}
while (valueTotal <= 0);
for (float quarter = 0; valueTotal >= 0.25; quarter--)
{
valueQuarter = valueTotal - ( q * 0.25);
}
for (float dime = 0; valueQuarter >= 0.10; dime--)
{
valueDime = valueQuarter - ( d * 0.10);
}
for (float nickel = 0; valueDime >= 0.05; nickel--)
{
valueNickel = valueDime - ( n * 0.05);
}
for (float penny = 0; valueNickel >= 0.01; penny--)
{
valuePenny = valueNickel - ( p * 0.01);
}
printf("q+d+n+p\n");
}
I didn't declared the ints q,d,n,p.
This is exactly your problem - at least one of them, anyways. If these variables are undeclared, how in the world is the program/code supposed to evaluate something like q * 0.25 ? If I said "Hey man, what is x times 0.25?" You'd have absolutely no idea, or tell me that the answer depends on x. The same goes with this code.
You said:
But if I do so it'll keep sending me more errors.
I'm assuming you also need to initialize them (or, in layman's terms, set them equal to something ie. q = 0)
Also, none of your loop conditions are actually changing.... meaning they're infinitely looping. Make sure that the code inside your loop is actually helping you reach the goal of satisfying the loop condition; for example:
for (float quarter = 0; valueTotal >= 0.25; quarter--)
{
valueQuarter = valueTotal - ( q * 0.25);
}
valueTotal is ALWAYS going to be greater than 0.25 (if it is less than 0.25 to begin with) since you are never changing it at all.