Find intersection in 2d arrays - c

I wrote a small code to find intersection of two 2d array, unfortunately it is not working, so maybe you can help me.. Intersection is, if both numbers on place (x,y) is a "1". Otherwise there should be "0"
void intersection(int *mat, int rows, int cols) {
int rows1 = 5, cols1 = 4; // secend matrix is in function because i just need it here
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i = 0;
int j = 0;
int x = 0;
int y = 0;
while (j < cols && y < cols1) { // maybe it is better with a for loop ??
j += 1;
y += 1;
while (i < rows && x < rows1) {
i += 1;
x += 1;
if (mat[j*rows+i] == 1 && ma2[y*rows1+x] == 1) {
printf("%d ", mat[j*rows+i]);
break;
} else {
printf("%d ", mat[j*rows+i]);
break;
}
}
printf("\n");
}
}
int main (void) {
int rows = 5, cols = 4; //first matrix is in main, because i need it for other functions
int ma[] = { 0, 0, 1, 0, 0, // 1. Zeile
1, 0, 0, 0, 0, // 2. Zeile
1, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
intersection(ma, rows, cols);
return 0;
}
Output should be (in this case):
{ 0, 0, 1, 0, 0, // 1. Zeile
0, 0, 0, 0, 0, // 2. Zeile
0, 0, 1, 0, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
but i just get a matrix with 1 row
Greets ;)

try this
#define min(x,y) ((x) < (y) ? (x) : (y))
void intersection(int *mat, int rows, int cols) {
rows = min(rows, 5);//rows <--> cols
cols = min(cols, 4);
int ma2[] = { 0, 0, 1, 0, 1, // 1. Zeile
0, 0, 1, 0, 1, // 2. Zeile
0, 0, 1, 1, 0, // 3. Zeile
0, 0, 1, 0, 0 // 4. Zeile
};
int i, j;
for(i = 0; i < cols; ++i){
for(j = 0; j < rows; ++j){
//printf("%d ", mat[i*rows + j] == ma2[i*rows + j] ? mat[i*rows + j] : 0);
printf("%d ", mat[i*rows + j] & ma2[i*rows + j]);
}
printf("\n");
}
}

I solved the problem with this solution ;) Thank you everyone for helping me ...
void intersection(int *mat, int rows, int cols) {
int ma2[4][5] = {{0, 1, 0, 0, 0},
{0, 1, 0, 0, 0},
{1, 1, 0, 1, 0},
{0, 1, 0, 0, 0}};
int i = 0;
int j = 0;
int t = 1;
int s = 0;
for(j = 0; j < cols; j++) {
for (i = 0; i < rows; i++) {
if (ma2[j][i] && mat[j*rows+i] == 1) {
printf("%d ", t);
} else {
printf("%d ", s);
}
}
printf("\n");
}
}

Related

Print out cycle of directed graph from adjacency matrix using DFS in C

#include<stdio.h>
#include<stdlib.h>
int visited[100];
int A[8][8] = {
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 1, 0, 0, 0, 0, 1},
{0, 1, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 1},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0}
};
void DFS(int i){
printf("%d ", i);
int back = 0;
visited[i] = 1;
for (int j = 0; j < 8; j++) {
if (visited[j]) {
if (back == 1 ) {
printf("\nrevisited: %d\n", j);
}
back = 1;
}
if(A[i][j]==1 && !visited[j]){
DFS(j);
}
}
}
void DFSLinker() {
for (int k = 0; k < 100; k++) {
visited[k] = 0;
}
for (int k = 0; k < 8; k++) {
if (visited[k] == 0) {
DFS(k);
}
}
}
int main(){
DFSLinker();
return 0;
}
I have an adjacency matrix of an directed graph and I am trying to print out any one cycle in this directed graph.
So far, this code prints out the DFS traversal as well as any of the nodes that I have already visited while doing the DFS traversal.
I know that the cycles in this particular directed graph are as follows:
1 2
4 7
4 5 7
When I try to get the very last revisited nodes in order to print out a cycle, I get 7 5 7, which is not a valid cycle.
What am I missing?

Converting 2D array into a greyscale image in C

