栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > C++面试题库

C语言编程笔试题(第三十一套)

C语言编程笔试题(第三十一套)

编程题:

26.请编写一个函数fun,它的功能是:根据以下公式求π的值(要求满足精度0.0005,即某项小于0.0005时停止迭代):

 

程序运行后,如果输入精度0.0005,则程序输出为3.14…。

注意:部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

#include <conio.h>

#include <stdio.h>

#include <string.h>

 

void fun(char *s, char t[])

{

}

 

main()

{

char s[100], t[100];

clrscr();

printf(“nPlease enter string S:”);

scanf(“%s”, s);

fun(s, t);

printf(“nThe result is : %sn”, t);

}

 

42.下列程序定义了NxN的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N],int n),该函数的功能是:使数组右上半三角元素中的值乘以m。例如:若m的值为2,a数组中的值为:,则返回主程序后a数组中的值应为:。

注意:部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#define  N  5

void  fun ( int a[][N],  int m )

{

}

 

main ( )

{

int a[N][N], m, i, j;

clrscr();

printf(“**** The array *****n”);

for ( i=0;  i<N; i++ )

{

for( j=0; j<N; j++ )

{

a[i][j] = rand()%20;

printf(“%4d”, a[i][j] );

}

printf(“n”);

}

do {

m = rand()%10

}   while ( m>=3 );

printf(“m=%4dn”, m);

fun ( a ,m );

printf (” THE RESULTn”);

for ( i=0;  i<N; i++ )

{

for ( j=0; j<N; j++ )

printf( “%4d”, a[i][j] );

printf(“n”);

}

}

 

改错题:

80.下列给定程序中函数fun的功能是:求出以下分数序列的前n项之和。

2    3    5    8    13   21

-   -   -   -   -   -

1,  2,  3,  5,  8,  13,…

和值通过函数值返回main函数。例如,若n=5,则应输出8.391667。

请改正程序中的错误,使它能计算出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!

试题程序:

#include <conio.h>

#include <stdio.h>

 

fun (int n )

{

int a, b, c, k ;

double s;

s = 0.0;

a = 2;

b = 1;

for ( k = 1; k <= n; k++ )

{

s = s + (Double) a / b ;

c = a;

a = a + b;

b = c;

}

return s;

}

 

main( )

{

int  n = 5 ;

clrscr( ) ;

printf(“nThe value of function is :%lfn”,  fun ( n ) ) ;

}

 

 

81.下列给定程序中,函数fun的功能是:根据整型形参n,计算如下公式的值。

1         1           1

A1=1,A2=  - ,A3=  -,…,An=  -

1+A1      1+A2         1+An-1

例如,若n=10,则应输出0.617977。

请改正程序中的错误,使它能得出正确的结果。

注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!

试题程序:

#include <conio.h>

#include <stdio.h>

 

int  fun(int n)

{

float A=1;int i;

for(i=2;i<n;i++)

A=1.0/(1+A);

return A;

}

 

main( )

{

int n;

clrscr();

printf(“nPlease enter n: “);

scanf(“%d”, &n );

printf(“A%d=%fn”, n, fun(n) );

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/265378.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号