#include <stdio.h> /*------- ERROR ------*/ #define N 20; void fun(int a[], int n) { int i, j, t, p; for (j = 0 ;j < n-1 ;j++) { /*------- ERROR ------*/ p = j for (i = j;i < n; i++) if(a[i] < a[p]) /*------- ERROR ------*/ p = j t = a[p] ; a[p] = a[j] ; a[j] = t; } } main() { int a[N]={9,6,8,3,-1},i, m = 5; printf("排序前的数据:"); for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); fun(a,m); printf("排序后的数据:"); for(i = 0;i < m;i++) printf("%d ",a[i]); printf("\n"); }
时间: 2024-01-17 07:04:52 浏览: 126
根据代码的意图,应该是想实现选择排序的功能,但是代码中存在一些错误。
可以将代码修改如下:
```c
#include <stdio.h>
#define N 20
void fun(int a[], int n) {
int i, j, t, p;
for (j = 0; j < n - 1; j++) {
p = j; // 加上分号
for (i = j + 1; i < n; i++) { // 将 i 的初始值改为 j+1
if (a[i] < a[p]) {
p = i; // 将 p 改为 i
}
}
t = a[p]; a[p] = a[j]; a[j] = t;
}
}
int main() {
int a[N] = {9, 6, 8, 3, -1}, i, m = 5;
printf("排序前的数据:");
for (i = 0; i < m; i++) printf("%d ", a[i]); printf("\n");
fun(a, m);
printf("排序后的数据:");
for (i = 0; i < m; i++) printf("%d ", a[i]); printf("\n");
return 0; // 添加返回值
}
```
这样就可以实现选择排序的功能了。
相关问题
优化这段代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<conio.h> #define OK 1 #define error 0 #define MVNum 100 #define MAXSIZE 10 typedef int OtherInfo,QElemtype; typedef char VerTexType; //结构体定义 typedef struct ArcNode{ int adjvex;
首先,该段代码中包含了一些不必要的头文件,例如:conio.h,malloc.h等。建议只保留stdio.h和stdlib.h。
其次,可以将#define OK 1和#define error 0替换为枚举类型,使代码更加清晰易读。
最后,在结构体定义中,可以将OtherInfo和QElemtype合并为一个类型,避免定义过多类型。同时,也可以将结构体中的变量类型进行优化,例如将int类型的adjvex改为short或者char类型。
重构后的代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
#define MAX_ARC_NUM 10
typedef enum {
ERROR = 0,
OK = 1
} Status;
typedef char VertexType;
typedef int ArcType;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
ArcType* info;
} ArcNode;
typedef struct VNode {
VertexType data;
ArcNode* firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
```
希望可以帮到你。
2、实现图的深度优先遍历和广度优先遍历 /*用邻接矩阵法创建有向网的算法如下:*/ //#include "adjmatrix.h" #include<stdio.h> *最多顶点个数*/ #define MAX VERTEX NUM20 *表示极大值,即∞*/ #define INFINITY 32768 #defne True 1 #define False 0 #define Error -1 16896 #define Ok 1
深度优先遍历(DFS):
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum, arcnum;
} MGraph;
bool visited[MAX_VERTEX_NUM];
void CreateGraph(MGraph* G) {
printf("请输入顶点数和边数:");
scanf("%d %d", &(G->vexnum), &(G->arcnum));
printf("请输入顶点:");
for (int i = 0; i < G->vexnum; i++) {
scanf(" %c", &(G->vexs[i]));
}
for (int i = 0; i < G->vexnum; i++) {
for (int j = 0; j < G->vexnum; j++) {
if (i == j) {
G->arcs[i][j] = 0;
} else {
G->arcs[i][j] = INFINITY;
}
}
}
printf("请输入每条边的起点、终点、权值:\n");
for (int k = 0; k < G->arcnum; k++) {
int i, j, w;
scanf("%d %d %d", &i, &j, &w);
G->arcs[i][j] = w;
}
}
void DFS(MGraph* G, int v) {
visited[v] = true;
printf("%c ", G->vexs[v]);
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[v][j] != INFINITY && !visited[j]) {
DFS(G, j);
}
}
}
void DFSTraverse(MGraph* G) {
for (int i = 0; i < G->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
DFS(G, i);
}
}
}
int main() {
MGraph G;
CreateGraph(&G);
printf("深度优先遍历结果:");
DFSTraverse(&G);
return 0;
}
```
广度优先遍历(BFS):
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef int EdgeType;
typedef struct {
VertexType vexs[MAX_VERTEX_NUM];
EdgeType arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
int vexnum, arcnum;
} MGraph;
bool visited[MAX_VERTEX_NUM];
void CreateGraph(MGraph* G) {
printf("请输入顶点数和边数:");
scanf("%d %d", &(G->vexnum), &(G->arcnum));
printf("请输入顶点:");
for (int i = 0; i < G->vexnum; i++) {
scanf(" %c", &(G->vexs[i]));
}
for (int i = 0; i < G->vexnum; i++) {
for (int j = 0; j < G->vexnum; j++) {
if (i == j) {
G->arcs[i][j] = 0;
} else {
G->arcs[i][j] = INFINITY;
}
}
}
printf("请输入每条边的起点、终点、权值:\n");
for (int k = 0; k < G->arcnum; k++) {
int i, j, w;
scanf("%d %d %d", &i, &j, &w);
G->arcs[i][j] = w;
}
}
void BFS(MGraph* G, int v) {
int queue[MAX_VERTEX_NUM], front = 0, rear = 0;
printf("%c ", G->vexs[v]);
visited[v] = true;
queue[rear++] = v;
while (front != rear) {
int i = queue[front++];
for (int j = 0; j < G->vexnum; j++) {
if (G->arcs[i][j] != INFINITY && !visited[j]) {
printf("%c ", G->vexs[j]);
visited[j] = true;
queue[rear++] = j;
}
}
}
}
void BFSTraverse(MGraph* G) {
for (int i = 0; i < G->vexnum; i++) {
visited[i] = false;
}
for (int i = 0; i < G->vexnum; i++) {
if (!visited[i]) {
BFS(G, i);
}
}
}
int main() {
MGraph G;
CreateGraph(&G);
printf("广度优先遍历结果:");
BFSTraverse(&G);
return 0;
}
```
阅读全文