I wrote a function where a 1-dimensional array (int8_t tabelle1[768]) is transferred into a 2-dimensional array (int8_t tabelle[24][32]).
I now want to find a "Hotspot" in the 2-dimensional array. A "Hotspot" is an 2x2 block in the array (can be located anywhere) where each value of the block has to be equal or over 30.
My Problem is now that my program is not going into my if statement.
The "Checkpoint" is not being printed.
The function:
bool Hotspotberechnung(int8_t tabelle1[768],int8_t tabelle2[24][32])
{
int i=0, j=0, x=0, y=0;
for(j=0; j<24; j++) //Transfer from 1D into 2D
{
for(i=0; i<32; i++)
{
tabelle2[j][i] = tabelle1[(j*32)+i];
}
}
for(y=0; y<24; y++) //search for hotspot
{
for(x=0; x<32; x++)
{
if(tabelle2[y][x]>=30)
{
printf ("1\n"); // checkpoint
if(tabelle2[y][x+1]>=30)
{
if(tabelle2[y+1][x]>=30)
{
if(tabelle2[y+1][x+1]>=30)
{
for(j=0; j<24; j++)
{
for(i=0; i<32; i++)
{
printf("%d.%d=%d\n",j+1,i+1,tabelle2[j][i]);
}
}
printf ("Hotspot!");
return true;
}
else
return false;
}
else
return false;
}
else
return false;
}
else
return false;
}
}
return 0;
}
It never reaches the print, because one of your many returns ends the function much sooner than you think. Specifically the one in the else of the then wich contains the print.
Use a debugger and you will see that.
Alternatively put a print next to each else, make sure to use {} for that. Also put a print after the call to the shown function.
for(y=0; y<24; y++) //search for hotspot
{
for(x=0; x<32; x++)
{
if(tabelle2[y][x]>=30)
{
/* this is not reached for y=0 and x=0, i.e. first */
printf ("1\n"); // checkpoint
/* much code deleted */
}
else
return false;
/* this is reached first,
end of the function,
end of the loop,
no printing ever */
}
}
A few things ...
The kernal width/height is 2, so we have to stop short of the array width/height or we'll go beyond the end of the array
Once we find an element that matches, we have to use that element as the upper left anchor of the 2x2 box
Here's the refactored code (it compiles, but is not tested):
#include <stdio.h>
#include <stdint.h>
#define YMAX 24
#define XMAX 32
#define BOXWID 2
#define MARG (BOXWID - 1)
int
Hotspotberechnung(int8_t tabelle1[YMAX * XMAX], int8_t tabelle2[YMAX][XMAX])
{
int i = 0,
j = 0,
x = 0,
y = 0;
// Transfer from 1D into 2D
for (j = 0; j < YMAX; j++) {
for (i = 0; i < XMAX; i++)
tabelle2[j][i] = tabelle1[(j * XMAX) + i];
}
for (y = 0; y < (YMAX - MARG); y++) {
int8_t *row = tabelle2[y];
for (x = 0; x < (XMAX - MARG); x++) {
if (row[x] < 30)
continue;
int8_t *box = &row[x];
int match = 1;
for (int yoff = 0; yoff < BOXWID; ++yoff) {
for (int xoff = 0; xoff < BOXWID; ++xoff) {
if (box[(yoff * XMAX) + xoff] < 30) {
match = 0;
break;
}
}
if (! match)
break;
}
if (! match)
continue;
for (int yoff = 0; yoff < BOXWID; ++yoff) {
for (int xoff = 0; xoff < BOXWID; ++xoff) {
int8_t val = box[(yoff * XMAX) + xoff];
printf("[%d,%d] = %d\n",y + yoff,x + xoff,val);
}
}
}
}
return 0;
}
Related
I have written a program with a function that looks at a two dimensional array ir_data[60][768].
The While loop should call the "Hotspotberechnung" function only when there is a value that equals or is over 30 in a given row.
"Hotspotberechnung" should store the given row in an array[24][32] and then print the array.
The problem is that the program prints an array without any values equal or over 30.
Unfortunately, I cannot find the mistake on my own.
The While Loop:
int8_t Temperatur[768] = {0};
while (get_ir_data( &Temperatur[0], sizeof(Temperatur)/sizeof(Temperatur[0])) == 0)
{
for(int k=0; k<768; k++)
{
if(Temperatur[k] >= 30)
{
Hotspotberechnung(Temperatur,bild);
}
}
}
The function :
bool Hotspotberechnung(int8_t tabelle1[768],int8_t tabelle2[24][32])
{
int i=0, j=0, x=0, y=0;
for(j=0; j<24; j++) //Tabellen werden gefüllt
{
for(i=0; i<32; i++)
{
tabelle2[j][i] = tabelle1[(j*24)+i];
}
}
for(j=0; j<24; j++)
{
for(i=0; i<32; i++)
{
printf("%d.%d=%d\n",j,i,tabelle2[j][i]);
}
}
get_ir_data :
int8_t get_ir_data(int8_t* data, int n)
{
static uint8_t idx = 0;
if (n != 24 * 32) {
printf("Invalid size of array data!\n");
return -1;
}
memcpy(data, ir_data[idx], n);
idx++;
if (idx >= sizeof(ir_data) / sizeof(*ir_data))
return -2;
return 0;
}
tabelle1[(j*24)+i]
This index calculation is wrong. You need
tabelle1[(j*32)+i]
To verify, calculate the maximal value of (j*24)+i, given the ranges of j (0 to 23) and j (0 to 31). Is it 767?
I am trying to build a square/rectangle using "Xs" on a 1:1 scale with the length and width, but the logic seems to be not perfect
void draw (float x, float y) {
int i, j;
int length = (int)x + 0;
int width = (int)y + 0;
for (i = 1; i < length; i++) {
for (j = 1; j < width; j++) {
if (((i = 1) || (i = length)) && ((j = 1) || (j = width))) {
printf("x");
} else {
printf(" ");
}
}
printf("\n");
}
}
The problem is that the loop iterates endlessly printing x's everywhere. I'm expecting Xs to be printed out in either a square or rectangular shape (depending on the length or width).
I see 3 flaws in your logic.
you mix = (assignment) with == comparison.
This is why your loop never ends: you always reset i to 1 with your if (((i = 1) || (i = length)) &...
you're not going far enough with your variables:
if i < length, then you'll never have it reach length and print the bottom line of X's
you can't draw a rectangle because your && in the test (((i = 1) || (i = length)) && ((j = 1) || (j = width))) is too restrictive. It can't work if width different from length.
You must learn the logic by yourself using tutorials:
Here such questions will be discarded by sometime-sad people. But as a welcome, here it is (you can replace y in the second case by x, but I thought it would help you understand):
#include <stdio.h>
#include <string.h>
void draw (float x, float y)
{
int i,j;
int length = (int)x + 0;
int width = (int)y + 0;
for(i=1; i<=length; i++) {
for(j=1;j<=width;j++) {
if(((i==1)||(i==length))) {
printf("x");
} else {
if (((j==1)||(j==width))) {
printf("y");
} else {
printf(" ");
}
}
}
printf("\n");
}
}
I'm trying to track a player's location with x marking their spot. When the player enters a string I increment the coordinates accordingly. However when the player is located one space from the perimeter, then attempts to move to the edge of the map, the player disappears.
Example:
.....
...x.
.....
.....
.....
Player located at 'x'
If player enters string "right" and I move player_loc, array simply returns:
.....
.....
.....
.....
.....
I attempted to add a sort of buffer by increasing the size of the array. No luck. I've been stuck on this for almost a week now. Any help would be appreciated. I apologize for messy code. I'm a total newbie at this and I'm really just futzing around in the dark with all this stuff. I've researched this across the forums here and haven't found a solution. If you know of something that I possibly (probably) missed feel free to point me in that direction.
#include <stdio.h>
#include <string.h>
char map[6][6];
char player_loc = 'x';
int row;
int col;
void init_map()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
map[i][j] = '.';
}
}
}
void print_map()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
}
int get_player_loc()
{
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
if(map[j][k] == player_loc)
{
row = k;
col = j;
}
}
}
return row;
return col;
}
void init_player_loc()
{
int check = 1;
for (int g = 0; g < 5; g++) {
for (int h = 0; h < 5; h++) {
if (map[g][h] == 'x') {
check = 0;
}
}
}
if(check == 1) {
map[0][0] = player_loc;
} else {
get_player_loc();
}
}
void move_left()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j-1] = player_loc;
map[i][j] = '.';
}
}
}
}
void move_right()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
}
}
}
}
int main(int argc, char* argv[])
{
char input[15];
printf("You enter a room...you can go left, right, or straight. Which way do you go?\n");
int done = 0;
init_map();
map[3][3] = player_loc;
//init_player_loc();
print_map();
while (!done) {
scanf("%s", input);
if (strcmp("left", input) == 0) {
move_left();
printf("You go left...\n");
print_map();
get_player_loc();
printf("%d %d\n", row, col);
done = 1;
}
else if (strcmp("right", input) == 0) {
move_right();
printf("You go right...\n");
print_map();
get_player_loc();
printf("%d %d\n", row, col);
done = 1;
}
else if (strcmp("straight", input) == 0) {
printf("You go straight...");
done = 1;
}
else {
printf("Sorry, can't do that.\n");
}
}
}
You must break the loop if you find the player location, e.g
void move_right()
{
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
return;
}
}
}
}
In your code you move right the player, and the next loop will find the player in the new location and do the right move again, forever.
Moreover in your code you are not taking care of boundaries of your 2d matrix: j+1 is valid only if j<5.
Then a better code should be
void move_right()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
return;
}
}
}
}
The problem is that your move_right function picks up the player and moves them completely off of the map. Let's say your player is at [0, 2] and step through the code.
for (int j = 0; j < 5; j++) {
if (map[i][j] == player_loc) {
map[i][j+1] = player_loc;
map[i][j] = '.';
}
}
[0, 0] No player here, move along
[0, 1] No player here, move along
[0, 2] Found a player! Move them right to [0, 3]
[0, 3] Found a player! Move them right to [0, 4]
[0, 4] Found a player! Move them right to [0, 5]
At 5, the loop ends. Because of the buffer you added, your array is 6x6, so the player is stashed in the wings without crashing the program. There are a few things you should do:
Once you've found and moved the player, break or return so they'll only move once.
Make your array 5x5 (or print all 6x6) so you can see everything.
Do some bounds checking so the player isn't allowed to move right from j = 5.
Watch out for this same bug in move_up, where it would happen as you increment i.
Your loops allow for checking the position twice, once at i,j, and again at i,(j+1) (or some other variant). This probably isn't what you intend. After you find the player you should make the updates and then break out of the loops.
Also, the code as is allows for indexing passed the bounds of the array, in theory. Also not what is desired. You may consider bounds checking. I don't know what is supposed to happen when the player moves right and there is a wall to the right. Does he not move? Wrap around? LR corner could cause seg fault as it is now.
You appear to have row and column indeces transposed in the get_player_loc function, as well as having two return statements (the compiler should warn you about unreachable code), neither of which is required or used by the calling code.
At the start, initialise the row and col variables. (Values taken from your main.)
int row = 3;
int col = 3;
Change the get_player_loc function so that it just updates the globals row and col. It sets row and col to 0 if the player is not found, as per the original.
void get_player_loc(void)
{
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 5; k++) {
if(map[j][k] == player_loc)
{
// The meaning of row and col is set by how they are used
// to index the array in the move and print functions. Keep
// the same order and meaning here.
row = j;
col = k;
return;
}
}
}
// Set row and col to 0 if the location is not found.
row = 0;
col = 0;
map[0][0] = player_loc;
}
You'll still have problems when they reach an edge, due to the index into the array going out of bounds in the move functions, but that's a different problem.
The program triggers a break point on the free() function call.
Debugging the program returns this message:
HEAP[C_C.exe]: Heap block at 00498240 modified at 00498298 past requested size of 50
C_C.exe has triggered a breakpoint.
I don't understand why free() triggers the break point if all seems correct...
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int* aloca_vetor(int control)
{
int *vetor;
vetor = (int *)malloc((control * sizeof(int)));
return vetor;
}
int main()
{
int *vetor;
int Input[2];
int testes;
int control;
int i, j, w = 0;
scanf("%d", &testes);
while (testes != 0)
{
scanf("%d%d", &Input[0], &Input[1]);
control = ((Input[1] - Input[0]) + 2);
vetor = aloca_vetor(control);
if (vetor == NULL)
{
printf("No memory!");
}
for (i = 2; i < control; i++)
{
vetor[i] = TRUE;
}
for (i = 2; i < control; i++)
{
if (vetor[i] == TRUE)
{
j = 0;
for (w = 0; j < (control - 1); w++)
{
j = i*i + w*i;
vetor[j] = FALSE;
}
}
}
for (i = 0; i < control; i++)
{
if (vetor[i] == TRUE)
{
printf("%d\n", i);
}
}
testes--;
free(vetor);
}
return 0;
}
I think that the problem arises in this loop
for (w = 0; j < (control - 1); w++)
{
j = i*i + w*i;
vetor[j] = FALSE;
}
It seems that the value calculated such a way as
j = i*i + w*i;
can be greater than control.
You could rewrite the loop like
for (w = 0; ( j = i*i + w*i ) < (control - 1); w++)
{
vetor[j] = FALSE;
}
Also in my opinion the loop
while (testes != 0)
{
//...
testes--;
free(vetor);
}
would be more readable if it would be rewritten the following way
while ( testes-- != 0 )
{
//...
free(vetor);
}
or
while ( testes-- )
{
//...
free(vetor);
}
Take into account that you wrote the program such a way that it seems nobody understands what it does.:) You should try to write programs more readable even if they are test programs.
j = i*i + w*i will become much larger than the last valid index for the array. Accesses beyond the borders of an array presents undefined behaviour.
One effect might be to overwrite memory used by the hep management, so the effect will only become visible when calling free.
I need to find the duplicate elements in a two dimensional array.
route_ptr->route[0][1] = 24;
route_ptr->route[0][2] = 18;
route_ptr->route[1][1] = 25;
route_ptr->route[2][1] = 18;
route_ptr->route[3][1] = 26;
route_ptr->route[3][2] = 19;
route_ptr->route[4][1] = 25;
route_ptr->route[4][2] = 84;
Those are my data; the duplicate entries of route[2][1] (duplicate of route[0][2]) and route[4][1] (duplicate of route[1][1]) has to be found.
The solution is the duplicate 'i' value of route[i][j] which is 2 & 4 from this example.
please guide me.
#include <stdio.h>
struct route{
int route[6][6];
int no_routes_found;
int count_each_route[6];
};
int main() {
struct route *route_ptr, route_store;
route_ptr=&route_store;
int i,j,k;
// the data
route_ptr->route[0][1] = 24;
route_ptr->route[0][2] = 18;
route_ptr->route[1][1] = 25;
route_ptr->route[2][1] = 18;
route_ptr->route[3][1] = 26;
route_ptr->route[3][2] = 19;
route_ptr->route[4][1] = 25;
route_ptr->route[4][2] = 84;
route_ptr->count_each_route[0]=3;
route_ptr->count_each_route[1]=2;
route_ptr->count_each_route[2]=2;
route_ptr->count_each_route[3]=3;
route_ptr->count_each_route[4]=3;
route_ptr->no_routes_found=5;
//// process
for (i = 0; i <(route_ptr->no_routes_found) ; i++)
{
for (j = 1; j < route_ptr->count_each_route[i]; j++)
{
printf("\nroute[%d][%d] = ", i, j);
printf("%d",route_ptr->route[i][j]);
}
}
}
The solution expected is:
route[0][1] is compared by route [0][2] i.e [24 !=18]
route[0][1] and route [0][2] is compared by route[1][1] i.e [24 && 18 !=25]
route[0][1] and route[0][2] and route[1][1] is compared by route[2][1] i.e [ 24&&18&&25 is compared by 18, there is a matching element,
save the newcomer 'i' value which matches to the existence and drop it for next checking]
break the 'i' loop
route[0][1], route[0][2], route[1][1] is now compared route[3][1]
route[0][1], route[0][2], route[1][1] ,[3][1] is now compared route[3][2]
route[0][1], route[0][2], route[1][1] ,[3][1] ,[3][2] is now compared to route [4][1] i.e [ now there is a match to route[1][1], so save the newcomer 'i' value and break the 'i' loop
So i values [2 and 4] are duplicate, and that is my expected result of my code.
Got something against index zero, zero?
I also don't see the point of the pointer shenanigans.
It's a general safety thing to initialize all your data. You know, to zero or something.
The algorithm you suggest in your solution is rather hard to be faithful to, but this will find your duplicates. You have to walk through the entire array, in both dimensions, twice.
This will also match all the zeroes in your data, so you could add an exception to ignore routes values of zero.
//Cycling through the array the first time.
for (i = 0; i < 6 ; i++)
{
for (j = 0; j < 6; j++)
{
//Cycling through the array the second time
for (x = 0; x < 6 ; x++)
{
for (y = 0; y < 6; y++)
{
if(i==x && j==y)
continue;
if(routestore.route[i][j] == routestore.route[x][y])
printf("You have a match [%d][%d] = [%d][%d]", i, j, x,y);
}
}
}
}
Ok, so if you only want to see matches once, ie [0][2] == [2][1] but not [2][1] == [0][2], then you can do something like what I have below. This one made me scratch my head. Usually, when it's a simple list of items, you initialize the inner loop to the value of the outer loop, plus one. But you can't quite do that when it's a 2D array. So I gave up and made a super-lame hack-job. I'm a big fan of brute forcing things when possible. I'd normally tell you not to use pointers like this.
Now... this will still have multiple hits if you have three similar values. If that irks you then you need to start building a list and comparing hits against that as you walk through the data.
#include <stdio.h>
#include <string.h>
struct route{
int route[6][6];
int no_routes_found;
int count_each_route[6];
};
int lameAddOneAlternative(int *i, int *j)
{
if((*j)<6)
{
(*j)++;
return 1;
}
else if (*i<6)
{
(*i)++;
(*j) = 0;
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
struct route routeStore;
int i,j,x,y;
memset(routeStore.route,0,sizeof(int)*36);
// the data
routeStore.route[0][1] = 24;
routeStore.route[0][2] = 18;
routeStore.route[1][1] = 25;
routeStore.route[2][1] = 18;
routeStore.route[3][1] = 26;
routeStore.route[3][2] = 19;
routeStore.route[4][1] = 25;
routeStore.route[4][2] = 84;
//Cycling through the array the first time.
for (i = 0; i < 6 ; i++)
{
for (j = 0; j < 6; j++)
{
x=i;
y=j;
//Cycling through the array the second time
while(lameAddOneAlternative(&x,&y))
{
if(routeStore.route[i][j] == 0 )
continue;
if(routeStore.route[i][j] == routeStore.route[x][y])
printf("You have a match [%d][%d], [%d][%d] == %d\n", i, j, x,y, routeStore.route[i][j] );
}
}
}
}
for (i = 0; i <(route_ptr->no_routes_found) ; i++)
{
for (j = 1; j < route_ptr-> count_each_route[i]; j++)
{
for (x = 0; x < (route_ptr->no_routes_found) ; x++)
{
for (y = 0; y < route_ptr-> count_each_route[x]; y++)
{
if(i==x && j==y)
continue;
if(route_ptr->route[i][j] == route_ptr->route[x][y])
printf("You have a match [%d][%d] = [%d][%d]\n", i, j, x,y);
}
}
}