Pascal's Triangle: Pointers and Dynamic Memory Allocation

  • Thread starter Thread starter TheSourceCode
  • Start date Start date
  • Tags Tags
    Array Pointers
Click For Summary
SUMMARY

The discussion focuses on implementing Pascal's Triangle using pointers and dynamic memory allocation in C. The user successfully allocates memory for 12 rows using malloc and calculates the triangle values, but seeks clarification on pointer usage and memory deallocation. Key issues identified include out-of-bounds indexing in the triangle array, specifically in the lines triangle[i][i+1]=1 and for(j=1; j. The correct approach to free allocated memory involves freeing each row before freeing the main pointer.

PREREQUISITES
  • C programming language fundamentals
  • Understanding of pointers and dynamic memory allocation
  • Familiarity with the malloc and free functions
  • Basic knowledge of loops and array indexing
NEXT STEPS
  • Review C pointer arithmetic and its application in dynamic arrays
  • Learn about memory management best practices in C
  • Explore error handling techniques for dynamic memory allocation
  • Study formatting output in C for better presentation of data
USEFUL FOR

C programmers, computer science students, and developers interested in understanding dynamic memory management and pointer usage in algorithms.

TheSourceCode
Messages
14
Reaction score
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
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?
 
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:

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K