I imported the 2d array of Lena using a header file (LenaArray.h); int lena [511][511] = {162,162,162,etc...},
but now I want to convert it into a greyscale image and I don't know how please help? Image of Lena I'm trying to print
#include <stdio.h>
#include "LenaArray.h"
int main () {
int i,j;
int width = 511;
int height = 511;
for (i = 0; i < height; i ++ )
{
for(j = 0; j < width; j ++)
{
printf("%d",&lena[i][j]);
}
}
}
#include <stdio.h>
typedef unsigned char U8;
typedef struct { U8 p[4]; } color;
U8 lena[511][511];
void save(char* file_name,int width,int height)
{
FILE* f = fopen(file_name, "wb");
color tablo_color[256];
for (int i = 0; i < 256; i++)
tablo_color[i] = { (U8)i,(U8)i,(U8)i,(U8)255 };//BGRA 32 bit
U8 pp[54] = { 'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0 ,
40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 32 };
*(int*)(pp + 2) = 54 + 4 * width * height; //file size
*(int*)(pp + 18) = width;
*(int*)(pp + 22) = height;
*(int*)(pp + 42) = height * width * 4; //bitmap size
fwrite(pp, 1, 54, f);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
U8 indis = lena[i][j];
fwrite(tablo_color+indis,4,1,f);
}
}
fclose(f);
}
int main()
{
for (int i = 0; i < 511; i++)
{
for (int j = 0; j < 511; j++)
{
lena[i][j]=i+j;
}
}
save("test.bmp", 511, 511);
}

C - How to fix recursive function return type error

I'm not sure why my solve_sudoku function results in this error:
error: void value not ignored as it ought to be
My full code is below. Please note I have to keep void as return type.
Any help is appreciated.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print_sudoku(int sudoku[9][9]){
printf("The Sudoku contains:\n");
for (int j=0; j<9; j++)
{
for (int i=0; i<9;i++)
{
printf("%d ",sudoku[j][i]);
}
printf("\n");
}
}
int rowExists(int sudoku[9][9], int i, int num){
for (int j=0;j<9;j++){
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int colExists(int sudoku[9][9], int j, int num) {
for (int i=0;i<9;i++) {
if (sudoku[i][j]==num){
return 1;
}
}
return 0;
}
int valExists(int sudoku[9][9], int i, int j, int num) {
for (int r=0;r<3;r++){
for (int s=0;s<3;s++){
if (sudoku[r+i][s+j]==num){
return 1;
}
}
}
return 0;
}
int DNE(int sudoku[9][9], int *i, int *j) {
for (*i=0; *i<9; (*i)++){
for (*j=0;*j<9;(*j)++){
if (sudoku[*i][*j]==0){
return 1;
}
}
}
return 0;
}
void solve_sudoku(int sudoku[9][9], int depth){
int i=0;
int j=0;
if (!DNE(sudoku, &i, &j)){
return;
}
for (int k=1;k<=9;k++){
if (!rowExists(sudoku, i, k) && !colExists(sudoku, j, k) && !valExists(sudoku, i-(i%3), j-(j%3), k)){
sudoku[i][j]=k;
if (solve_sudoku(sudoku, depth)){
return;
}
sudoku[i][j]=0;
}
}
return;
}
#ifndef __testing
int main(){
int Sudoku[9][9]={{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printf("Input puzzle is:\n");
print_sudoku(Sudoku);
solve_sudoku(Sudoku, 0);
printf("Solution is:\n");
print_sudoku(Sudoku);
}
#endif
The following line inside solve_sudoku function expectes that solve_sudoku function will return a boolean value but return type of the function is void, thus creating the error.
if (solve_sudoku(sudoku, depth))
You can change signature of the function like below and return true/false as per condition.
Tips: when passing a multi dimesional array you don't need to specify the first dimension. Ex: int sudoku[][9],
bool solve_sudoku(int sudoku[][9], int depth)
Because your solve_sudoku returns void, so the condition check if (solve_sudoku(sudoku, depth)) shows that error - we can't check a "void" is true or false.
You should let it return int.

Wave Algorithm (Lee's Algorithm): incorrect final matrix

I'm writing a program calculating the shortest way from point A to point B.
I have a map (matrix) with values:
0 is block (wall, no way to pass);
1 is free way (you can pass);
2 is start point;
In the code below I declare 2 arrays: an array " map"and changed array "visited" while running program demonstrating visited points.
I check the cells in 4 directions (not diagonals) for 1 or 0. If it's 1 (possible to pass), I increase the counter for 1. For do not count the previous cell I'm trying to avoid it by the condition. I realized that in two one-dimensional arrays {1 0 -1 0} and { 0, 1, 0, -1 } to check neighbor points (what mean i check [i+1][j], [i-1][j], [i][j+1] and [i][j-1]).
As a result I wanna see "visited" matrix with a few lines which shows the way to reach to the point B (1, 2, 3, ... 15). I wanna find the way to map[7][7] point.
Right now the error here that I do count++ for the previous position. How to avoid that?
Thank you.
P.S. I wrote a few functions implementing a new array with 0 values, counting free to go cells and printing arrays.
main.c:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 8
#define HEIGHT 8
int mapZero(int map[WIDTH][HEIGHT]);
int mapPrint(int map[WIDTH][HEIGHT]);
int mapInit(int map[WIDTH][WIDTH]);
int findFreeToGoCells(int map[WIDTH][HEIGHT]);
int main(int argc, char * argv[])
{
bool stop;
unsigned int count;
unsigned int max;
int visited[WIDTH][HEIGHT];
int map[WIDTH][HEIGHT] =
{
{ 1, 1, 1, 1, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 1, 1 },
};
mapZero(visited);
printf("Matrix of zeroed-visited cells:\n\n");
mapPrint(visited);
printf("Matrix of the map:\n\n");
mapPrint(map);
printf("Free to go cells: %d\n\n", findFreeToGoCells(map));
max = WIDTH * HEIGHT - 1;
visited[0][0] = map[0][0];
count = 0;
visited[0][0] = 0;
int di[4] = { 1, -1, 0, 0 };
int dj[4] = { 0, 0, 1, -1 };
//do
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (visited[i][j] == count)
{
for (int k = 0; k < 4; ++k)
{
int i_check = i + di[k];
int j_check = j + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] != 0))
{
visited[i_check][j_check] = count + 1;
}
}
count++;
}
}
}
}// while (visited[7][7] == 0);
if (count > max + 99999)
printf("The way couldn't be found\n");
else
{
printf("Matrix of visited cells:\n\n");
mapPrint(visited);
printf("Free to go cells from [0][0] to [7][7]: %d\n", findFreeToGoCells(visited));
}
/*************************************************************************************/
/*************************************************************************************/
int len;
int x = 7;
int y = 7;
int x_path[WIDTH * HEIGHT];
int y_path[WIDTH * HEIGHT];
len = visited[7][7];
count = len;
while (count > 0)
{
x_path[count] = x;
y_path[count] = y;
count--;
for (int k = 0; k < 4; ++k)
{
int i_check = x + di[k];
int j_check = y + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] == count))
{
x = x + di[k];
y = y + dj[k];
break;
}
}
}
x_path[0] = 0;
y_path[0] = 0;
printf("\nThe shortest way consist of %d cells\nThere are %d the shortest way to reach th the final point\n\n", len, findFreeToGoCells(visited)-len);
system("pause");
return 0;
}
int mapZero(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
map[i][j] = 0;
}
}
return 0;
}
int mapPrint(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
printf("%2d ", map[i][j]);
}
printf("\n\n");
}
printf("\n");
return 0;
}
int findFreeToGoCells(int map[WIDTH][HEIGHT])
{
int count = 0;
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (map[i][j] != 0) count++;
}
}
return count;
}
Result:

