当前位置:网站首页>C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
C language 2: find the maximum value of three numbers, find the middle value of three numbers, and write program steps
2022-07-27 11:02:00 【He Xiaoqi (QI) ~】
List of articles
1. Example 1
Enter three numbers , Find the largest number
1.1 Ternary operator
#include<stdio.h>
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("max %d \n", max);
return 0;
}
1.2 if Nesting of statements
#include<stdio.h>
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
max = a;
}
else if (b > a && b > c)
{
max = b;
}
else
{
max = c;
}
printf("max %d \n", max);
return 0;
}
1.3 Code reuse is the highest ( Pass value assignment )
#include<stdio.h>
int Max_Int(int a, int b)
{
int c = a > b ? a : b;
return c;
}
int main()
{
int a = 0, b = 0, c = 0;// initialization
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = Max_Int(a, Max_Int(b, c));// Value transfer call , Arguments are assigned to parameters
printf("max %d \n", max);
return 0;
}
// Code reuse is the highest , That is, the best execution method
2. Example 2
Enter three numbers , Find the output intermediate value .
Algorithm implementation : Bubble sort , Enter three values , First sort the three values , Output intermediate value , This is the number you want .
2.1 The wrong sample
Code redundancy , Did not pay attention to the code writing specification
#include<stdio.h>
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
mid = b;
printf("mid=%d \n", mid);
return 0;
}
2.2 Method 1
The main function is separated from other functions , Code algorithm implementation is clearly visible , Code writing is standard, clear and good
#include<stdio.h>
int Mid_Int(int a, int b, int c)
{
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
return b;
}
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
mid = Mid_Int(a, b, c);
printf("mid=%d \n", mid);
return 0;
}
2.3 Method 2
A formal parameter is defined as a pointer type , Delivery address ( Exchange function )
#include<stdio.h>
void Swap_Int(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
int main()
{
int x = 10, y = 20;
Swap_Int(x, y);
printf("x=%d,y=%d \n", x, y);
return 0;
}
3. The steps and process of programming ( Suitable for beginners )
(1) demand : Put forward your own needs for code , Ask questions
(2) analysis : Analyze the problems and requirements raised
(3) Design : That is to design the algorithm of the code
(4) The implementation of : Implementation of program code , That is, the implementation of coding
(5) test : Test the code modules that you write your own code
边栏推荐
- Solved syntaxerror: (Unicode error) 'Unicode scape' codec can't decode bytes in position 2-3: truncated
- 发动机悬置系统冲击仿真-瞬时模态动态分析与响应谱分析
- 树形数据转换
- Tdengine business ecosystem partner recruitment starts
- How to assemble a registry
- 一次跨域问题的记录
- JVM -- Analysis of bytecode
- flask_ Output fields in restful (resources, fields, marshal, marshal_with)
- 学习笔记-微信支付
- Edata base, a secondary development project based on spark packaging, is introduced
猜你喜欢

MySQL index, transaction and storage engine

PyQt5快速开发与实战 4.2 QWidget

Tdengine business ecosystem partner recruitment starts
[intensive reading of thesis]bert

Advanced operation of MySQL data table

迭代次数和熵之间关系的一个验证试验

How to create a.Net image with diagnostic tools

MySQL日志管理、备份与恢复

c语言指针函数和函数指针的辨析

文档智能多模态预训练模型LayoutLMv3:兼具通用性与优越性
随机推荐
MIMO array 3D imaging technology based on mobile terminal
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
Learning C language together: structure (2)
Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change
Advanced operation of MySQL data table
学习笔记-微信支付
Shardingproxy sub database and table actual combat and comparison of similar products
The permission problem of Oracle operating openldap
Time and power allocation method to ensure fairness in sensor fusion system
GEE中下载过程中出现 Error: Image.clipToBoundsAndScale, argument 'input'
Use kaggle to run Li Hongyi's machine learning homework
SQL Server2000数据库错误
MySQL must know and know!!! Reading this article is enough!!!
MySQL master-slave architecture, read-write separation, and high availability architecture
Server access speed
antd table+checkbox 默认值显示
Camera switching
Overview of data security in fog computing
parsel的使用
Tcp/ip protocol