Instructions
Requirements and Specifications
Source Code
/* PLEASE DO NOT MODIFY A SINGLE STATEMENT IN THE TEXT BELOW.
READ THE FOLLOWING CAREFULLY AND FILL IN THE GAPS
I hereby declare that all the work that was required to solve the following problem including designing the algorithms and writing the code below, is solely my own and that I received no help in creating this solution and I have not discussed my solution with anybody. I affirm that I have read and understood the Senate Policy on Academic honesty at https://www.yorku.ca/secretariat/policies/policies/academic-honesty-senate-policy-on/ and I am well aware of the seriousness of the matter and the penalties that I will face as a result of committing plagiarism in this assignment.
BY FILLING THE GAPS,YOU ARE SIGNING THE ABOVE STATEMENTS.
Full Name:
Student Number:
Course Section:
*/
#include
#include
#define IMAGE_SIZE 10
// this function prints the array
void printImgArray(int array[IMAGE_SIZE][IMAGE_SIZE])
{
printf("------ Image Contents -------\n");
for (int i=0; i
for (int j=0; j
printf("%02d, ",array[i][j]);
printf("\n");
}
printf("-----------------------------\n");
}
void cellCountStep(int r, int c, int image[IMAGE_SIZE][IMAGE_SIZE], int visited[IMAGE_SIZE][IMAGE_SIZE], int color) {
visited[r][c] = 1;
image[r][c] = color;
if (r > 0 && c > 0) {
if (image[r-1][c-1] > 0 && visited[r-1][c-1] == 0) {
cellCountStep(r-1, c-1, image, visited, color);
}
}
if (r > 0) {
if (image[r-1][c] > 0 && visited[r-1][c] == 0) {
cellCountStep(r-1, c, image, visited, color);
}
}
if (r > 0 && c < IMAGE_SIZE - 1) {
if (image[r-1][c+1] > 0 && visited[r-1][c+1] == 0) {
cellCountStep(r-1, c+1, image, visited, color);
}
}
if (c > 0) {
if (image[r][c-1] > 0 && visited[r][c-1] == 0) {
cellCountStep(r, c-1, image, visited, color);
}
}
if (c < IMAGE_SIZE - 1) {
if (image[r][c+1] > 0 && visited[r][c+1] == 0) {
cellCountStep(r, c+1, image, visited, color);
}
}
if (r < IMAGE_SIZE - 1 && c > 0) {
if (image[r+1][c-1] > 0 && visited[r+1][c-1] == 0) {
cellCountStep(r+1, c-1, image, visited, color);
}
}
if (r < IMAGE_SIZE - 1) {
if (image[r+1][c] > 0 && visited[r+1][c] == 0) {
cellCountStep(r+1, c, image, visited, color);
}
}
if (r < IMAGE_SIZE - 1 && c < IMAGE_SIZE - 1) {
if (image[r+1][c+1] > 0 && visited[r+1][c+1] == 0) {
cellCountStep(r+1, c+1, image, visited, color);
}
}
}
/**
* This function counts the number of distinct
* number (i.e. the number of cells)
**/
int cellCount(int image[IMAGE_SIZE][IMAGE_SIZE]) {
int visited[IMAGE_SIZE][IMAGE_SIZE];
int i, j;
for (i = 0; i
for (j = 0; j
visited[i][j] = 0;
}
}
int counter = 0;
for (i = 0; i
for (j = 0; j
if (image[i][j] > 0 && visited[i][j] == 0) {
cellCountStep(i, j, image, visited, 1);
counter++;
}
}
}
return counter;
}
/**
* This function colors each cell with a unique color
* (i.e. unique number)
**/
void color(int image[IMAGE_SIZE][IMAGE_SIZE]){
// insert your code for task 1.2 here
int visited[IMAGE_SIZE][IMAGE_SIZE];
int i, j;
for (i = 0; i
for (j = 0; j
visited[i][j] = 0;
}
}
int counter = 0;
for (i = 0; i
for (j = 0; j
if (image[i][j] == 1 && visited[i][j] == 0) {
cellCountStep(i, j, image, visited, counter+1);
counter++;
}
}
}
}
/**
* This function colors each cell with a unique color
* (i.e., unique number). This function works with
* pointers
* currentRow: shows the current row that is processed
* currentCol: shows the current column that is process
* currentIndex: show the index that is processed
* color: is an integer that represents a color
**/
int colorRecursively(int image[IMAGE_SIZE][IMAGE_SIZE], int currentRow, int currentCol, int currentIndex, int color) {
// insert your code for task 2.1 here, in case you decided to complete this task
// you may want to change the return value
if (image[currentRow][currentCol] == 1) {
int newColor = color;
if (color == 0) {
currentIndex++;
newColor = currentIndex;
}
image[currentRow][currentCol] = color;
if (currentRow > 0 && currentCol > 0) {
colorRecursively(image, currentRow-1, currentCol-1, currentIndex, newColor);
}
if (currentRow > 0) {
colorRecursively(image, currentRow-1, currentCol, currentIndex, newColor);
}
if (currentRow > 0 && currentCol < IMAGE_SIZE - 1) {
colorRecursively(image, currentRow-1, currentCol+1, currentIndex, newColor);
}
if (currentCol > 0) {
colorRecursively(image, currentRow, currentCol-1, currentIndex, newColor);
}
if (currentCol < IMAGE_SIZE - 1) {
colorRecursively(image, currentRow, currentCol+1, currentIndex, newColor);
}
if (currentRow < IMAGE_SIZE - 1 && currentCol > 0) {
colorRecursively(image, currentRow+1, currentCol-1, currentIndex, newColor);
}
if (currentRow < IMAGE_SIZE - 1) {
colorRecursively(image, currentRow+1, currentCol, currentIndex, newColor);
}
if (currentRow < IMAGE_SIZE - 1 && currentCol < IMAGE_SIZE - 1) {
colorRecursively(image, currentRow+1, currentCol+1, currentIndex, newColor);
}
}
if (color == 0) {
if (currentRow == IMAGE_SIZE-1 && currentCol == IMAGE_SIZE-1 ) {
return 0;
}
else {
return colorRecursively(image, currentRow + (currentCol + 1) / IMAGE_SIZE, (currentCol + 1) % IMAGE_SIZE, currentIndex, 0);
}
}
else {
return 0;
}
}
void colorPtr(int* image){
// insert your code for task 2.2 here
}
#ifndef __testing
int main(){
// DO not change anything in main(), it will confuse the
// auto-checker.
puts("testing the code with color() function");
int count = 0;
int cellImg[IMAGE_SIZE][IMAGE_SIZE]={{0,0,1,1,0,0,1,0,0,1},\
{1,0,1,1,0,0,1,1,0,1},\
{1,0,0,1,1,0,1,1,0,1},\
{1,1,0,0,0,0,0,0,0,0},\
{1,0,0,1,1,1,0,0,1,0},\
{0,0,0,0,1,0,0,1,1,0},\
{0,0,1,0,0,1,0,1,0,0},\
{0,0,1,1,0,0,1,0,0,0},\
{0,0,1,0,0,0,0,0,1,1},
{0,1,1,0,0,0,1,1,1,1}};
printImgArray(cellImg);
color(cellImg);
printImgArray(cellImg);
count=cellCount(cellImg);
printf("Total number of cells in the image: %d\n",count);
puts("Testing the code with colorPtr");
int cellImg_[IMAGE_SIZE][IMAGE_SIZE]={{0,0,1,1,0,0,1,0,0,1},\
{1,0,1,1,0,0,1,1,0,1},\
{1,0,0,1,1,0,1,1,0,1},\
{1,1,0,0,0,0,0,0,0,0},\
{1,0,0,1,1,1,0,0,1,0},\
{0,0,0,0,1,0,0,1,1,0},\
{0,0,1,0,0,1,0,1,0,0},\
{0,0,1,1,0,0,1,0,0,0},\
{0,0,1,0,0,0,0,0,1,1},
{0,1,1,0,0,0,1,1,1,1}};
int** ptr = cellImg_;
printImgArray(ptr);
colorPtr(ptr);
printImgArray(ptr);
count=cellCount(ptr);
printf("Total number of cells in the image: %d\n",count);
puts("Testing the code with colorRecursively");
int cellImg__[IMAGE_SIZE][IMAGE_SIZE]={{0,0,1,1,0,0,1,0,0,1},\
{1,0,1,1,0,0,1,1,0,1},\
{1,0,0,1,1,0,1,1,0,1},\
{1,1,0,0,0,0,0,0,0,0},\
{1,0,0,1,1,1,0,0,1,0},\
{0,0,0,0,1,0,0,1,1,0},\
{0,0,1,0,0,1,0,1,0,0},\
{0,0,1,1,0,0,1,0,0,0},\
{0,0,1,0,0,0,0,0,1,1},
{0,1,1,0,0,0,1,1,1,1}};
printImgArray(cellImg__);
colorRecursively(cellImg__, 0, 0, 1, 0);
printImgArray(cellImg__);
count=cellCount(cellImg__);
printf("Total number of cells in the image: %d\n",count);
return 0;
}
#endif