#include <stdio.h> void sort(int *a); int main() { int num[7],i; for(i=0; i<7; i++) scanf("%d",&num[i]); sort(num); return 0; }
时间: 2024-05-13 09:02:18 浏览: 73
这段代码是一个数组排序的程序,使用了指针作为形参。
首先,在 `main` 函数中,我们定义了一个长度为 7 的整型数组 `num`,并通过循环读入 7 个整数,存储到数组中。
然后,我们调用了 `sort` 函数,将数组 `num` 的首地址作为实参传递给 `sort` 函数。
在 `sort` 函数中,我们使用了指针作为形参,用于指向我们需要排序的数组。通过双重循环,实现了对数组的排序。最终,数组 `num` 中存储的元素就是按照从小到大的顺序排列的。
最后,在 `main` 函数中,我们输出排好序的数组元素。
需要注意的是,因为我们在 `sort` 函数中直接操作了数组元素,所以在 `main` 函数中输出时,排好序的数组元素已经保存在了数组 `num` 中,不需要再进行输出排序。
相关问题
补齐以下代码#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void sort(struct node *head); //采用选择排序法思想,对结点数据进行交换处理 void destroy(struct node* head); struct node* creatlink(); void traverse(struct node* head); int m
代码中缺少函数 `main`,需要补充。
以下是完整代码:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void sort(struct node *head);
void destroy(struct node* head);
struct node* creatlink();
void traverse(struct node* head);
int main() {
struct node *head = creatlink(); // 创建带头结点的链表
sort(head); // 对链表进行排序
traverse(head); // 遍历链表
destroy(head); // 销毁链表
return 0;
}
struct node *creatlink() {
struct node *head = (struct node*)malloc(sizeof(struct node)); // 创建头结点
struct node *p = head;
int num;
printf("请输入数据(输入0结束):\n");
scanf("%d", &num);
while (num) {
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = num;
newnode->next = NULL;
p->next = newnode;
p = p->next;
scanf("%d", &num);
}
return head;
}
void sort(struct node *head) {
struct node *p = head->next;
while (p != NULL) {
struct node *q = p->next;
while (q != NULL) {
if (p->data > q->data) {
int temp = p->data;
p->data = q->data;
q->data = temp;
}
q = q->next;
}
p = p->next;
}
}
void traverse(struct node *head) {
struct node *p = head->next;
printf("排序后的链表数据为:\n");
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void destroy(struct node *head) {
struct node *p = head;
while (p != NULL) {
struct node *q = p->next;
free(p);
p = q;
}
}
```
#include<stdio.h> #include<string.h> typedef struct date { int year; int month; int day; }DATE; typedef struct student { int num; char name[20]; char sex; DATE birthday; float score; }STUDENT; void input(STUDENT *s); void output(STUDENT s); /********** Begin **********/ /********** End **********/
完整代码如下所示:
```c
#include <stdio.h>
#include <string.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
typedef struct student {
int num;
char name[20];
char sex;
DATE birthday;
float score;
} STUDENT;
void input(STUDENT *s);
void output(STUDENT s);
void sort(STUDENT s[], int n);
int main() {
STUDENT s[5];
int i;
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
input(&s[i]);
}
sort(s, 5);
printf("\n排序后的学生信息为:\n");
for (i = 0; i < 5; i++) {
output(s[i]);
}
return 0;
}
void input(STUDENT *s) {
printf("学号:");
scanf("%d", &s -> num);
printf("姓名:");
scanf("%s", s -> name);
printf("性别:");
scanf(" %c", &s -> sex);
printf("生日:");
scanf("%d/%d/%d", &s -> birthday.year, &s -> birthday.month, &s -> birthday.day);
printf("成绩:");
scanf("%f", &s -> score);
}
void output(STUDENT s) {
printf("学号:%d,姓名:%s,性别:%c,生日:%d/%d/%d,成绩:%.2f\n",
s.num, s.name, s.sex, s.birthday.year, s.birthday.month, s.birthday.day, s.score);
}
void sort(STUDENT s[], int n) {
int i, j;
STUDENT temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (s[i].score < s[j].score) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
}
```
这段代码中,定义了一个main函数,用于程序的入口,创建了一个学生信息数组s。在main函数中,使用for循环对每个学生的信息进行输入。输入完成后,调用sort函数对学生信息数组s进行排序。最后,使用for循环对排序后的学生信息进行输出。
阅读全文
相关推荐













