当前位置:网站首页>明解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');
}
边栏推荐
- Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
- LeetCode 0952.按公因数计算最大组件大小:建图 / 并查集
- 055 c# print
- MySQL分库分表
- 【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
- MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
- Start foreground Activity
- el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
- centos7安装mysql8
- Brush questions record----string
猜你喜欢
These services can't ali interview?Then don't go to, the basic notification, etc
win2003下FTP服务器如何搭建
MySQL六脉神剑,SQL通关大总结
Download and installation of the latest version of MySQL 8.0 under Linux (detailed steps)
ECCV2022 | 对比视觉Transformer的在线持续学习
来了!东方甄选为龙江农产品直播带货
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
The JDBC programming of the MySQL database
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
MySQL数据库————视图和索引
随机推荐
推荐系统:AB测试(AB Test)
Database Tuning - Database Tuning
推荐系统:概述【架构:用户/物品特征工程---->召回层---->排序层---->测试/评估】【冷启动问题、实时性问题】
【MindSpore】用coco2017训练Model_zoo上的 yolov4,迭代了两千多batch_size之后报错,大佬们帮忙看看。
“数字化重构系统,搞定 CEO 是第一步”
Perfectly Clear QuickDesk & QuickServer图像校正优化工具
时间复杂度与空间复杂度
MySQL eight-part text recitation version
Linux下载安装mysql5.7版本教程最全详解
MySQL数据库 ---MySQL表的增删改查(进阶)
MySql密码
基于人脸的常见表情识别(2)——数据获取与整理
[PyTorchVideo Tutorial 01] Quickly implement video action recognition
TensorFlow2:概述
Win11如何更改默认下载路径?Win11更改默认下载路径的方法
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
Niuke.com - Huawei Question Bank (100~108)
Swift简介
The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary