c语言作业资料(5)

2019-09-01 16:00

6.编程输出以下的杨辉三角形(输出前10行) 源程序: #include void main()

{int i,j,a[10][10]; for(i=0;i<10;i++) {a[i][0]=1; a[i][i]=1;}

for(i=2;i<10;i++) for(j=1;j

a[i][j]=a[i-1][j-1]+a[i-1][j]; for(i=0;i<10;i++) {for(j=0;j<=i;j++) printf(\printf(\}

运行结果: 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 9 36 84 126 126 84 36 Press any key to continue

1 9 1 指针作业:

1比较n(n<10)个字符串排序输出 源程序:

#include #include

void sort(char array[][20],int n); void main() {char str[10][20]; int i,j,k,n;

printf(\scanf(\

printf(\for(i=0;i<=n;i++) gets(str[i]); sort(str,n);

for(i=0;i<=n;i++)

puts(str[i]);}

void sort(char array[][20],int n) {char temp[20]; int i,j,k;

for(i=1;i<=n-1;i++)

{k=i;

for(j=i+1;j<=n;j++)

if(strcmp(array[k],array[j])>0) k=j;

if(k!=i)

{strcpy(temp,array[i]); strcpy(array[i],array[k]); strcpy(array[k],temp);} }

}

运行结果: input n(n<=10):5 input 5 string: girl boy student teacher docter boy docter girl student

teacher

Press any key to continue

方法二: 源程序: #include #include #define N 5 void main()

{char *str[N]={\int i,j,k; char *temp; for(i=0;i

for(j=i+1;j0) k=j;

temp=str[i];str[i]=str[k];str[k]=temp;} for(i=0;i

printf(\}

运行结果: boy doctor girl student

teacher

Press any key to continue 2 用函数实现对变量的交换。 源程序; #include void swap(int *p1,int *p2) {int temp;

temp=*p1;*p1=*p2;*p2=temp;} void main() {int a=2,b=5; swap(&a,&b);

printf(\运行结果;

5,2Press any key to continue

3,输入n个学生姓名,数学成绩,英语成绩,并按这两门成绩的平均分从小到大输出。 源程序:

#include

struct student{char name[5];float math,eng;float aver;}; void main()

{struct student stu[5],temp;

int i,sub,k; float aver=0; for(i=0;i<5;i++)

{scanf(\stu[i].aver=(stu[i].math+stu[i].eng)/2.0;} for(i=0;i<4;i++) {sub=i;

for(k=i+1;k<5;k++) if(stu[k].aver

{printf(\printf(\运行结果; 输入: zs 50 90 ls 60 60 wr 80 80 zh 70 50 qr 65 65 输出: ls60.0 60.0 zh70.0 50.0 qr65.0 65.0 zs50.0 90.0

wr80.0 80.0

Press any key to continue


c语言作业资料(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:大物A(二)综合训练

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: