This assignment is about formatted output, and more string manipulation. Download the two attached source files, pascal.

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

This assignment is about formatted output, and more string manipulation. Download the two attached source files, pascal.

Post by answerhappygod »

This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 1
This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 1 (39.28 KiB) Viewed 45 times
------------------------------------------------------------------
safe_string
This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 2
This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 2 (43.18 KiB) Viewed 45 times
------------------------------------------------------------------
pascal.c
This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 3
This Assignment Is About Formatted Output And More String Manipulation Download The Two Attached Source Files Pascal 3 (101.68 KiB) Viewed 45 times
This assignment is about formatted output, and more string manipulation. Download the two attached source files, pascal.c and safe_string.c, and upload them to your AWS machine. Compile and run pascal.c. It should display the first 12 lines of Pascal's triangle as: 1 11 121 1331 14641 1 5 10 10 51 1 6 15 20 15 6 1 1721 35 35 21 71 1 8 28 56 70 56 28 8 1 19 36 84 126 126 84 3691 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 111 The assignment is to reformat the output by adding code to the function fmt Triangle() using hints in the code comments and a general idea of how the other functions in the program operate. The reformatted output should look like: 1 11 121 133 1 14641 1 5 10 10 51 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 1 11 55 165 330 462 462 330 165 55 11 1 The two functions assignString and appendString() (in safe_string.c) were presented in a earlier lecture as function to simplify string manipulation in a safe way. You can include these functions in the pascal.c source if you want, but it is not a requirement. When your code is complete, post the modified pascal.c only to the assignment. The difficulty comes from the need to format and store the output until the last line. Only after the last line is formatted will you know how much white space is needed to precede each line when it is output.

6 8 9 C safe_string) No Selection 1 #include <stdio.h> 2 #include <string.h> 3 #include <ctype.h> 4 #include <stdlib.h> 5 char *assignString(char **lft, char *rgt) { if (0 != *lft) 7 free(*lft); *lft = malloc(strlen(rgt) + 1); strcpy(*lft, rgt); return *lft; 11} 12 char *appendString(char **lft, char *rgt) { char *nw; int In = strlen(*lft); 14 nw = malloc(in + strlen(rgt) + 1); 15 strcpy(nw, *lft); strcpy(nw + in, rgt); 17 free(*lft); 18 *lft nw; 19 return *lft; 20 } 21 10 13 16

8 14 16 19 20 C pascal No Selection 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include "safe_string.h" 4 #define LINES 12 5 static char err1[] = "memory alloction error %s requesting %ld bytes\n"; 6 void allocErr(char *who, long siz) { 7 fprintf(stderr, erri, who, siz); exit(1); 9} 10 int *arrayCopy(int *ar, int sz) { 11 int *cp; 12 int ix; 13 long msz; msz = (sz + 1) * sizeof(int); 15 if (0 == (cp = (int *)malloc(msz))) allocErr("arrayCopy", msz); 17 for (ix = 0; ix < sz; ++ix) 18 { cp[ix] = ar[ix]; } 21 cp[sz) = -1; 22 return cp; 23 } 24 int **newPascal(int lines) { int *array: int **triangle; 27 int ix, jx; long msz; 29 msz = lines * sizeof(int); 30 if (0 == (array = (int *) malloc(msz))) 31 allocErr("newPascall", msz); 32 msz = (lines + 1) * sizeof(int *); 33 if (0 == (triangle = (int **)malloc(msz))) 34 allocErr("newPascal2", msz); for (ix = 0; ix < lines; ++ix) { array[ix] = 1; 38 for (jx ix - 1; jx >= 1; --jx) 39 { 40 array[jx] += array[jx 1]; 41 } triangle[ix] = arrayCopy(array, ix + 1); 43 } triangle[lines] = 0; return triangle; 46 } 25 26 28 35 36 37 = 42 44 45

pascal No Selection 46 } 47 void delPascal(int **tri) { 48 int ix; 49 ix = 0; 50 while (!= tri[ix]) 51 { 52 free(tri[ix]); 53 ix += 1; 54 } 55 free(tri); 56 } 57 void rawTriangle(int **tri) { 58 int ix, jx; 59 ix = 0; 60 while (0 != tri[ix]) 61 { 62 jx = 0; 63 while (tri[ix][jx] != -1) 64 { 65 printf("%d", tri[ix][jx]); 66 jx += 1; 67 } 68 printf("\n"); 69 ix += 1; 70 } 71 } 72 /** 73 * Displays a formatted Pascal's Triangle from the two-dimensional array tri. int ** is the same type 74 * as int 00, but with int **, it is not necessary to explicitly define the size of the arrays. 75 * In Pascal's Triangle, each element triſt points to a int of variable size. The last element in 76 * the array is -1, a sentinel indicating end-of-array. The last element, tri[last] is indicated with a 77 * null pointer 78 */ 79 void fmtTriangle(int **tri) { 80 int ix, jx, sz, mx; char **arr; 82 char buf[12]; /* to be used for sprintf(), a buffer large enough to contain formatted 11-digit integers */ 83 sz = 0; 84 /* calculate the size in the input array tri - see rawTriangle() */ 85 /* allocate arr, an array of char * to store the formatted strings */ 86 /* because arr is a pointer to an array of char *, it is a char ** */ /* use a variable to keep track of the maximum length formatted line */ 88 /* use an index variable in a while loop to index each line. */ /* while loop termination is 0 == tri[ix] */ 90 /* in the while loop */ 2乃恐西形刀 81 87 89

90 94 95 96 97 98 c pascal) No Selection /* in the while loop */ 91 /* use sprintf() to format the first element of the current array */ 92 /* assign the result to the array of saved formatted strings */ 93 /* in an inner while loop, format each succeeding triangle value with sprintf() */ /* append the formatted result to the saved formatted string */ /* the end of the inner loop is indicated by an array element of -1 */ /* when the inner loop finishes, update the maximum formatted string length */ /* incrementing the outer loop index goes to the next line in the triangle */ /* print the formatted strings - the key to printing the correct amount of indentation */ 99 /* is to print an empty string with a varible length field with specification */ /* before printing the formatted string - the length with depend upon the difference between */ 101 /* the maximum string length and the current string length */ /* after the strings are printed, free all the dynamically allocated storage */ 103 104 } 105 int main(int argc, char *argv[]) { 106 int **triangle; 107 triangle = newPascal(LINES); rawTriangle(triangle); 109 fmtTriangle(triangle); 110 delPascal(triangle); return 0; 112 } 100 102 108 111 113
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply