C语言程序设计18

问题18_1

        函数 f u n fun fun 的功能是:有 N × N N\times N N×N 的矩阵,根据给定的 m ( m < = N ) m(m<=N) m(m<=N) 值,将每行元素中的值均右移动 m m m 个位置,左位置为 0 0 0
        例如 N = 3 , m = 2 N=3,m=2 N=3,m=2 所有下列矩阵
[ 1 2 3 4 5 6 7 8 9 ] \begin{equation} \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5& 6 \\ 7 & 8 & 9 \end{bmatrix} \end{equation} 147258369
        执行程序结果为
[ 0 0 1 0 0 4 0 0 7 ] \begin{equation} \begin{bmatrix} 0 & 0 &1 \\ 0 & 0& 4 \\ 0 & 0 & 7 \end{bmatrix} \end{equation} 000000147

代码18_1

#include<stdio.h>

#define N 4

void fun(int (*t)[N], int m){
	int i, j;
	for(i=0; i<N; i++){
		for(j=N-1-m; j>=0; j--)
			t[i][j+m] = t[i][j];
		for(j=0; j<m; j++)
			t[i][j] = 0;
	}
} 

void main(void){
	int t[][N] = {21, 12, 13, 24, 
				  25, 16, 47, 38,
				  29, 11, 32, 54,
				  42, 21, 33, 10}, i, j, m;
	printf("\nThe original array:\n");
	for(i=0; i<N; i++){
		for(j=0; j<N; j++)
			printf("%4d", t[i][j]);
		printf("\n");
	}
	printf("Input m(m<=%d): ", N);
	scanf("%d", &m);
	fun(t, m);
	printf("\nThe result is:\n");
	for(i=0; i<N; i++){
		for(j=0; j<N; j++)
			printf("%4d", t[i][j]);
		printf("\n");
	}
}

结果18_1

Result_18_1

问题18_2

        函数 f u n fun fun的功能是:计算并输出 h i g h high high 以内最大 10 10 10 个素数的和。 h i g h high high 的值由主函数传给 f u n fun fun 函数。
        例如,若 h i g h = 100 high=100 high=100,则输出的数值为 732 732 732

代码18_2

#include<stdio.h>
#include<conio.h>
#include<math.h>

int fun(int high){
	int sum=0, n=0, j, yes;
	while((high>=2)&&(n<10)){
		yes = 1;
		for(j=2; j<=high/2; j++){
			if(high%j==0){
				yes = 0;
				break;
			}
		}
		if(yes){
			sum += high;
			n++;
		}
		high--;
	}
	return sum;
}

void main(void){
	printf("%d\n", fun(100));
}

结果18_2

Result_18_2

问题18_3

         函数 f u n fun fun的功能是:利用下面的简单迭代方法求方程 c o s ( x )   −   x   =   0 cos(x)\ -\ x\ =\ 0 cos(x)  x = 0 的一个实根。
         x n + 1   =   c o s ( x n ) x_{n+1}\ =\ cos(x_{n}) xn+1 = cos(xn)
        迭代步骤如下:
         ( 1 ) (1) (1) x 1 x_{1} x1 初值为 0.0 0.0 0.0
         ( 2 ) (2) (2) x 0   =   x 1 x_{0}\ =\ x_{1} x0 = x1 ,将 x 1 x_{1} x1 的值赋给 x 0 x_{0} x0
         ( 3 ) (3) (3) x 1   =   c o s ( x 0 ) x_{1}\ =\ cos(x_{0}) x1 = cos(x0) ,求出一个新的 x 1 x_{1} x1
         ( 4 ) (4) (4) x 0 − x 1 x_{0}-x_{1} x0x1 的绝对值小于 0.000001 0.000001 0.000001 ,执行步骤 ( 5 ) (5) (5) ,否则执行步骤 ( 2 ) (2) (2)
         ( 5 ) (5) (5)所求 x 1 x_{1} x1 就是方程 c o s ( x )   −   x   =   0 cos(x)\ -\ x\ =\ 0 cos(x)  x = 0 的一个实根,作为函数值返回。
        程序输出结果为 R o o t = 0.739086 Root = 0.739086 Root=0.739086

代码18_3

#include<conio.h>
#include<math.h>
#include<conio.h>

double fun(){
	double x1=0.0, x0;
	do{
		x0 = x1;
		x1 = cos(x0);
	}while(fabs(x0-x1)>=1e-6);
	return x1;
}

void main(void){
	double f = fun();
	printf("Root = %f\n", f);
}

结果18_3

Result_18_3

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值