当前位置:网站首页>明解C语言第六章习题
明解C语言第六章习题
2022-07-30 19:54:00 【子夜的星】
明解C语言第六章习题
第一节:什么是函数
练习6-1
#include<stdio.h>
int min2(int a, int b)
{
return (a > b) ? b : a;
}
int main()
{
int a, b;
printf("a="); scanf("%d", &a);
printf("b="); scanf("%d", &b);
printf("最小的是:%d", min2(a, b));
}
练习6-2
#include<stdio.h>
int min3(int a, int b,int c)
{
int min = a;
if (b < min) min = b;
if (c < min) min = c;
return min;
}
int main()
{
int a, b;
printf("a="); scanf("%d", &a);
printf("b="); scanf("%d", &b);
printf("c="); scanf("%d", &c);
printf("最小的是:%d", min3(a, b,c));
}
练习6-3
#include <stdio.h>
int cub(int x)
{
return x * x * x;
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("它的立方是%d",cub(x));
return 0;
}
练习6-4
#include <stdio.h>
int sqr(int x)
{
return x * x ;
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("它的四次幂是%d", sqr(sqr(x)));
return 0;
}
练习6-5
#include <stdio.h>
int sumup(int n)
{
int sum = 0;
while (n-->0)
{
sum += n;
}
return sum;
}
int main()
{
int x;
printf("请输入一个数:");
scanf("%d", &x);
printf("从1到%d之间所有整数的和是%d",x, sumup(x));
return 0;
}
第二节:函数设计
练习6-6
#include <stdio.h>
void alert(int n)
{
while (n-- > 0)
{
putchar('\a');
}
}
int main()
{
int x;
printf("请输入要发出响铃的次数:");
scanf("%d", &x);
alert (x);
}
练习6-7
#include <stdio.h>
void hello()
{
printf("你好。\n");
}
int main()
{
hello();
}
练习6-8
#include <stdio.h>
#define NUMBER 5
int min_of(const int v[], int n)
{
int i;
int min = v[0];
for (i = 1; i < n; i++)
if (v[i] < min)
min = v[i];
return min;
}
练习6-9
#include <stdio.h>
#define number 7
void rev_intary(int v[], int n)
{
int i;
for (i = 0; i < n/ 2 - 1; i++)
{
int temp = v[i];
v[i] = v[n - 1 - i];
v[n- 1 - i] = temp;
}
}
练习6-10
#include <stdio.h>
#define number 7
void intary_rcpy(int v1[], const int v2[], int n)
{
int i;
for (i = 0; i < n; i++)
{
v1[i] = v2[n - i - 1];
}
}
练习6-11
#include <stdio.h>
#define number 5
int search_idx(const int v[], int idx[], int key, int n)
{
int i;
int x = 0;
for (i = 0; i < n; i++)
{
if (v[i] == key)
x++;
idx[x] = v[i];
}
return x;
}
练习6-12
#include<stdio.h>
void mat_mul(const int a[4][3], const int b[3][4], int c[3][3])
{
int i, j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
c[i][j] = 0;
for (k = 0; k < 3; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
练习6-13
#include <stdio.h>
/*--- 将4行3列矩阵a和b的和存储在c中 ---*/
void mat_add(const int a[4][3], const int b[4][3], int c[2][4][3])
{
int x, y, z;
for (x = 0; x < 2; x++)
for (y = 0; y < 4; y++)
for (z = 0; z < 3; z++)
{
c[x][y][z] = a[y][z] + b[y][z];
}
}
/*--- 显示4行3列矩阵m ---*/
void mat_print(const int m[4][3])
{
int i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
printf("%4d", m[i][j]);
putchar('\n');
}
}
int main(void)
{
int tensu1[4][3] = {
{
91, 63, 78}, {
67, 72, 46}, {
89, 34, 53}, {
32, 54, 34} };
int tensu2[4][3] = {
{
97, 67, 82}, {
73, 43, 46}, {
97, 56, 21}, {
85, 46, 35} };
int sum[2][4][3]; /* 总分 */
mat_add(tensu1, tensu2, sum); /* 求两次考试中成绩的总和 */
puts("第一次考试的分数"); mat_print(tensu1); /* 显示第一次考试的分数 */
puts("第二次考试的分数"); mat_print(tensu2); /* 显示第二次考试的分数 */
puts("总分"); mat_print(sum); /* 显示总分 */
return 0;
}
第三节:作用域和存储期
练习6-14
#include <stdio.h>
#define NUMBER 4
int main()
{
int i;
static double x[NUMBER];
for (i = 0; i < NUMBER; i++)
{
printf("v[%d]=%.1f", i, x[i]);
putchar('\n');
}
}
练习6-15
#include <stdio.h>
void put_count()
{
static i = 1;
printf("put-count:第%d次", i++);
putchar('\n');
}
边栏推荐
- 银行数据资产转换能力弱?思迈特软件助力解决银行困境
- MindSpore:【JupyterLab】查看数据时报错
- 【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
- ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
- iPhone真是十三香?两代产品完全对比,或许上一代更值得买
- The JDBC programming of the MySQL database
- MySQL database - DQL data query language
- Start background services across processes
- 【私人系列】日常PHP遇到的各种稀奇古怪的问题
- mysql8 installation under linux
猜你喜欢
随机推荐
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
基于人脸的常见表情识别(1)——深度学习基础知识
在jOOQ中获取数据的多种不同方式
KEIL问题:【keil Error: failed to execute ‘C:\Keil\ARM\ARMCC‘】
MySQL slow query optimization
The JDBC programming of the MySQL database
【flink】报错整理 Could not instantiate the executor. Make sure a planner module is on the classpath
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
数据库索引:索引并不是万能药
推荐系统:AB测试(AB Test)
技术很牛逼,还需要“向上管理”吗?
MySQl数据库————DQL数据查询语言
win2003下FTP服务器如何搭建
Is the iPhone really thirteen incense?The two generations of products are completely compared, perhaps the previous generation is more worth buying
来了!东方甄选为龙江农产品直播带货
【PyTorchVideo教程01】快速实现视频动作识别
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
.eslintrc.js for musicApp
Brush questions record----string
[Node implements data encryption]