当前位置:网站首页>明解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');
}
边栏推荐
- 055 c# print
- Range.CopyFromRecordset method (Excel)
- Trial writing C language sanbang
- TensorFlow2:概述
- mysql慢查询优化
- 刷题记录----字符串
- coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products
- MindSpore:自定义dataset的tensor问题
- 推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】
- MySQL复制表结构、表数据的方法
猜你喜欢

Win11如何更改默认下载路径?Win11更改默认下载路径的方法

Database indexes: indexes are not a panacea

从离线到实时对客,湖仓一体释放全量数据价值

Snowflake vs. Redshift的2022战报:两个数据平台谁更适合你?

阿里面试这些微服务还不会?那还是别去了,基本等通知

数据库索引:索引并不是万能药

M3SDA:用于多源域自适应的矩匹配

倾斜文档扫描与字符识别(opencv,坐标变换分析)

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

MySQL database master-slave configuration
随机推荐
数据库索引:索引并不是万能药
Day31 LeetCode
ERROR 1045 (28000) Access denied for user 'root'@'localhost'Solution
win2003下FTP服务器如何搭建
【PM专用】快速统计团队还有谁没有登记上报信息,快速筛选出属于自己项目组的成员,未完成XXX工作事项的名单
MindSpore:【JupyterLab】查看数据时报错
【PyTorchVideo教程01】快速实现视频动作识别
MySQL夺命10问,你能坚持到第几问?
Swift简介
VBA runtime error '-2147217900 (80040e14): Automation error
MySql密码
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
Is the iPhone really thirteen incense?The two generations of products are completely compared, perhaps the previous generation is more worth buying
MySQL database - DQL data query language
MindSpore:ImageFolderDataset数据读取问题
MySQL大总结
普通的int main(){}没有写return 0;会怎么样?
为单行查询设置JDBC Statement.setFetchSize()为1的方法指南
【视频】极值理论EVT与R语言应用:GPD模型火灾损失分布分析
coming!Dongfang Selection brings goods to the live broadcast of Longjiang agricultural products