Avatar of vlzware

vlzware's solution

to Pascal's Triangle in the C Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
# ... etc

Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the test task.

make test

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

Source

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_pascals_triangle.c

#include <stdbool.h>
#include "../src/pascals_triangle.h"
#include "vendor/unity.h"

void setUp(void)
{
}

void tearDown(void)
{
}

static bool check(size_t count, size_t expected[][count], size_t ** result)
{
   size_t i, j;
   for (i = 0; i < count; i++) {
      for (j = 0; j < count; j++) {
         if (expected[i][j] != result[i][j]) {
            return 0;
         }
      }
   }
   return 1;
}

void test_no_rows(void)
{
   size_t expected[1][1] = { {0} };
   size_t **r = create_triangle(0);
   TEST_ASSERT_TRUE(check(1, expected, r));
   free_triangle(r, 1);
}

void test_single_row(void)
{
   TEST_IGNORE();               // delete this line to run test
   size_t expected[1][1] = {
      {1}
   };
   size_t **r = create_triangle(1);
   TEST_ASSERT_TRUE(check(1, expected, r));
   free_triangle(r, 1);
}

void test_two_rows(void)
{
   TEST_IGNORE();
   size_t expected[2][2] = {
      {1, 0},
      {1, 1}
   };
   size_t **r = create_triangle(2);
   TEST_ASSERT_TRUE(check(2, expected, r));
   free_triangle(r, 2);
}

void test_three_rows(void)
{
   TEST_IGNORE();
   size_t expected[3][3] = {
      {1, 0, 0},
      {1, 1, 0},
      {1, 2, 1}
   };
   size_t **r = create_triangle(3);
   TEST_ASSERT_TRUE(check(3, expected, r));
   free_triangle(r, 3);
}

void test_four_rows(void)
{
   TEST_IGNORE();
   size_t expected[4][4] = {
      {1, 0, 0, 0},
      {1, 1, 0, 0},
      {1, 2, 1, 0},
      {1, 3, 3, 1}
   };
   size_t **r = create_triangle(4);
   TEST_ASSERT_TRUE(check(4, expected, r));
   free_triangle(r, 4);
}

void test_negative_rows(void)
{
   TEST_IGNORE();
   TEST_ASSERT_TRUE((create_triangle(-1) == NULL));
}

int main(void)
{
   UnityBegin("test/test_pascals_triangle.c");
   RUN_TEST(test_no_rows);
   RUN_TEST(test_single_row);
   RUN_TEST(test_two_rows);
   RUN_TEST(test_three_rows);
   RUN_TEST(test_four_rows);
   RUN_TEST(test_negative_rows);
   UnityEnd();
}

src/pascals_triangle.c

#include "pascals_triangle.h"
#include <stdlib.h>

void check_alloc(void *p);

size_t **create_triangle(int rows)
{
	if (rows < 0)
		return NULL;

	size_t **triangle =
		(size_t **) malloc(sizeof(size_t*) * (rows? rows : 1));
	check_alloc(triangle);

	if (rows == 0) {
		triangle[0] = (size_t *) malloc(sizeof(size_t));
		check_alloc(triangle[0]);
		triangle[0][0] = 0;
		return triangle;
	}

	int i, j, cols = rows;
	for (i = 0; i < rows; i++) {

		triangle[i] = (size_t *) malloc(sizeof(size_t) * cols);
		check_alloc(triangle[i]);

		triangle[i][0] = 1;

		for (j = 1; j < cols; j++)
			triangle[i][j] = (j > i)
				? 0
				: (triangle[i - 1][j] +
					triangle[i - 1][j - 1]);
	}

	return triangle;
}

void free_triangle(size_t **triangle, int size)
{
	if (size <= 0)
		return;

	while(size--)
		free(triangle[size]);

	free(triangle);
}

#include <stdio.h>
void check_alloc(void *p)
{
	if (p == NULL) {
		fprintf(stderr, "Memory error.\n");
		exit(1);
	}
}

src/pascals_triangle.h

#ifndef PASCALS_TRIANGLE_H
#define PASCALS_TRIANGLE_H
#include <stddef.h>

size_t **create_triangle(int rows);
void free_triangle(size_t **triangle, int size);

#endif

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of vlzware

Using the formula was just plain stupid. Realized this after seeing the first solution without it (@acornq's)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?