程序设计题(41-50)

第四十一题

题目

#include  <stdio.h>
#include  <math.h>
double  fun(double x[ ], int n)
{	int i;
    double avg=0.0, sum=0.0;
	for (i=0; i<n; i++) 
/**********found**********/
		avg += ____(1)____;
/**********found**********/
	____(2)____ /= n;
	for (i=0; i<n; i++) 
		sum += (x[i]-avg)* (x[i]-avg);
/**********found**********/
	return ____(3)____;
}
main( )
{  double score[12] ={50,60,70,80,90,100,55,65,75,85,95,99};
   double aa;
   aa= fun(score,12);
   printf("%f\n", aa);
}

解析

x[i]  avg  sqrt(sum/n)

分析

第四十二题

题目

给定程序中,函数fun的功能是:根据形参c中存储的整数序列,将偶数下标位置所有偶数元素求和,减去奇数下标位置所有奇数元素的和,所得差由函数值返回。形参d中存储的是序列的长度。

例如:若c中存储的数值依次为13, 15,4, 7,20,d为5,

则函数返回(4+20)-(15+7) = 2,主程序输出2。

#include  <stdio.h>
int fun(int* c, int d) { 
  int s0, s1, i;
  s0 = 0;
  s1 = 0;
/********************found***********************/
  for (i=0; i<___(1)____; i++) 
{
         if ((i % 2 == 0) && (c[i] % 2 == 0)) 
                s0 += c[i];
/**********************found***********************/
         if ((i % 2 == 1) && (_____(2)_____))
               s1 += c[i]; 
  }
/**********************found***********************/
  return ___(3)____;
}
 main( ) 
{ 
    int i, c[100], d;
    printf("请输入整数序列的长度:");
    scanf("%d", &d);
    printf("请输入%d个整数:\n",d);
    for (i = 0; i < d; i++)
         scanf("%d", &(c[i]));
    i = fun(c, d);
    printf("%d", i);
}

解析

d c[i]==1 s0-s1

分析

第四十三题

题目

给定程序中,函数fun的功能是用函数指针指向要调用的函数,并进行调用。规定在__2_处使f指向函数f1,在_3_处使f指向函数f2。当调用正确时,程序输出: x1=5.000000,x2=3.000000,x1*x1+x1*x2=40.000000

#include  <stdio.h>
double f1(double  x)
{  return  x*x;  }
double f2(double x, double y)
{  return  x*y;  }
double fun(double  a, double  b)
{
/**********found**********/
  __1__ (*f)();
  double  r1, r2;
/**********found**********/
  f = __2__ ;   /* point fountion f1 */
  r1 = f(a);
/**********found**********/
  f = __3__ ;   /* point fountion f2 */
  r2 = (*f)(a, b);
  return  r1 + r2;
}
main()
{ double  x1=5, x2=3, r;
  r = fun(x1, x2);
  printf("\nx1=%f,  x2=%f,  x1*x1+x1*x2=%f\n",x1, x2, r);
  getchar();
}

解析

double  f1  f2

分析

第四十四题

题目

程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a中的数据进行修改,并把a中地址作为函数值返回主函数,在主函数中输出修改后的数据。

例如: a所指变量s中的学号、姓名、和三门课的成绩依次是:10001、" ZhangSan "、95、80、88,修改后输出t中的数据应为:10002、"LiSi "、96、81、89。

#include   <stdio.h>
#include   <string.h>
struct student {
  long  sno;
  char  name[10];
  float  score[3];
};
/**********found**********/
__1__ fun(struct  student  a)
{ int  i;
  a.sno = 10002;
/**********found**********/
  strcpy(__2__, "LiSi");
/**********found**********/
  for (i=0; i<3; i++) __3__+= 1;
  return  a;
}
main()
{ struct student  s={10001,"ZhangSan", 95, 80, 88}, t;
  int  i;
  printf("\n\nThe original data :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",s.sno, s.name);
  for (i=0; i<3; i++)  printf("%6.2f ", s.score[i]);
  printf("\n");
  t = fun(s);
  printf("\nThe data after modified :\n");
  printf("\nNo: %ld  Name: %s\nScores:  ",t.sno, t.name);
  for (i=0; i<3; i++)  printf("%6.2f ", t.score[i]);
  printf("\n");
  getchar();
}

