Pascal's Triangle: Pointers and Dynamic Memory Allocation

In summary, In order to print the first 12 rows of the famous Pascal triangle, an int **triangle; is created, which is referred to exclusively via pointers. Each row is then accessed by referencing the pointer stored at triangle[i][i], and the function prints the row by padding it with the appropriate amount of white space on both sides. Finally, the row pointers are freed.
  • #1
TheSourceCode
14
0
Here is the given problem. I have a question for part c and e.

Calculate and print first 12 rows of the famous Pascal triangle, as shown below. Each number is the sum of the two numbers immediately above it. As our intent is to practice pointers, functions, loops, and dynamic memory allocation, the following steps are mandatory (even if you could find an alternative solution):
a. Each row of the triangle is to be referred to via a pointer; thus an array of 12 pointers is to be declared.
b. Cycle over all rows. Use malloc to allocate the necessary amount of memory for each row pointer.
c. Calculate the numbers in the row by summing the respective values from the previous row. All values are to be referred to exclusively via pointers.
d. Write a function that would print a given row in a symmetric fashion as shown below, by
padding the row with the appropriate amount of white space on both sides.
e. In the end, be sure to free all row pointers.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 12
int main(){
 int **triangle;
 int i, j; 

triangle=malloc(MAX * sizeof(int *));

 for (i=0; i<MAX; i++){   
        triangle[i]=malloc((i+1) * sizeof(int));
        triangle[i][0]=1;
        triangle[i][i]=1;
        for(j=1; j<i; j++)
            triangle[i][j]=triangle[i-1][j-1]+triangle[i-1][j];
    }
            
//function to format will go here
    
 free(triangle);
 
  printf("Press enter to continue ...");
  getchar();	
  return 0;
}
My code works fine but I'm not sure I'm referring to them with pointers (part c). I know arary is the same as *(array+i). Should I be using that notation and if so how do I do this since i have an array of pointers?

My second question is am I using free right? I feel like I should have to free each array but my code doesn't work when I try. Right now this compiles:
Code:
free(array);
I feel like I should be doing this (but it freezes):
Code:
for(i = 0; i < MAX; i++)
	free(triangle[i]);
free(triangle);
Thanks for the feedback!
 
Last edited:
Physics news on Phys.org
  • #2
TheSourceCode said:
My code works fine but I'm not sure
You got lucky. You're indexing an array out of bounds, which can cause any sort of random behavior to happen. Can you see where you're doing it?
 
  • #3
Code:
triangle[i][i+1]=1;
It's this line isn't it? Thanks, I'll fix that now.
EDIT:
I did it again here.
Code:
for(j=1; j<i+1; j++)
Should be corrected in the OP now.
 
Last edited:

1. What is the difference between an array and a pointer?

An array is a collection of elements of the same data type that are stored in contiguous memory locations. A pointer is a variable that stores the memory address of another variable. Arrays can be accessed using indexing, while pointers can be dereferenced to access the value stored at the memory address they point to.

2. What is the purpose of using malloc in C?

Malloc is used to dynamically allocate memory in C. It allows the program to request a specific amount of memory at runtime, rather than declaring a fixed amount of memory at compile time. This is useful when the size of the data needed is not known beforehand or may change during the execution of the program.

3. How do you allocate memory for an array using malloc?

To allocate memory for an array using malloc, you first need to declare a pointer of the appropriate data type. Then, you can use the malloc function to allocate memory for the array, specifying the number of elements and the size of each element. The returned pointer can then be used to access the allocated memory space.

4. How is memory allocated for a multi-dimensional array using pointers?

In C, multi-dimensional arrays are stored in contiguous memory locations, so you can use a pointer to allocate memory for them. For example, a 2D array with m rows and n columns can be allocated by first allocating memory for an array of m pointers, and then allocating memory for each row using a loop, and assigning the address of each row to the corresponding pointer in the first array.

5. How do you free the memory allocated using malloc?

To free the memory allocated using malloc, you can use the free function, passing in the pointer to the allocated memory as an argument. This will release the allocated memory and make it available for other parts of the program to use. It is important to free allocated memory to avoid memory leaks and ensure efficient memory usage.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
758
  • Engineering and Comp Sci Homework Help
Replies
3
Views
673
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Back
Top