69、给定程序中fun函数的功能是:为一个偶数寻找两个素数,这两个素数之和等于该偶数,并将这两个素数通过形参指针传回主函数。
#include <stdio.h>
#include <math.h>
void fun(int a,int *b,int *c)
{ int i,j,d,y;
for(i=3;i<=a/2;i=i 2) {
/**************found**************/
Y=1; 改为:y=1;
for(j=2;j<=sqrt((double)i);j )
if(i%j==0) y=0;
if(y==1) {
/**************found**************/
d==a-i; 改为:d=a-i;
for(j=2;j<=sqrt((double)d);j )
if(d%j==0) y=0;
if(y==1) { *b=i; *c=d; }
}
}
}
main( )
{ int a,b,c;
do
{ printf("\nInput a: "); scanf("%d",&a); } while(a%2);
fun(a,&b,&c);
printf("\n\n%d = %d %d\n",a,b,c); }
70、给定程序中fun函数的功能是:比较两个字符串,将长的那个字符串的首地址作为函数值返回。
#include <stdio.h>
/**********found**********/
char fun(char *s, char *t) 改为:char *fun(char *s, char *t)
{ int sl=0,tl=0; char *ss, *tt;
ss=s; tt=t;
while(*ss)
{ sl ;
/**********found**********/
(*ss) ; 改为:ss ;
}
while(*tt)
{ tl ;
/**********found**********/
(*tt) ; 改为:tt ;
}
if(tl>sl) return t;
else return s;
}
main( )
{ char a[80],b[80],*p,*q; int i;
printf("\nEnter a string : "); gets(a);
printf("\nEnter a string again : "); gets(b);
printf("\nThe longer is :\n\n\"%s\"\n",fun(a,b)); }
71、给定程序中fun函数的功能是:读入一个英文文本行,将其中每个单词的第一个字符改为大写,然后输出次文本行。(这里的“单词”是指由空格隔开的字符串)。例如,若输入“I am a student to take the examination.”,则应输出“I Am A Student To Take The Examination.”。
#include <ctype.h>
#include <string.h>
/************found************/
include (stdio.a) 改为:# include “stdio.h”
/************found************/
upfst ( char p ) 改为:upfst ( char *p )
{ int k=0;
for ( ; *p; p )
if ( k ) { if ( *p == ' ' ) k = 0; }
else if ( *p != ' ' ) { k = 1; *p = toupper( *p ); }
}
main( )
{ char chrstr[81];
printf( "\nPlease enter an English text line: " ); gets( chrstr );
printf( "\n\nBefore changing:\n %s", chrstr );
upfst( chrstr );
printf( "\nAfter changing:\n %s\n", chrstr ); }
72、给定程序中fun函数的功能是:根据形参m的值(2<=m<=9),在m行m列的二维数组中存放如下所示规律的数据,由main函数输出。例如,
若输入2,则输出 若输入4,则输出
1 2 1 2 3 4
2 4 2 4 6 8
3 6 9 12
4 8 12 16
#include <conio.h>
#define M 10
int a[M][M] = {0} ;
/**************found**************/
fun(int **a, int m) 改为:void fun(int (*a)[M], int m)
{ int j, k ;
for (j = 0 ; j < m ; j )
for (k = 0 ; k < m ; k )
/**************found**************/
a[j][k] = k * j ; 改为:a[j][k] = (k 1) * (j 1);
}
main ( )
{ int i, j, n ;
printf ( " Enter n : " ) ; scanf ("%d", &n ) ;
fun ( a, n ) ;
for ( i = 0 ; i < n ; i )
{ for (j = 0 ; j < n ; j )
printf ( "%4d", a[i][j] ) ;
printf ( "\n" ) ; }
}
73、给定程序中fun函数的功能是:对N名学生的学习成绩,按从高到低的顺序找出前m(m<=10)名学生来,并将这些学生数据存放在一个动态分配的连续存储区中,此存储区的首地址作为函数值返回。
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#define N 10
typedef struct ss
{ char num[10];
int s;
} STU;
STU *fun(STU a[], int m)
{ STU b[N], *t;
int i,j,k;
/**********found**********/
t=(STU *)calloc(sizeof(STU),m) 改为:t=(STU *)malloc(sizeof(STU));
for(i=0; i<N; i ) b[i]=a[i];
for(k=0; k<m; k )
{ for(i=j=0; i<N; i )
if(b[i].s > b[j].s) j=i;
/**********found**********/
t(k)=b(j); 改为:t[k]=b[j];
b[j].s=0; }
return t;
}
outresult(STU a[], FILE *pf)
{ int i;
for(i=0; i<N; i )
fprintf(pf,"No = %s Mark = %d\n", a[i].num,a[i].s);
fprintf(pf,"\n\n"); }
main( )
{ STU a[N]={ {"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77},
{"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71} };
STU *pOrder;
int i, m;
printf("***** The Original data *****\n");
outresult(a, stdout);
printf("\nGive the number of the students who have better score: ");
scanf("%d",&m);
while( m>10 )
{ printf("\nGive the number of the students who have better score: ");
scanf("%d",&m); }
pOrder=fun(a,m);
printf("***** THE RESULT *****\n");
printf("The top :\n");
for(i=0; i<m; i )
printf(" %s %d\n",pOrder[i].num , pOrder[i].s);
free(pOrder); }
74、给定程序中fun函数的功能是:从低位开始取出长整形变量s中奇数为上的数,依次构成一个新书放在t中。高位仍在高位,地位仍在低位。例如,当s中的数为:7654321时,t中的数为7531。
#include <stdio.h>
/************found************/
void fun (long s, long t) 改为:void fun (long s, long *t)
{ long sl=10;
*t = s % 10;
while ( s > 0)
{ s = s/100;
*t = s%10 * sl *t;
/************found************/
sl = sl*100; 改为:sl = sl*10;
}
}
main( )
{ long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t); }
75、给定程序中fun函数的功能是:从n(形参)个学生的成绩中统计出低于平均分的学生人数,此人数有函数值返回,平均分存放在形参aver所指的存储单元中。例如,若输入8名学生的成绩:80.5 60 72 90.5 98 51.5 88 64,则低于平均分的学生人数为4(平均分为75.5625)。
#include <stdio.h>
#define N 20
int fun ( float *s, int n, float *aver )
{ float ave, t = 0.0 ;
int count = 0, k, i ;
for ( k = 0 ; k < n ; k )
/**************found**************/
t = s[k] ; 改为: t = s[k] ;
ave = t / n ;
for ( i = 0 ; i < n ; i )
if ( s[ i ] < ave ) count ;
/**************found**************/
*aver = Ave ; 改为:*aver = ave ;
return count ;
}
main ( )
{ float s[30], aver ;
int m, i ;
printf ( "\nPlease enter m: " ) ; scanf ("%d", &m ) ;
printf ( "\nPlease enter %d mark :\n ", m ) ;
for( i = 0 ; i < m ; i ) scanf ( "%f", s i ) ;
printf( "\nThe number of students : %d \n" , fun ( s, m, &aver ) );
printf( "Ave = %f\n", aver ) ; }
76、给定程序中fun函数的功能是:根据整形形参m,计算如下公式的值。
例如,若m=2000,则应输出0.000160。
#include <stdio.h>
/************found************/
fun ( int m ) 改为:double fun ( int m )
{ double y = 0, d ;
int i ;
/************found************/
for( i = 100, i <= m, i = 100 ) 改为:for( i = 100;i <= m; i = 100 )
{ d = (double)i * (double)i ;
y = 1.0 / d ; }
return( y ) ;
}
main( )
{ int n = 2000 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ; }
77、给定程序中fun函数的功能是:读入一个整数k(2<=k<=10000),打印它的所有质因子(即所有为素数的因子)。例如,若输入整数2310,则应输出:2、3、5、7、11。
#include <stdio.h>
/************found************/
IsPrime ( int n ); 改为:IsPrime ( int n )
{ int i, m;
m = 1;
for ( i = 2; i < n; i )
/************found************/
if !( n%i ) 改为:if (!( n%i ))
{ m = 0; break ; }
return ( m );
}
main( )
{ int j, k;
printf( "\nPlease enter an integer number between 2 and 10000: " );
scanf( "%d", &k );
printf( "\n\nThe prime factor(s) of %d is( are ):", k );
for( j = 2; j <= k; j )
if( ( !( k%j ) )&&( IsPrime( j ) ) ) printf( "\n %4d", j );
printf("\n"); }