解析

struct student  a.name  a.score[i]

分析

第四十五题

题目

给定程序中,函数fun的作用是:不断从终端读入整数,由变量a统计大于0的个数,用变量b来统计小于0的个数,当输入0时结束输入,并通过形参px和py把统计的数据传回主函数进行输出。

#include  <stdio.h>
void  fun( int *px,  int  *py)
{
/**********found**********/
   int  __(1)__ ;
   scanf( "%d", &k );
/**********found**********/
   while  __(2)__
   {  if (k>0 ) a++;
      if(k<0 ) b++;
/**********found**********/
      __(3)__;
   }
   *px=a;  *py=b;
}
main()
{  int  x,  y;
   fun( &x, &y );
   printf("x=%d  y=%d\n", x,y );
  getchar();
}

解析

a=0,b=0,k    (k!=0)   scanf("%d",&k)

分析

第四十六题

题目

给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为: 2、4、6、8、10,逆置后,从头至尾结点数据域依次为: 10、8、6、4、2。

#include  <stdio.h>
#include  <stdlib.h>
#define    N    5
typedef struct node {
  int  data;
  struct node  *next;
} NODE;
/**********found**********/
__1__ * fun(NODE *h)
{ NODE  *p, *q, *r;
  p = h;
  if (p == NULL)
    return NULL;
  q = p->next;
  p->next = NULL;
  while (q)
  {
/**********found**********/
    r = q->__2__;
    q->next = p;
    p = q;
/**********found**********/
    q = __3__ ;
  }
  return  p;
}
NODE *creatlist(int  a[])
{  NODE  *h,*p,*q;        int  i;
   h=NULL;
   for(i=0; i<N; i++)
   {  q=(NODE *)malloc(sizeof(NODE));
      q->data=a[i];
      q->next = NULL;
      if (h == NULL)  h = p = q;
      else    {  p->next = q;  p = q;   }
   }
   return  h;
}
void outlist(NODE  *h)
{  NODE  *p;
   p=h;
   if (p==NULL)  printf("The list is NULL!\n");
   else
   {  printf("\nHead  ");
      do
      {  printf("->%d", p->data); p=p->next;  }
      while(p!=NULL);
      printf("->End\n");
  }
}
main()
{  NODE  *head;
   int  a[N]={2,4,6,8,10};
   head=creatlist(a);
   printf("\nThe original list:\n");
   outlist(head);
   head=fun(head);
   printf("\nThe list after inverting :\n");
   outlist(head);
   getchar();
}

解析

NODE  next  r 

分析

这段代码是 C 语言的程序,它定义了一个单链表的结构体 NODE 并实现了几个操作链表的函数。creatlist 函数用于创建链表,outlist 函数用于输出链表中的元素,fun 函数用于反转链表。

fun 函数中,需要填写三个空白处的代码来完成链表的反转。

  1. 第一个空白处(1)需要定义一个函数指针类型,它应该能够指向接受 NODE 指针参数并返回 NODE 指针的函数。所以,这里应该填写 NODE

  2. 第二个空白处(2)需要获取 q 节点的下一个节点,所以应该填写 next

  3. 第三个空白处(3)需要更新 q 指针指向下一个节点,所以应该填写 r

第四十七题

题目

给定程序中,函数fun的功能是:在形参ss所指字符串数组中,将所有串长超过k的字符串中右边的字符删除,只保留左边的k个字符。ss所指字符串数组中共有N个字符串,且串长小于M。

