gpt4 book ai didi

c - 我需要做什么才能使 bubbleSort() 全局修改数组值?

转载 作者:行者123 更新时间:2023-11-30 18:55:11 26 4
gpt4 key购买 nike

这段代码可能看起来很糟糕,我是一个初学者程序,所以让我的代码更好的提示会有很大帮助。我想知道如何使 bubbleSort() 全局修改数组值?目前我在 main 中填充数组,它适用于搜索方法,但后来我使用 bubbleSort() 然后尝试进行搜索,它看来 bubbleSort() 并没有影响 main 中的数组,而只是将其保留在函数中。我一直在环顾四周,有些地方说我需要 malloc,如果是这样,我将如何修改我的代码才能解决这个问题?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct {
char name[10][9];
int data[10];
}Word;

void bubbleSort (Word q);
void linearSearch(int num, Word q);
void binarySearch(int num, Word q);

int main (int argc, const char *argv[]){
Word q;
char txtName[9]; /* One extra for nul char. */
int score;
int i = 0;
int m = 0;
FILE *ifp, *ofp;
ifp = fopen("Data.txt", "r");
while (fscanf(ifp, "%8s %d", txtName, &score) == 2) {
strcpy(q.name[i], txtName);
printf ("Name: %s \n", q.name[i], i);
q.data[i] = score;
printf ("Data: %d \n", q.data[i], i);
i++;
}
linearSearch(320, q);
binarySearch(320, q);
bubbleSort(q);
while (m <= 9){
printf("Name: %s, Data: %d \n", q.name[m], q.data[m]);
m++;
}
return EXIT_SUCCESS;
}
void linearSearch(int num, Word q){
int i = 0;
int foundIt = 0;
int numNumbers = 10;
while ((foundIt == 0) && (i <= numNumbers)){
if (num != q.data[i]){
i = i + 1;
} else {
foundIt = 1;
}
}
if (foundIt == 1){
printf("Name found at position %d \n", i + 1);
} else {
printf("Required person not found \n");
}
}

void bubbleSort (Word q){
int last = 9;
int Swapped = 1;
int i = 0;
int m = 0;
char temp[32] = "Hello";
int tempA;
while (Swapped == 1){
Swapped = 0;
i = 0;
while (i < last){
if (q.data[i] > q.data[i+1]) {;
//Copy Name of Element
strcpy (temp, q.name[i]);
strcpy(q.name[i], q.name[i+1]);
strcpy(q.name[i+1] , temp);

//Copy Data of corresponding element
tempA = q.data[i];
q.data[i] = q.data[i+1];
q.data[i+1] = tempA;
Swapped = 1;

}
i = i + 1;
}
last = last - 1;
}
linearSearch(320, q);
}

void binarySearch(int num, Word q){
int Lower = 0;
int Upper = sizeof(&q.data);
int FoundIt = 0;
int PositionFound;
int Middle = 0;
while (FoundIt == 0 || (Lower > Upper) != 0){
Middle = floor((Upper + Lower) / 2);
if (num == q.data[Middle]){
FoundIt = 1;
PositionFound = Middle;
} else {
if (num < q.data[Middle]){
Upper = Middle - 1;
} else {
Lower = Middle + 1;
}
}
}
if (FoundIt == 1){
printf("Name found at position %d \n", PositionFound + 1);
} else {
printf("Required person not found \n");
}
}

输入是:
约翰福音 32 章
马克12
马太福音29章
路加福音 21
伊萨克24
凯恩2
瑞安5号
亚伯10
亚当320
前夕 1

最佳答案

当您传递一个结构时,您传递的是它的一个副本。被调用函数内部的任何更改仅反射(reflect)在该副本中。

您可以传递现有结构的地址,并在被调用函数内部取消引用该地址以获取其内容。

void bubbleSort(Word *pq); // pass address of structure

int main(int argc, char *argv[]) {
/* ... */
bubbleSort(&q);
/* ... */
}

void bubbleSort(Word *pq) {
/* ... */
/* use pq->STUFF instead of p.STUFF */
if (pq->data[i] > pq->data[i + 1]) {
/* ... */
}
/* ... */
}

关于c - 我需要做什么才能使 bubbleSort() 全局修改数组值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28429162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com