Order Now  +1 678 648 4277 

Drawing a diamond, a Sierpinski triangle and searching HTML links using C assignment help

The assignment is divided into three parts and is completed by our C assignment helpers are explained:- The first part deals with printing a text diamond of a given height. The second part deals with printing a Sierpinski triangle using characters given a fractal level and a height. The last part deals with searching for all the href Wikipedia links in a given HTML page and printing the found title names.

Printing a Text diamond and Sierpinski Triangle

#include #include int main(int argc, char **argv) { int i; int j; int spaces; int stars; int height; if (argc != 2) { printf("ERROR: Wrong number of arguments. One required.\n"); return 1; } sscanf(argv[1], "%d", &height); /* convert argument to an integer */ if (height <=0 || height %2==0) { /* if it's negative or even */ printf("ERROR: Bad argument. Height must be positive odd integer.\n"); return 1; } /*print the diamond*/ spaces = height / 2; /* number of spaces to print */ stars = 1; /* number of start to print */ for (i = 0; i < height; i++) { for (j = 0; j < spaces; j++) /* print spaces */ printf(" "); for (j = 0; j < stars; j++) /* print stars*/ printf("*"); printf("\n"); /* jump to next line */ /* adjust number of spaces and stars for next line in diamond */ if (i < height/2) { spaces--; stars+=2; } else { spaces++; stars-=2; } } return 0; } #include #include #include #include /* returns 1 if the number n is a power of 2, 0 otherwise */ int is_power_of_2(int n) { int ones; ones = 0; while (n != 0) { if (n & 1) /* if the lowest bit is one, increment count */ ones++; n >>= 1; /* shift right to test next bit */ } return (ones == 1); /* if it has only one 1 bit, it's a power of 2*/ } /* generates the Sierpinski triangle of a given height and level at a given position in the buffer */ void gen_triangle(int level, int height, int posx, int posy, char **buf) { int i, j, m; if (level == 1) { /* if the level is 1, print a filled diamond */ for (i = 0; i <= height/2; i++) for (j = 0; j < height - i*2; j++) buf[posy - i][posx + i + j] = '*'; } else { /* if the level is not 1, recurse for 3 triangles */ m = height /2; gen_triangle(level - 1, m, posx + m - m / 2, posy - m/2 - 1, buf); /* upper triangle*/ gen_triangle(level - 1, m, posx, posy, buf); /* lower left triangle */ gen_triangle(level - 1, m, posx + m + 1, posy, buf); /* lower right triangle */ } } int main(int argc, char **argv) { int i, j, mid; int level; int height; int tri_height; char **buffer; if (argc != 3) { printf("ERROR: Wrong number of arguments. Two are required.\n"); return 1; } sscanf(argv[1], "%d", &height); /* convert first argument to an integer */ if (height <=0 || height %2==0) { /* if it's negative or even */ printf("ERROR: Bad argument. Height must be positive odd integer.\n"); return 1; } sscanf(argv[2], "%d", &level); /* convert second argument to an integer */ tri_height = (height + 1) / 2; if (!is_power_of_2(tri_height) || tri_height < (1 << (level - 1))) { printf("ERROR: Height does not allow evenly dividing requested number of levels.\n"); return 1; } mid = height / 2; /* create a buffer to save the diamond picture */ buffer = (char **) malloc((mid+1)*sizeof(char *)); for (i =0; i <= mid; i++) { /* fill with spaces */ buffer[i] = (char *) malloc(height + 1); for (j = 0; j < height; j++) buffer[i][j] = ' '; buffer[i][j] = 0; } /* generate the stierpinski triangles */ gen_triangle(level, height, 0, height/2, buffer); /* print the buffer that contains the generated triangles for the upper part*/ for (i = 0; i<= height/2; i++) printf("%s\n",buffer[i]); for (i = height/2 - 1; i>=0; i--) /* copy lower triangle */ printf("%s\n",buffer[i]); for (i = 0; i <= mid; i++) /* free the space used by the buffer */ free(buffer[i]); free(buffer); return 0; } #include #include #include int main(int argc, char **argv) { FILE *f; size_t size; char *buffer; char *ptr, *end, *endq, *tmp; char PageName[512]; if (argc != 2) { printf("ERROR: Wrong number of arguments. One required.\n"); return 1; } f = fopen(argv[1], "rt"); /* open file for reading */ if (f == NULL) { printf("ERROR: Unable to open file.\n"); return 1; } fseek(f, 0, SEEK_END); /* move to end of file */ size = ftell(f); /* get filesize */ fseek(f, 0, SEEK_SET); /* move to start of file */ buffer = (char *) malloc(size + 1); /* reserve space for all file */ fread(buffer, 1, size, f); /* load all the file in the buffer */ ptr = buffer; /* point to start of buffer */ while ((ptr = strstr(ptr, "'); /* find the closing */ end[0] = 0; /* replace closing by string terminator */ if ((tmp = strstr(ptr, "href=\"/wiki/")) != NULL) { /* see if it has the href pattern */ ptr = tmp + 12; /* advance to page name */ endq = strchr(ptr, '\"'); /* find the closing double quotes */ strncpy(PageName, ptr, endq - ptr); /* copy the page name to our variable */ PageName[endq - ptr] = 0; /* inset string terminator */ if ((tmp = strstr(ptr, "title=\"")) != NULL) { /* find the title */ ptr = tmp + 7; endq = strchr(ptr, '\"'); /* find closing quotes */ if (endq + 1 == end) { ptr = end + 1; /* move to char after closing > */ if((end = strstr(ptr, "")) != NULL) { /* find closing */ ptr = end + 4; printf("%s\n", PageName); } } } else ptr = end+1; } else /* if it's not out opening, advance a character after the closing */ ptr = end + 1; } free(buffer); fclose(f); return 0; }