(1) 通过scanf函数输入三个数据并存入三个变量中; (2) 利用指针实现从大到小输出;
时间: 2023-06-05 16:09:02 浏览: 58
1. 答:可以,这是一个与编程相关的问题,可以回答。
2. 答:可以,这也是一个与编程相关的问题,可以回答。
以下为示范程序:
```c
#include <stdio.h>
void sort(int *a, int *b, int *c);
int main()
{
int x, y, z;
printf("请输入三个整数,以空格分隔:");
scanf("%d %d %d", &x, &y, &z);
sort(&x, &y, &z);
printf("从大到小输出为:%d %d %d\n", x, y, z);
return 0;
}
void sort(int *a, int *b, int *c)
{
int tmp;
if (*a < *b) {
tmp = *a;
*a = *b;
*b = tmp;
}
if (*a < *c) {
tmp = *a;
*a = *c;
*c = tmp;
}
if (*b < *c) {
tmp = *b;
*b = *c;
*c = tmp;
}
}
```
相关问题
编程实现:输入三个整数,按由大到小的顺序输出。 要求: (1) 通过scanf函数输入三个数据并存入三个变量中; (2) 利用指针实现从大到小输出;
### 回答1:
好的,这是一个编程问题,我可以帮您回答。以下是实现代码:
```c
#include <stdio.h>
void sort(int* a, int* b, int* c) {
int temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*a < *c) {
temp = *a;
*a = *c;
*c = temp;
}
if (*b < *c) {
temp = *b;
*b = *c;
*c = temp;
}
}
int main() {
int x, y, z;
printf("请输入三个整数:\n");
scanf("%d %d %d", &x, &y, &z);
sort(&x, &y, &z);
printf("从大到小的顺序为:%d %d %d\n", x, y, z);
return 0;
}
```
如果您有其他问题,可以随时问我。
### 回答2:
编写一个简单的C程序来实现这个要求。首先,我们需要定义三个整数变量,并使用scanf函数从用户那里输入这三个整数。
```c
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("请输入三个整数:\n");
scanf("%d %d %d", &num1, &num2, &num3);
```
接下来,我们需要利用指针来实现从大到小的顺序输出。我们定义一个整数指针数组,并将其指向输入的三个整数变量。然后,我们使用冒泡排序算法对这个指针数组进行排序。
```c
int *ptr[3]; // 定义整数指针数组
ptr[0] = &num1;
ptr[1] = &num2;
ptr[2] = &num3;
int i, j, temp;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2 - i; j++) {
if (*ptr[j] < *ptr[j+1]) { // 如果前一个元素小于后一个元素,交换它们
temp = *ptr[j];
*ptr[j] = *ptr[j+1];
*ptr[j+1] = temp;
}
}
}
```
最后,我们使用循环打印排好序的指针数组对应的值,即从大到小的顺序输出。
```c
printf("按由大到小的顺序输出:\n");
for (i = 0; i < 3; i++) {
printf("%d ", *ptr[i]);
}
printf("\n");
return 0;
}
```
这样,我们就实现了按从大到小顺序输出三个整数的需求。完整的代码如下:
```c
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("请输入三个整数:\n");
scanf("%d %d %d", &num1, &num2, &num3);
int *ptr[3]; // 定义整数指针数组
ptr[0] = &num1;
ptr[1] = &num2;
ptr[2] = &num3;
int i, j, temp;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2 - i; j++) {
if (*ptr[j] < *ptr[j+1]) { // 如果前一个元素小于后一个元素,交换它们
temp = *ptr[j];
*ptr[j] = *ptr[j+1];
*ptr[j+1] = temp;
}
}
}
printf("按由大到小的顺序输出:\n");
for (i = 0; i < 3; i++) {
printf("%d ", *ptr[i]);
}
printf("\n");
return 0;
}
```
当我们运行程序并输入三个整数时,它会按照从大到小的顺序打印出输入的整数。
### 回答3:
```cpp
#include <stdio.h>
void sort(int *a, int *b, int *c) {
int temp;
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
if (*b < *c) {
temp = *b;
*b = *c;
*c = temp;
}
if (*a < *b) {
temp = *a;
*a = *b;
*b = temp;
}
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d %d %d", &a, &b, &c);
sort(&a, &b, &c);
printf("由大到小的顺序输出:%d %d %d\n", a, b, c);
return 0;
}
```
在这个程序中,我们首先通过`scanf`函数输入三个整数,并存入变量`a`,`b`和`c`中。然后,我们使用指针来实现从大到小的排序。具体来说,我们定义了一个`sort`函数,该函数接受三个指向整数的指针参数。在`sort`函数中,我们使用了一个临时变量`temp`来交换变量的值,从而达到从大到小的排序目的。
在`main`函数中,我们调用了`sort`函数,并将变量`a`,`b`和`c`的地址作为参数传递给`sort`函数。最后,我们按照从大到小的顺序输出变量`a`,`b`和`c`的值。
C语言,从键盘输入20个数,存入一个数组中,用指针变量做函数参数,求最大值和最小值。
好的,以下是C语言程序的实现方法:
```c
#include <stdio.h>
void findMinMax(int *arr, int size, int *min, int *max) {
*min = *max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] < *min) {
*min = arr[i];
}
if (arr[i] > *max) {
*max = arr[i];
}
}
}
int main() {
int arr[20], min, max;
printf("请输入20个数:\n");
for (int i = 0; i < 20; i++) {
scanf("%d", &arr[i]);
}
findMinMax(arr, 20, &min, &max);
printf("最小值:%d\n", min);
printf("最大值:%d\n", max);
return 0;
}
```
在这个程序中,`findMinMax()`函数用于查找最小值和最大值。它接收一个整数数组的指针、数组大小、最小值指针和最大值指针作为参数。函数使用指针间接修改最小值和最大值的值。在`main()`函数中,首先从键盘输入20个数,然后调用`findMinMax()`函数查找最小值和最大值,并将结果打印出来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)