当前位置:网站首页>明解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');
}
边栏推荐
- 如何优化OpenSumi终端性能?
- Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
- M3SDA: Moment matching for multi-source domain adaptation
- 【私人系列】日常PHP遇到的各种稀奇古怪的问题
- 360杜跃进:太空安全风险加剧,需打造一体化防御体系
- MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
- SQLyog注释 添加 撤销 快捷键
- 湖仓一体电商项目(四):项目数据种类与采集
- musicApp 的.eslintrc.js
- HCIP --- 企业网的三层架构
猜你喜欢

Linux下最新版MySQL 8.0的下载与安装(详细步骤)

【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。

mysql慢查询优化

“数字化重构系统,搞定 CEO 是第一步”

一文2500字手把手教你配置Jenkins自动化邮件通知

MySQL six-pulse sword, SQL customs clearance summary

Maxwell 一款简单易上手的实时抓取Mysql数据的软件

MySQL大总结

推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】

Mac安装PHP开发环境
随机推荐
PHP低代码开发平台 V5.0.7新版发布
ResNet18-实现图像分类
[Node implements data encryption]
【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析
After MySQL grouping, take the largest piece of data [optimal solution]
[Private Series] All kinds of strange problems encountered in daily PHP
055 c# print
从离线到实时对客,湖仓一体释放全量数据价值
Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
MindSpore:Cifar10Dataset‘s num_workers=8, this value is not within the required range of [1, cpu_thr
[hbuilder] cannot run some projects, open the terminal and cannot enter commands
MindSpore:对image作normalize的目的是什么?
【MindSpore1.2.0-rc1产品】num_workers问题
时间复杂度与空间复杂度
Day31 LeetCode
推荐系统:开源项目/工具【谷歌:TensorFlow Recommenders】【Facebook:TorchRec】【百度:Graph4Rec】【阿里:DeepRec和EasyRec】
技术很牛逼,还需要“向上管理”吗?
推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】
PostgreSQL 14.4如何安装使用
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远