c语言在哪里交作业,C语言编程 急!要交作业了!不会!!

匿名用户

1级

2010-06-14 回答

下面是我给的答案,有问题的联系我,再讨论。从12点多一直做到现在,别忘了给我选成推荐答案哈。累傻了我了都

1、 杨辉三角形的每一项数据正好是组合 (即s(n!/m!/(n-m)!)的值,其中n是行数(从0行开始);m是列数(从0列开始)。 请使用上述算法得到杨辉三角形每一个位置的值并按下图打印。 要求用函数f计算一个正整数的阶乘(用递归函数来实现),通过主函数调用f完成计算。

答: 下面是我的源代码,程序输出的部分在最后的注释里面

/*

* yanghui.cc

*

* Created on: 2010-6-14

* Author: LiuFeng

* Email: sohu2000000@hotmail.com

*/

#include

#include

using namespace std;

int fac(int base){

if(base==1){

return 1;

}

return fac(base-1)*base;

}

int triDisplay(int** a, int row, int col){

if(row != col){

perror("must : row=col");

return (-1);

}

int sz=row;

printf("\n");

for(int i=0;i

*((int*)a+i*sz+0)=1;

*((int*)a+i*sz+i)=1;

}

for(int n=2;n

for(int m=1;m

*((int*)a+n*sz+m) = fac(n)/fac(m)/fac(n-m);

}

for(int n=0;n

for(int m=0;m<=n;++m){

printf("%5d", *((int*)a+n*sz+m) );

}

printf("\n");

}

return 0;

}

int

main(void){

int data[10][10];

int size=10;

if(triDisplay((int**)data,size,size)<0){

perror("bad mtria");

exit(-2);

}

return 0;

}

/*

[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc

[Administrator@ /<7>06/14]$ ./tri.exe

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

*/

2. 编写一个函数,要求对n个学生的成绩进行排序,要求用数组名作函数参数。在数组a中存放了10个学生某门课程的成绩,调用上述函数,实现对10个学生的成绩排序。

答: 程序的源代码如下,程序的输出在最后面的注释里面

/*

* DcSort.cc

*

* Created on: 2010-6-14

* Author: LiuFeng

* Email: sohu2000000@hotmail.com

*

* g++ -g -O3 DcSort.cc -Wall -o dsort

*/

#include

#include

#include

typedef struct

{

char _sname[30];

double _sscore;

} stu;

int

Partition(stu * stus, int l, int h)

{

stu pivot = stus[l];

while(l

while(l= (pivot._sscore))) h--;

if(l

while(l

if(l

}

stus[l]=pivot;

return l;

}

void DcSort(stu* stus, int low, int high){

int pivotpos;

if(low

pivotpos=Partition(stus,low,high);

DcSort(stus,low,pivotpos-1);

DcSort(stus,pivotpos+1,high);

}

}

int main(void){

stu s[10]={

{"zhao",60},

{"qian",40},

{"sun",80},

{"li",90},

{"zhou",70},

{"wu",50},

{"zheng",80},

{"jiang",90},

{"shen",100},

{"han",80},

};

for(int i=0; i<10;++i){

printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);

}

DcSort(s,0,10);

printf("\n===================after sorted ==================\n");

for(int i=0; i<10;++i){

printf("student: name: %s, score: %f\n", s[i]._sname, s[i]._sscore);

}

return 0;

}

/*

输出结果:

[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort

[Administrator@ ~/<1>preInterView/SortAndFind]$ clear

[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe

student: name: zhao, score: 60.000000

student: name: qian, score: 40.000000

student: name: sun, score: 80.000000

student: name: li, score: 90.000000

student: name: zhou, score: 70.000000

student: name: wu, score: 50.000000

student: name: zheng, score: 80.000000

student: name: jiang, score: 90.000000

student: name: shen, score: 100.000000

student: name: han, score: 80.000000

===================after sorted ==================

student: name: qian, score: 40.000000

student: name: wu, score: 50.000000

student: name: zhao, score: 60.000000

student: name: zhou, score: 70.000000

student: name: han, score: 80.000000

student: name: sun, score: 80.000000

student: name: zheng, score: 80.000000

student: name: jiang, score: 90.000000

student: name: li, score: 90.000000

student: name: shen, score: 100.000000

[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

三、程序运行结果示例:

第一题:

/*

[Administrator@ /<7>06/14]$ g++ -g -O3 -o tri yanghui.cc

[Administrator@ /<7>06/14]$ ./tri.exe

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

*/

第二题:

/*

输出结果:

[Administrator@ ~/<1>preInterView/SortAndFind]$ g++ -g -O3 DcSort.cc -Wall -o dsort

[Administrator@ ~/<1>preInterView/SortAndFind]$ clear

[Administrator@ ~/<1>preInterView/SortAndFind]$ ./dsort.exe

student: name: zhao, score: 60.000000

student: name: qian, score: 40.000000

student: name: sun, score: 80.000000

student: name: li, score: 90.000000

student: name: zhou, score: 70.000000

student: name: wu, score: 50.000000

student: name: zheng, score: 80.000000

student: name: jiang, score: 90.000000

student: name: shen, score: 100.000000

student: name: han, score: 80.000000

===================after sorted ==================

student: name: qian, score: 40.000000

student: name: wu, score: 50.000000

student: name: zhao, score: 60.000000

student: name: zhou, score: 70.000000

student: name: han, score: 80.000000

student: name: sun, score: 80.000000

student: name: zheng, score: 80.000000

student: name: jiang, score: 90.000000

student: name: li, score: 90.000000

student: name: shen, score: 100.000000

[Administrator@ ~/<1>preInterView/SortAndFind]$

*/

二、实验内容:按题目要求完成程序的改错、调试、填空和编写。

1、以下程序中,main函数通过调用fun()函数统计整数序列中的负数的个数以及平均值。

本题约定平均值由函数返回,负数的个数由参数返回。程序有若干错误,请先阅读程序,找出其中的错误行,

并写出出错的原因,最后上机调试该程序验证自己的预测。

#1 double aver(int a[], int n, int *p) // p所谓返回的参数之一,应该使用二级指针,或者指针的引用;

#2 { int i,sum=0 ; // 这里的*p指向了函数内部的临时变量,且该变量在函数结束时,

// 会同时被系统从栈上释放掉, 那么你在main函数中读取p指向的地址,得到的

// 就是一个不可以预期的值了,也就是“野指针”

// 使用二级指针或者指针的引用,是可以将内存在函数体内外带入带出的,但是

// 注意要在main中先申请好

#3 *p=0 ;

#4 for(i=0;i

#5 { sum=sum+a[i] ;

#6 if(a[i]<0) *p++;

#7 }

#8 return sum/n;

#9 }

#10 #include "stdio.h"

#11 main()

#12 { int count,x[]={0,12,33,-9,-5,27,80,0,54,63};

#13 double av;

#14 av=aver(x,10,count);

#15 printf("count: %d\naverage: %.2f\n",count,av);

#16 }

2、设有如下结构定义,且struct link为链表的结点类型,由该结构类型创建的链表中的a结点已被指针p指向(见下图),请完成下面的操作:

struct link

{ int score;

struct link *next;

}*p,*q;

(1) 写出删除b结点(包括释放其存储空间)的语句序列(允许借助于q指针),但要求链表的连续结构不能破坏,不能移动p指针。

答:

void

deleteNode(link* p, link* q, int b)

{

q = p;

int len=0;

while(q->next)++len;

if(len < b) {perror("You input a overflow position"); exit(-1);}

q=p;

while(--b){ // move to b

q=q->next;

}

while(q->next) { // overwrite the prenode,one by one

q->score=q->>next->score;

}

free(q) // q point to the last node;

}

(2) 阅读下面程序说明,按注释提示,在划线处补充细节,使程序达到预期功能。 [程序说明]以下程序中,函数create的功能是创建一个结点类型为struct link的学生成绩链表,main函数中,首先调用create函数创建一个包含N个结点的成绩链表,然后调用问题(1)的算法,将链表的第2个结点删除掉,要求输出结点删除前、后链表的内容,以验证问题(1)算法的正确性。

#include

#define N 5

#define L sizeof(struct link)

struct link

{ int score; /* 成绩 */

(1);/* 定义结点的指针域next */

};

struct link *create(void)

{

struct link *head,*p;

int i;

head=NULL;

printf("Input %d records\n",N);

for(i=1;i<=N;i++)

{

p=(2) ; /* 创建一个动态结点 */

scanf("%d",&p->score);

(3); /* 新结点进栈 */

head=p;

}

return (4) ; /* 返回所创建链表的头指针 */

}

void printlist(struct link *head) /* 输出链表 */

{ struct link *p;

p=head;

while(p!=NULL)

{ printf("%d,",p->score);

(5) ; /* 使p后移一个结点 */

}

printf("\n");

}

int

main( void)

{ struct link *base,*new;

(6) ; /* 调用create函数,创建链表 */

printlist(base); /* 输出原始链表 */

/* 借助于new指针删除第2个结点 */

(8) ; /* 输出结点删除以后的链表内容 */

printf("ok!\n");

}

(2)答:

【 (1) link * next; 】

【 (2) p= (link*)malloc(sizeof(struct link)); 】

【 (3) p->next = head 】

【 (4) head 】

【 (5) p=p->next 】

【 (6) base = create(); 】

【 (7) deleteNode(base, new, int b); // 利用我们上一问中,写好的函数 】

【 (8) printlist(base);】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值