高级语言期末2010级B卷(计算机学院)

1.编一函数,求一个NXM的二维整形数组中各元素的平均值和最大值。

#include <stdio.h>
#define N 10
#define M 10

void getmean(int a[N][M]) {
	int sum=0,max=0;
	for(int i=0; i<N; i++)
		for(int j=0; j<M; j++) {
			sum+=a[i][j];
			if(a[i][j]>max)
				max=a[i][j];
		}
	printf("mean=%f,max=%d",sum/N*M,max);
}

2.编一函数,完成NXN的整形数组 a的赋值,其中,a的各元素值如下图所示,未显示的其他元素值均为0.

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 45 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81

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

int** putlist(int N) {
	int **a=(int **)malloc(sizeof(int *)*N);
	for(int i=0; i<N; i++)
		a[i]=(int *)malloc(sizeof(int)*N);
	for(int i=0; i<N; i++)
		for(int j=0; j<N; j++) {
			if(i>=j)
				a[i][j]=(i+1)*(j+1);
			else
				a[i][j]=0;
		}
	return a;
}

int main() {
	int **a=putlist(9);
	for(int i=0; i<9; i++) {
		for(int j=0; j<9; j++)
			if(i>=j)
				printf("%d ",a[i][j]);

		printf("\n");
	}
}

3.编写一个递归函数,计算f(n)=r1!+r2!+r3!+r4!,其中r1,r2,r3,r4都是n的各个数位。

#include <stdio.h>

int fac(int n) {
	if(n==0)
		return 1;
	else
		return n*fac(n-1);
}

int func(int n) {
	if(n==0)
		return 0;
	return func(n%10)+fac(n/10);
}

4.编一函数,完成对字符串参数的判断,如果该字符是对称字符串,函数值返回1,如果不是则返回零。

#include <stdio.h>
#include <string.h>

int func(char *str) {
	int i=0,j=strlen(str)-1;
	while(i<j) {
		if(str[i]!=str[j])
			return 0;
		i++;
		j--;
	}
	return 1;
}

int main() {
	char str[]="qwewq";
	printf("%d",func(str));
}

5.某班学生的信息包括学号、姓名、性别、成绩等信息。
1)完成学生信息的结构定义;
2)编写一个名为CreatList的函数实现建立具有n个节点的链表来存储这个班的学生信息;
3)编一函数WriteFile,将该班的信息存入文件。

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

typedef struct student {
	int num;
	char name[10];
	int sex;
	int score;
	struct student *next;
} student;

struct student *CreatList(int n) {
	struct student *head=(struct student*)malloc(sizeof(struct student));
	for(int i=0; i<n; i++) {
		struct student *p=(struct student*)malloc(sizeof(struct student));
		scanf("%d %s %d %d",&p->num,&p->name,&p->sex,&p->score);
		p->next=head->next;
		head->next=p;
	}
	return head->next;
}

void WriteFile(struct student *head) {
	FILE *file;
	if((file=fopen("write.txt","w"))==NULL) {
		printf("OPEN ERROR");
		exit(0);
	}
	struct student *p=head;
	while(p!=NULL) {
		fprintf(file,"num=%d name=%s sex=%d score=%d",p->num,p->name,p->sex,p->score);
		p=p->next;
	}
	fclose(file);
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值