Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:05:37+00:00 2026-06-13T08:05:37+00:00

I am mostly a Python/MIT Scheme programmer, but I want to learn C because

  • 0

I am mostly a Python/MIT Scheme programmer, but I want to learn C because I will be taking a class on it next semester, and I want to be ready. I would say I am pretty good with python and scheme, so I’m not a newbie to programming in general. It’s a hobbie of mine 🙂 .

So I tried writing a C program to compute pascals triangle, but it is giving me an incorrect output. I compile with no errors, here is my code:

/*
  This code is just to see exactly how much faster C is than python                                                                          
  at doing numerically stuff. It should calculate the nth row of                                                                             
  pascals triangle.                                                                                                                          
  The way you use it is by calling the executable with the row                                                                               
  you wish to compute as the only argument. For example if you                                                                               
  are on an *nix system:                                                                                                                     
  ./pascal 6                                                                                                                                 

  Or in a windows command prompt:                                                                                                            
  pascal.exe 6                                                                                                                               
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

/*
  I decided to use a struct here because I thought it was ugly                                                                               
  to have to pass around the length of my arrays in function                                                                                 
  calls.                                                                                             
*/
typedef struct{
  int length;
  unsigned long int* vals;
} CoolArray;

//initArray allocates memory for the CoolArray.                                                                                              
void initArray(CoolArray* array, int length);

//destroyArray frees allocated memory in the struct.                                                                                         
void destroyArray(CoolArray* array);

//printArray prints the contents of the array.                                                                                               
void printArray(CoolArray* array);

//pascal computes the nth row of pascal's                                                                                                    
//triangle, with n being array->length,                                                                                                      
//and stores the values in the array.                                                                                                        
void pascal(CoolArray* array);

//setArray takes two CoolArrays of the same length                                                                                           
//and sets the values in the first to the values                                                                                             
//in the second.                                                                                                                             
void setArray(CoolArray* array1, CoolArray* array2);

int main(int argc, char** argv){
  int length = atoi(argv[1]);
  CoolArray array1;
  initArray(&array1, length);
  printf("Calculating the %dth row of pascals triangle...\n", length);
  pascal(&array1);
  printArray(&array1);
  destroyArray(&array1);
  return 0;
}

void initArray(CoolArray* array, int length){
  assert(length>=1); //don't make arrays with a length <=0!!!                                                                                
  array->length = length;
  array->vals = (unsigned long int*) calloc(length, sizeof(unsigned long int));
  return;
}

void destroyArray(CoolArray* array){
  free(array->vals);
  return;
}

void pascal(CoolArray* array){
  assert(array->length >= 1);//making sure the length wasn't fiddled with...                                                                 
  if(array->length == 1){
    array->vals[0] = 1;
    return;
  }
  int row;
  int index;
  array->vals[0]=1; //if we aren't looking for the first row                                                                                 
  array->vals[1]=1;//then i'll start with the second row                                                                                     
  CoolArray current;
  initArray(&current, array->length);
  for(row = 2; row < array->length; ++row){
    current.vals[0]=1;
    for(index = 1; index < row; ++index){
      current.vals[index]=array->vals[index]+array->vals[index-1];
    }
    current.vals[row]=1;
    printArray(&current);
    setArray(array, &current);
  }
  destroyArray(&current);
  return;
}

void setArray(CoolArray* array1, CoolArray* array2){
  assert(array1->length==array2->length);//making sure they are the same length                                                              
  int i=0;
  for(; i < array2->length; ++i){
    array1->vals[i]=array2->vals[i];
  }
  return;
}

void printArray(CoolArray* array){
  int i=0;
  printf("[");
  for(; i < array->length; ++i){
    if (i = array->length - 1) printf("%d]\n", array->vals[i]);
    else printf("%d, ", array->vals[i]);
  }
  return;
}

Can anyone see where I’m going wrong?
it’s output looks like this right now:

./pascal 6

Calculating the 6th row of pascals triangle…

[1]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:05:38+00:00Added an answer on June 13, 2026 at 8:05 am

    Your error is on this line:

    if(array->length = 1){
    

    You are assigning 1 to array->length because of the single equals sign. Use == for equality testing, and turn on warnings in your compiler (-Wall).

    (You have a similar error on the line if (i = array->length - 1) which results in the incorrectly-formatted output you see).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to design a game server in python. The game will mostly just
The title says it mostly. If I have an object in Python and want
So my current project is mostly in Python, but I'm looking to rewrite the
I have mostly programmed in Python, but I am now learning the statistical programming
I am using Python (SimPy package mostly, but it is irrelevant to the question
I mostly develop in PHP, but I'm using Python and Ruby more and more.
I am new to Python and mostly used my own code. But so now
I mostly switched to python but sometimes I still have to work with old
I have some python modules containing mostly functions and a few classes. Each one
I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.