上题中使用指针student *p1、*p2,按数组中的成绩2从小到大进行排序。(选择排序法)
时间: 2023-06-25 14:04:50 浏览: 46
可以按照以下步骤进行选择排序:
1. 定义一个指针变量p1,让它指向数组的第一个元素。
2. 从p1所指向的元素开始,依次与它后面的元素比较,找到其中成绩最小的元素。
3. 如果找到了比p1所指向的元素更小的元素,就交换这两个元素的位置。
4. 将指针p1向后移动一位,重复步骤2和3,直到p1指向数组的最后一个元素为止。
代码示例:
```c
struct student {
char name[20];
int age;
float score1;
float score2;
float score3;
float score4;
float score5;
float score6;
float average;
};
void select_sort(struct student *arr, int len) {
struct student *p1, *p2, temp;
for (p1 = arr; p1 < arr + len - 1; p1++) {
for (p2 = p1 + 1; p2 < arr + len; p2++) {
if (p2->score2 < p1->score2) {
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
}
}
}
```
相关问题
用C语言编程,用指针实现以下功能,用选择法对一维数组中的 n 个整数按从小到大进行排序。
以下是用指针实现选择排序的C语言代码:
```c
#include <stdio.h>
void selection_sort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
if (min_idx != i) {
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
}
int main() {
int arr[] = {5, 3, 8, 1, 2};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
首先定义了一个 `selection_sort` 函数,它接受一个指向整型数组的指针 `arr` 和数组的长度 `n`。该函数使用选择排序的算法,对数组进行从小到大排序。
在 `main` 函数中,我们定义了一个整型数组 `arr`,并计算了它的长度 `n`。然后调用 `selection_sort` 函数,对数组进行排序。最后输出排序后的数组。
需要注意的是,在 `selection_sort` 函数中,我们使用了指针来操作数组元素。具体来说,我们使用了 `arr[j]` 和 `arr[min_idx]` 来访问数组中的元素,其中 `arr` 是指向数组首元素的指针。此外,我们还使用了指针来交换数组中的元素,即 `int temp = arr[i]; arr[i] = arr[min_idx]; arr[min_idx] = temp;`。
利用指针变量*p1,*p2对整型变量a和b赋值、数值交换及输出
好的,以下是一个示例代码,供你参考:
```c
#include <stdio.h>
void swap(int *p1, int *p2);
int main() {
int a, b;
int *p1, *p2;
p1 = &a;
p2 = &b;
printf("请输入两个整数:");
scanf("%d %d", p1, p2);
printf("交换前 a=%d, b=%d\n", *p1, *p2);
swap(p1, p2);
printf("交换后 a=%d, b=%d\n", *p1, *p2);
return 0;
}
void swap(int *p1, int *p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
```
该程序利用指针变量*p1,*p2对整型变量a和b赋值,并实现了数值的交换和输出。在程序中,首先通过指针变量*p1,*p2对a和b进行赋值,然后输出交换前的a和b的值。接着,调用函数swap对a和b的值进行交换,并输出交换后的a和b的值。在swap函数中,采用了传递指针的方式,通过指针变量*p1,*p2来实现对a和b的值进行交换。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)