Question
Hi! I have a question regarding my homework in programming with C. I am asked to implement the pascal triangle in a given format by
Hi!
I have a question regarding my homework in programming with C. I am asked to implement the pascal triangle in a given format by implementing the logic in the given code (see below).
The functions should do this:
struct pascals_triangle: The structure struct pascals_triangle is used to map a Pascal triangle of a given number of rows so that the entries of the Pascal triangle can be stored in the dynamically allocated, non-rectangular, two-dimensional array values of the structure struct pascals_triangle. The pointer values points to a memory which contains pointers for every single row of the Pascal triangle. Each pointer of a row points to a memory area which can store exactly the number of values of this row.
init_pascal_traingle: Reserves memory for the structure struct pascals_triangle and for the values of the Pascal's triangle according to the number of rows passed. Then, the rows element of the structure is set to the passed value and the values of the Pascal's triangle are calculated.
get_element: Returns the value of the entry in the row and column of the two-dimensional array values, i.e. of the Pascal's triangle. If the value for row or column exceeds the limits, the value 0 is returned.
free_pascal_triangle: Frees the reserved memory for the struct pascals_triangle and thus the entries of the Pascal's triangle.
print_pascal_traingle: Outputs the entries of the Pascal's triangle on the standard output. The format of the output is not specified.
main: Complete the main function using the previously implemented functions so that the program allocates memory for a Pascal triangle with row count rows, calculates the entries, outputs them to standard output, and then frees the memory for the Pascal triangle. The number of rows is passed as a command line parameter.
The code that is given is the following:
#include
struct pascals_triangle { unsigned rows; unsigned **values; };
struct pascals_triangle *init_pascals_triangle(unsigned rows) { /* TODO: Implement function logic */ }
unsigned get_element(const struct pascals_triangle *triangle, unsigned row, unsigned column) { /* TODO: Implement function logic */ }
void free_pascals_triangle(struct pascals_triangle *triangle) { /* TODO: Implement function logic */ }
void print_pascals_triangle(const struct pascals_triangle *triangle) { /* TODO: Implement function logic */ }
int main(int argc, char **argv) { /* TODO: Implement function logic */ return EXIT_SUCCESS; }
I started doing this but I am stuck at creating the two-dimensional array and storing the numbers of the Pascal triangle in there. I use a nested for-loop but I do not know how to store the values in the array correctly. In addition, the reservation of memory for the struct does not work for me.
Is it possible for you to give me help how to tackle this problem and how to implement all the logic?
Thank you very much in advance!
Best regards
Fiona
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started