Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的帕斯卡三角形_C - Fatal编程技术网

C中的帕斯卡三角形

C中的帕斯卡三角形,c,C,我主要是一名Python/MIT Scheme程序员,但我想学习C,因为我下学期要上一门C课程,我想做好准备。我会说我非常擅长python和scheme,所以我不是一般编程新手。这是我的一个霍比:) 所以我试着写一个C程序来计算帕斯卡三角形,但它给了我一个错误的输出。我编译时没有错误,下面是我的代码: /* This code is just to see exactly how much faster C is than python

我主要是一名Python/MIT Scheme程序员,但我想学习C,因为我下学期要上一门C课程,我想做好准备。我会说我非常擅长python和scheme,所以我不是一般编程新手。这是我的一个霍比:)

所以我试着写一个C程序来计算帕斯卡三角形,但它给了我一个错误的输出。我编译时没有错误,下面是我的代码:

/*
  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;
}
/*
这段代码只是想看看C比python快多少
在做一些数字上的事情。它应该计算第n行的
帕斯卡三角形。
您使用它的方式是通过使用行调用可执行文件
您希望计算作为唯一的参数。例如,如果你
在*nix系统上:
/帕斯卡6
或在windows命令提示符中:
pascal.exe 6
*/
#包括
#包括
#包括
/*
我决定在这里使用struct,因为我觉得它很难看
必须在函数中传递数组的长度
电话。
*/
类型定义结构{
整数长度;
无符号长整数*VAL;
}冷却阵列;
//initArray为CoolArray分配内存。
void initArray(CoolArray*array,int-length);
//destroyArray释放结构中分配的内存。
空灭阵法(冷阵法*阵法);
//printArray打印数组的内容。
无效打印阵列(CoolArray*阵列);
//pascal计算pascal的第n行
//三角形,n为数组->长度,
//并将值存储在数组中。
void pascal(CoolArray*数组);
//setArray采用两个长度相同的CoolArray
//并将第一个中的值设置为
//在第二个。
void setArray(CoolArray*array1,CoolArray*array2);
int main(int argc,字符**argv){
int length=atoi(argv[1]);
阵列1;
initArray(&array1,长度);
printf(“计算帕斯卡三角形的%dth行…\n”,长度);
帕斯卡(和阵列1);
打印阵列(&array1);
销毁阵列(&array1);
返回0;
}
void initArray(CoolArray*array,int-length){
assert(length>=1);//不要使用length=length的数组;
array->vals=(无符号长整型*)calloc(长度,sizeof(无符号长整型));
返回;
}
空销毁阵列(CoolArray*阵列){
自由(数组->VAL);
返回;
}
空帕斯卡(CoolArray*array){
断言(数组->长度>=1);//确保长度没有被篡改。。。
如果(数组->长度==1){
数组->VAL[0]=1;
返回;
}
int行;
整数指数;
array->vals[0]=1;//如果不查找第一行
array->vals[1]=1;//然后从第二行开始
阵列电流;
initArray(&当前,数组->长度);
对于(行=2;行<数组->长度;++行){
当前VAL[0]=1;
对于(索引=1;索引<行;++索引){
当前.vals[index]=array->vals[index]+array->vals[index-1];
}
当前.vals[行]=1;
打印阵列(¤t);
设置阵列(阵列和当前);
}
销毁阵列(¤t);
返回;
}
无效集合数组(CoolArray*array1,CoolArray*array2){
断言(array1->length==array2->length);//确保它们的长度相同
int i=0;
对于(;ilength;++i){
阵列1->VAL[i]=阵列2->VAL[i];
}
返回;
}
无效打印阵列(CoolArray*阵列){
int i=0;
printf(“[”);
对于(;ilength;++i){
如果(i=数组->长度-1)printf(“%d]\n”,数组->VAL[i]);
else printf(“%d”,数组->VAL[i]);
}
返回;
}
有人能看出我哪里做错了吗? 它的输出现在如下所示:

/帕斯卡6

正在计算第六排帕斯卡三角形


[1]

您的错误在这一行:

if(array->length = 1){
由于单等号,您将1分配给
array->length
。对e使用
==