This week lecture took me 3-4 times of watching again and again to understand the concept of pointers(basically file pointers).
The Practice Problems were also too much harder than the previous weeks.
Problem sets:
1) Volume = This problem was hard for me because of lack of understanding of file pointers. I watched file pointer video part again and again and understood what the functions actually do and what is the meaning of their arguments.
2) Filter-less = This problem was not that much hard but the subsection(Blur function) of the problem was very very time taking and hard. This function uses the strategy of box blur where we have to calculate the new pixel value from the average of its surrounding pixel's original value including itself.
-Here is the code which I had implemented to solve this problem with the help of cs50.ai
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
/*created temproray matrix for storing the new values beacause we have to calcluate the new
values from the original values in image*/
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++) // for iterating rows
{
for (int j = 0; j < width; j++) // for iterating columns
{
//*************************1) HANDLING THE CORNERS***************************
//----------1.1) for the TOP-LEFT pixel-----------
if (i == 0 && j == 0)
{
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = 0; r < 2; r++)
{
for (int c = 0; c < 2; c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 4.0);
temp[i][j].rgbtGreen = round(green / 4.0);
temp[i][j].rgbtBlue = round(blue / 4.0);
} //---------------------------------------------------
//----------1.2) for TOP-RIGHT pixel------------
if (i == 0 && j == (width - 1))
{
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = 0; r < 2; r++)
{
for (int c = (width - 2); c < width; c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 4.0);
temp[i][j].rgbtGreen = round(green / 4.0);
temp[i][j].rgbtBlue = round(blue / 4.0);
} //-----------------------------------------------------
//----------1.3) for BOTTOM-LEFT pixel------------
if (i == (height - 1) && j == 0)
{
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (height - 2); r < height; r++)
{
for (int c = 0; c < 2; c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 4.0);
temp[i][j].rgbtGreen = round(green / 4.0);
temp[i][j].rgbtBlue = round(blue / 4.0);
} //------------------------------------------------------
//----------1.4) for BOTTOM-RIGHT pixel------------
if (i == (height - 1) && j == (width - 1))
{
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (height - 2); r < height; r++)
{
for (int c = (width - 2); c < width; c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 4.0);
temp[i][j].rgbtGreen = round(green / 4.0);
temp[i][j].rgbtBlue = round(blue / 4.0);
} //------------------------------------------------------
//*******************************************************************************
//*****************************2)HANDLING THE EDGES************************************
//----------2.1)HANDLING THE TOP ROW EDGES ---------------
if (i == 0 && j != 0 && j != (width - 1))
{
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = i; r < 2; r++)
{
for (int c = (j - 1); c <= (j + 1); c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 6.0);
temp[i][j].rgbtGreen = round(green / 6.0);
temp[i][j].rgbtBlue = round(blue / 6.0);
}
//----------------------------------------------------
//----------2.2)HANDLING THE BOTTOM ROW EDGES ---------------
if (i == (height - 1) && j != 0 && j != (width - 1))
{
// ex i[4]j[2]
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (i - 1); r < height; r++)
{
for (int c = (j - 1); c <= (j + 1); c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 6.0);
temp[i][j].rgbtGreen = round(green / 6.0);
temp[i][j].rgbtBlue = round(blue / 6.0);
}
//----------------------------------------------------
//----------2.3)HANDLING THE LEFT COLUMN EDGES ---------------
if (i != 0 && i != (height - 1) && j == 0)
{
// ex i[2]j[0]
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (i - 1); r <= (i + 1); r++)
{
for (int c = 0; c <= (j + 1); c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 6.0);
temp[i][j].rgbtGreen = round(green / 6.0);
temp[i][j].rgbtBlue = round(blue / 6.0);
}
//----------------------------------------------------
//----------2.4)HANDLING THE RIGHT COLUMN EDGES ---------------
if (i != 0 && i != (height - 1) && j == (width - 1))
{
// ex i[2]j[3]
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (i - 1); r <= (i + 1); r++)
{
for (int c = (j - 1); c < width; c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 6.0);
temp[i][j].rgbtGreen = round(green / 6.0);
temp[i][j].rgbtBlue = round(blue / 6.0);
}
//----------------------------------------------------
//*****************HANDLING THE INNER PART************************
if (i != 0 && i != (height - 1) && j != 0 && j != (width - 1))
{
// ex i[3]j[2]
float red = 0.0, green = 0.0, blue = 0.0;
for (int r = (i - 1); r <= (i + 1); r++)
{
for (int c = (j - 1); c <= (j + 1); c++)
{
red = red + image[r][c].rgbtRed;
green = green + image[r][c].rgbtGreen;
blue = blue + image[r][c].rgbtBlue;
}
}
temp[i][j].rgbtRed = round(red / 9.0);
temp[i][j].rgbtGreen = round(green / 9.0);
temp[i][j].rgbtBlue = round(blue / 9.0);
}
//*****************************************************************
}
}
// copying the temp[height][width] to image[height][width]
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = temp[i][j].rgbtRed;
image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
}
}
return;
}
3)Filter-more = Skipped :-)
4)Recover = In this problem I have recovered the jpg images from the card.raw which is a file format used by digital cameras to store back to back images. This uses fat file system in which one block is of 512 bytes and so on. Overall this problem was very interesting and very little hard.
Lastly the one thing that i learned till now is to write a clear pseudo code, understand the problem and then code it.
Top comments (0)