-1.#Q0 shows up randomly - c

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.

Related

CS50x - Filter More

I doing filter of images bmp in c. The pset required the Sobel Operator. I don't know where I being mistake.
Help me please.
I'm basically making a copy of my image (because the original will be changed.)
Then I take the 3x3 values to put in the formula
So we add and multiply
Finally I take the result and put it in the formula: square root (Gx ^ 2 + Gy ^ 2)
If it exceeds 255 it must be 255, because RGB goes up to 255 which is white
And if there is a broken number, round to the nearest
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Variáveis
RGBTRIPLE temp[height][width];
int GR[3][3];
int GG[3][3];
int GB[3][3];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
float resultR, resultG, resultB;
// Cópia temporária do original
for (int tempi = 0; tempi < height; tempi++)
{
for (int tempj = 0; tempj < width; tempj++)
{
temp[tempi][tempj].rgbtRed = image[tempi][tempj].rgbtRed;
temp[tempi][tempj].rgbtGreen = image[tempi][tempj].rgbtGreen;
temp[tempi][tempj].rgbtBlue = image[tempi][tempj].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int countx = 0;
// Pegar 3X3
for (int x = i - 1; x < i + 2; x++)
{
int county = 0;
for (int y = j - 1; y < j + 2; y++)
{
if ((x < 0 || y < 0) || (x >= height || y >= width))
{
GR[countx][county] = 0;
GG[countx][county] = 0;
GB[countx][county] = 0;
}
else
{
GR[countx][county] = temp[x][y].rgbtRed;
GG[countx][county] = temp[x][y].rgbtGreen;
GB[countx][county] = temp[x][y].rgbtBlue;
}
county++;
}
countx++;
}
float sumxR = 0, sumyR = 0, sumxG = 0, sumyG = 0, sumxB = 0, sumyB = 0;
for (int ix = 0; ix <= 2; ix++)
{
for (int iy = 0; iy <= 2; iy++)
{
sumxR = sumxR + (GR[ix][iy] * Gx[ix][iy]);
sumxG = sumxG + (GG[ix][iy] * Gx[ix][iy]);
sumxB = sumxB + (GB[ix][iy] * Gx[ix][iy]);
sumyR = sumyR + (GR[ix][iy] * Gy[ix][iy]);
sumyG = sumyG + (GG[ix][iy] * Gy[ix][iy]);
sumyB = sumyB + (GB[ix][iy] * Gy[ix][iy]);
}
}
resultR = sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR);
resultG = sqrt(sumxG * sumxG) + sqrt(sumyG * sumyG);
resultB = sqrt(sumxB * sumxB) + sqrt(sumyB * sumyB);
if (resultR > 255)
{
resultR = 255;
}
if (resultG > 255)
{
resultG = 255;
}
if (resultB > 255)
{
resultB = 255;
}
image[i][j].rgbtRed = round(resultR);
image[i][j].rgbtGreen = round(resultG);
image[i][j].rgbtBlue = round(resultB);
}
}
}
You need to changesqrt(sumxR * sumxR) + sqrt(sumyR * sumyR) to sqrt((sumxR * sumxR) + (sumyR * sumyR)) and they are not the same

cs50 pset4 filter edge detection

I made a new post for the major changes in my post, but the problem now is that my image is returned as the same image entered. I believe it's a problem with the newimage variable and swapping it with the old image variable but I don't know why.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
RGBTRIPLE newimage[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int redx = 0;
int greenx = 0;
int bluex = 0;
int redy = 0;
int greeny = 0;
int bluey = 0;
for (int k = i - 1; k <= i + 1 && k < height; k++)
{
for (int m = j - 1; m <= j + 1 && m < width; m++)
{
if (k != -1 && m != -1)
{
redx += image[k][m].rgbtRed * gx[k-(i-1)][m-(j-1)];
greenx += image[k][m].rgbtGreen * gx[k-(i-1)][m-(j-1)];
bluex += image[k][m].rgbtBlue * gx[k-(i-1)][m-(j-1)];
redy += image[k][m].rgbtRed * gy[k-(i-1)][m-(j-1)];
greeny += image[k][m].rgbtGreen * gy[k-(i-1)][m-(j-1)];
bluey += image[k][m].rgbtBlue * gy[k-(i-1)][m-(j-1)];
}
}
}
int finalred = round(sqrt((redx * redx) + (redy * redy)));
int finalgreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
int finalblue = round(sqrt((bluex * bluex) + (bluey * bluey)));
if (finalred > 255)
{
finalred = 255;
}
if (finalgreen > 255)
{
finalgreen = 255;
}
if (finalblue > 255)
{
finalblue = 255;
}
newimage[i][j].rgbtRed = finalred;
newimage[i][j].rgbtGreen = finalgreen;
newimage[i][j].rgbtBlue = finalblue;
}
}
image = newimage;
return;
}

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);
}

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:

Find intersection in 2d arrays

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");
}
}

Resources