-1.#Q0 shows up randomly

I have the following code:
float w[n][n] = {
{0, 0, 0.5, -1, 0, 0, 0},
{0, 0, 1.5, -2, 0, 0, 0},
{0, 0, 0, 0, 1, -1, 0},
{0, 0, 0, 0, 3, -4, 0},
{0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, -3}
};
float x[ne] = {2, -1};
float d = 1;
float alpha = 0.1;
float in[n];
float delta[n];
float a[n];
float sum;
for(j = ne; j <= n; j++) {
for(i = 0; i <= n; i++) {
in[j] += w[i][j] * a[i];
}
a[j] = g(in[j]);
}
for(i = 0; i < n; i++) {
printf("a[%d] = %.3f\n", i+1, a[i]);
}
delta[n-1] = d - a[n-1];
for(i = n-2; i >= ne; i--) {
for(j = 0; j <= n; j++) {
sum += w[i][j] * delta[j];
}
delta[i] = g(in[i]) * (1 - g(in[i])) * sum;
printf("delta[%d] = %.3f\n", i+1, delta[i]);
sum = 0;
}
printf("\n\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
if (w[i][j] != 0) {
w[i][j] = w[i][j] + alpha * a[i] * delta[j];
printf("w[%d][%d] = %.3f\n", i+1, j+1, w[i][j]);
}
}
}
I keep getting some variation of this output:
a[1] = 2.000
a[2] = -1.000
a[3] = 0.378
a[4] = 0.500
a[5] = 0.867
a[6] = 0.085
a[7] = 0.649
delta[6] = -1.#QO
delta[5] = -1.#QO
delta[4] = -1.#QO
delta[3] = -1.#QO
w[1][3] = -1.#QO
w[1][4] = -1.#QO
w[2][3] = -1.#QO
w[2][4] = -1.#QO
w[3][5] = -1.#QO
w[3][6] = -1.#QO
w[4][5] = -1.#QO
w[4][6] = -1.#QO
w[5][7] = 1.030
w[6][7] = -2.997
It shows up in different places everytime I compile, knowing that the first time it was giving me the right answer, I'm not sure what went wrong?
That code is not complete. Why did you leave out the declarations all the variables? Because they "aren't important?"
Note that those declarations ARE IMPORTANT.
I can't tell but I predict that d is not set to 0 and is picking up 0xFFFFFFFF from the stack. Which is Not A Number in floating point. A NaN value in a calculation always results in NaN results.

Resources