当前位置:文档之家› 第十章(指针)

第十章(指针)

1.补充程序,程序实现从10个数中找出最大值和最小值。

#include <stdio.h>
#include <stdlib.h>
int max,min;
void find_max_min(int *p,int n)
{
int *q;
max=min=*p;
for(q=p; q</**/ p+n /**/; q++)
if(/**/ max<*q /**/ ) max=*q;
else if(min>*q) min=*q;
}
void main()
{
int i,num[10];
printf("Input 10 numbers: ");
for(i=0;i<10;i++) scanf("%d",&num[i]);
find_max_min(/**/ num /**/,10);
printf("max=%d,num=%d\n",max,min);
}
2.补充程序,其中main函数通过调用average函数计算数组元素的平均值。

#include <stdio.h>
float average(int *pa,int n)
{
int k;
/**/ float avg=0; /**/
for(k=0;k<n;k++)
avg = avg+/**/ *(pa+k) /**/;
avg = avg/n;
return avg;
}
void main()
{ int a[5]={20,30,45,64,23};
float m;
m=average(/**/ a /**/, 5);
printf("Average=%f\n",m);
}
3.程序功能是将程序中的两个字符串”ABC”、”xyz”连接在一起,并输出:ABCxyz。

#include <stdio.h>
#include <string.h>
void main()
{
char s1[12]="ABC", s2[]="xyz";
char * ps1=s1,*ps2;
/**/ ps2 = NULL;ps2=s2; /**/
/**/ while(*ps1 == NULL)while(*ps1!=’\0’) /**/
ps1++;
while(*ps2) *(ps1++) = *(ps2++);
printf("%s\n",s1);
getch();
}
4.打开程序Cprog032.c,完成其中的函数fun(char *s ),使程序实现统计输入字符串中空格的个数。

#include <stdio.h>
int fun(char *s)
{ /**/
int count=0;
while(*s)
{count++;
s++;
}
return count;
/**/
}
void main()
{
char str[255];
gets(str);
printf("%d\n",fun(str));
}
5. 将程序填写完整,使其中函数void tru(char *s),实现将字符串中所有奇数位置上(注:本题将字符串的首字符称为第一位)的小写英文字母转换为大写英文字母(若不是英文字母则不转换)。

例如:原字符串ssdax31 被转换成SsDaX31
#include <stdio.h>
#include <conio.h>
void trn(char *s)
{
int i=1,n=0;
char *p=s;
while(*p)
{
if(/**/ (i%2!=0) && (*p>='a' && *p<='z' ) /**/ ) *p=*p-32;
/**/ p++; /**/
i++;
}
}
void main()
{
char ss[100];
printf("Input string: \n");
gets(ss);
trn(ss);
printf("\nNow string is :\n");
puts(ss);
getch();
}。

相关主题