C语言编程实验8
实验8 指针(P148-P158)
说明:
完成以下实验内容后,将本文档改名为“实验8_班级_学号_姓名.doc”,并按规定提交。
第一部分:练习题
练习题1
程序代码#include
#define ARR_SIZE 40
int FindMax(int score[], long num[],int n,long *pMaxNum);
void main()
{
int score[ARR_SIZE],maxScore,n,i;
long num[ARR_SIZE],maxNum;
printf("Please enter total number:");
scanf("%d",&n);
printf("Please enter the number and score:\n");
for (i=0;i
{
scanf("%ld%d",&num[i],&score[i]);
}
maxScore=FindMax(score,num,n,&maxNum);
printf("maxScore=%d,maxNum=%ld\n",maxScore,maxNum);
}
int FindMax(int score[],long num[],int n,long *pMaxNum)
{
int i;
int maxScore;
maxScore=score[0];
*pMaxNum=num[0];
for (i=1;i
{
if (score[i]>maxScore)
{
maxScore=score[i];
*pMaxNum=num[i];
}
}
return(maxScore);
}程序运行截图
用二维数组和指针变量作为函数参数,编程打印3个班学生的某门课程成绩的最高分,并指出具有该最高分成绩的学生是第几个班的第几个学生。程序代码#include
#define CLASS 3
#define STU 10
#define ARR_SIZE 40
int FindMax(int score[CLASS][ARR_SIZE], int m,int n,int *pRow,int *pCol);
void main()
{
int score[CLASS][ARR_SIZE],maxScore,n,i,j,row,col;
printf("Please enter student number in a class:");
scanf("%d",&n);
printf("Please enter score:\n");
for (i=0;i
{
for(j=0;j
{
scanf("%d",&score[i][j]);
}
}
maxScore=FindMax(score,n,CLASS,&row,&col);
printf("maxScore=%d,class=%d,number=%d\n",maxScore,row+1,col+1);
}
int FindMax(int score[][ARR_SIZE],int n,int m,int *pRow,int *pCol)
{
int i,j,maxScore;
maxScore=score[0][0];
*pRow=0;
*pCol=0;
for (i=0;i
{
for(j=0;j
{
if(score[i][j]>maxScore)
{
maxScore=score[i][j];
*pRow=i;
*pCol=j;
}
}
}
return(maxScore);
}程序运行截图问题解答思考题1能否使用二维数组或者而为数组的行指针作为函数参数进行编程实现呢?可以,两者都可以作为指针指向函数,从而进行地址传递思考题2利用动态内存分配,编写计算任意m行n列二维数组中最大值的函数。程序代码#include
#include
int m;
int n;
int main()
{
int *p;
int *q;
printf("输入行数m列数n: ");
scanf("%d %d",&m,