当前位置:
文档之家› c语言程序设计实验报告(数组)
c语言程序设计实验报告(数组)
case 2:
p=strcat;
break;
case 3:
goto down;
}
getchar();
printf("input the first string please!\n");
i=0;
__________________________________________
printf("input the second string please!\n");
printf("%s\n",strcpy(a,b));
return 0;
}
char *strcpy(char *s,char *t)
{
while(*s++=*t++);
return(s);
}
4.程序设计
编写并上机调试运行能够实现以下功能的程序或者函数。
(1)已知一个长整型变量占4个字节,其中每个字节又分为高四位和低四位。是编写一个程序,从该整型的高字节开始,一次取出每个字节的高四位为低四位,并以数字字符的形式进行显示。
return 0;
}
void averageput(float *p)
{
float c[N],*pc=c,t=0,*pp=p;
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
t+=*(pp+M*i+j);
*(pc+i)=t/M;
printf("该同学的平均成绩为:%.1f\n",*(pc+i));
(2)利用大小为n的指针数组指向用gets函数输入的n行,每行不超过80个字符。是编写一个函数,他的每一行中连续的多个空格字符压缩为一个空格字符。在调用函数时输入压缩后的各行,空行不予输出。
(3)编写一个程序,输入n个数,排列后输出。
5.选做题
编写并上机调试运行能够实现以下功能的程序或者函数。
(1)设有N位整数和M位小数的两个数据a,b。编写
void below(float *,float *);
int main()
{
char a[M][10],(*pa)[10]=a;
int i,j;
printf("请输入各个课程的名字:\n");
for(j=0;j<M;j++)
scanf("%s",*pa+j);
float b[N][M];
float *pb1=&b[0][0];
(1)按照单步执行源程序,进入strcpy时,watches窗口中的s为何值?返回main函数时,s为何值?
(2)排除源程序的错误,并输出正确的答案。
源程序:
#include<stdio.h>
char *strcpy(char *,char *);
int main()
{
char a[20],b[60]="There is a boat in the lake.";
t=0;
}
}
float *averageall(float *p)
{
float c[M],*pc=c,*pc2=c,t=0,*pp=p;
(三)实验步骤以及结果
1.源程序改错:
存在错误,造成了空指针,并且scanf函数的格式不对应。
正确的源程序应为:
#include<stdio.h>
int main(void)
{
float a,*p;
p=&a;
scanf("%f",p);
printf("%f\n",*p);
return 0;
}
2.源程序修改,完善与替换。
printf("\t\t3 exit.\n");
printf("\t\t2 input a number(1-3)please!\n");
scanf("%d",&choice);
}while(choice<1||choice>5);
switch(choice){
case 1:
p=strcpy;
break;
#include<stdio.h>
char *strcpy(char *,char *);
int main()
{
char a[20],b[60]="There is a boat in the lake.";
printf("%s\n",strcpy(a,b));
return 0;
}
char *strcpy(char *s,char *t)
i=0;
gets(pb);
result=(*p)(a,b);
printf("The result is: %s\n",result);
down:
;
return 0;
}
运行的结果为:
3.跟踪调试源程序
进入strcpy时,s的值为:0x28fefc
返回main函数时,s的值为:0x28fedd
修改之后的正确的源程序为:
(4)掌握指针函数与函数指针的使用。
(5)掌握带有参数的main函数的使用。
二、实验内容及要求。
1.源程序改错
下面的源程序中是否存在问题?如果存在,原因是什么?如果存在错误,请改正并输出正确的结果。
源程序:
#include<stdio.h>
int main(void)
{
float *p;
scanf("%d",p);
printf("\t\t2 connect string.\n");
printf("\t\t3 exit.\n");
printf("\t\tinput a number(1-3)please!\n");
scanf("%d",&choice);
}while(choice<1||choice>=4);
switch(choice){
p--;
printf("%c\t%c\n",uphalf,lowhalf);
}
return 0;
}
运行结果为:
(2)源程序为:
#include<stdio.h>
#define N 3
void getid(char *);
int main()
{
char a[N][80],*p[N];
int i;
printf("请输入n行字符\n");
{
int t;
if(a[k]>a[k+1])
t=a[k],a[k]=a[k+1],a[k+1]=t;
}
}
运行的结果为:
#include<stdio.h>
#define M 2
#define N 5
void averageput(float *);/*计算每个学生各门课程的平均成绩*/
float *averageall(float *);/*计算全班每门课程的平均成绩*/
{
for(i=0;i<argc-1;i++)
b[i]=atoi(argv[i+1]);
p2(b,argc);
for(i=0;i<argc-1;i++)
printf("%d\n",b[i]);
}
return 0;
}
void sort1(int a[],int argc)
{
in++)
C语言程序设计实验报告
专业计算机班级计算机卓工日期2012年12月31日成绩
实验组别第8次实验;指导教师李开
学生姓名徐钦振学号U201215120
实验名称指针实验
一、实验目的
(1)熟练掌握指针的使用,赋值与使用。
(2)掌握用指针引用数组的元素,熟练指向数组的指针的使用。
(3)熟练掌握字符数组与字符串的使用。
{
char *p=s;
while(*s++=*t++);
return(p);
}
4.程序设计
(1)源代码为:
#include<stdio.h>
int main(void)
{
long x=0x1234ABCD,k;
char *p=(char *)&x;
char uphalf,lowhalf;
for(k=0;k<4;k++)
while(*ps++)
{
if(*ps==' ')
{
if(b==0)
{
b=1;
putchar(*ps);
}
}
if((*ps)!=' ')
{
b=0;
putchar(*ps);
}
}
}
运行的结果为:
程序(3):
源代码为:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
for(i=0;i<N;i++)