#include  <stdio.h>
#include  <string.h>
#define   N   5
#define   M   10
/**********found**********/
void fun(char  (*ss) __1__, int  k)
{ int  i=0  ;
/**********found**********/
  while(i< __2__) {
/**********found**********/
    ss[i][k]=__3__;  i++;  }
}
main()
{ char  x[N][M]={"Create","Modify","Sort","skip","Delete"};
  int  i;
  printf("\nThe original string\n\n");
  for(i=0;i<N;i++)puts(x[i]);  printf("\n");
  fun(x,4);
  printf("\nThe string after deleted :\n\n");
  for(i=0; i<N; i++)  puts(x[i]);  printf("\n");
  getchar();
}

解析

[M]  N '\0'

分析

第四十八题

题目

给定程序中,函数fun的功能是:在形参ss所指字符串数组中,查找含有形参substr所指子串的所有字符串并输出,若没找到则输出相应信息。ss所指字符串数组中共有N个字符串,且串长小于M。程序中库函数strstr(s1, s2)的功能是在s1串中查找s2子串,若没有,函数值为0,若有,为非0。

#include  <stdio.h>
#include  <string.h>
#define   N   5
#define   M   15
void fun(char  (*ss)[M], char  *substr)
{ int  i,find=0;
/**********found**********/
  for(i=0; i< __1__ ; i++)
/**********found**********/
    if( strstr(ss[i], __2__) != NULL )
    {  find=1;  puts(ss[i]);  printf("\n");  }
/**********found**********/
    if (find==__3__) printf("\nDon't found!\n");
}
main()
{ char  x[N][M]={"BASIC","C langwage","Java","QBASIC","Access"},str[M];
   int  i;
   printf("\nThe original string\n\n");
   for(i=0;i<N;i++)puts(x[i]);  printf("\n");
   printf("\nEnter a string for search :  ");  gets(str);
   fun(x,str);
   getchar();
}

解析

N substr 0 

分析

第四十九题

题目

函数fun的功能是:根据所给的年、月、日,计算出该日是这一年的第几天,并作为函数值返回。其中函数isleap用来判别某一年是否为闰年。例如,若输入:2008 5 1,则程序输出:2008年5月1日是该年的第122天。

#include  <stdio.h>
int  isleap(int  year)
{ int  leap;
  leap= (year%4==0 && year%100!=0 || year%400==0);
/**********found**********/
  return  __(1)__;
}
int fun(int  year, int  month, int  day)
{  int  table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
   int  days=0 , i;
   for(i=1; i<month; i++)
      days=days + table[i];
/**********found**********/
   days=days+__(2)__ ;
   if( isleap(year) && month>2 )
 /**********found**********/
     days=days+__(3)__;
   return  days;
}
main()
{  int  year, month, day, days ;
   printf("请输入年、月、日:");
   scanf("%d%d%d",&year, &month, &day);
   days = fun(year, month, day); 
   printf("%d年%d月%d日是该年的第%d天\n",year, month, day, days);
  getchar();
}

解析

leap  day  1 

分析

第五十题

题目

给定程序中,函数fun的作用是:统计整型变量m中各数字出现的次数,并存放到数组a中,其中: a[0]存放0出现的次数,a[1]存放1出现的次数,…… a[9]存放9出现的次数。

例如,若m为14579233,则输出结果应为:0,1,1,2,1,1,0,1,0,1,

#include  <stdio.h>
void fun( int  m, int  a[10])
{  int  i;
  for (i=0; i<10; i++)
/**********found**********/
    __1__ = 0;
  while (m > 0)
  {
/**********found**********/
     i = ___2___;
     a[i]++;
/**********found**********/
     m = ___3___;
  }
}
main()
{  int  m,  a[10],i;
   printf("请输入一个整数 :  ");   scanf("%d", &m);
   fun(m, a);
   for (i=0; i<10; i++)   printf("%d,",a[i]);  printf("\n");
  getchar();
}

解析

a[i]  m%10 m/10

分析

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值