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);
}