C 我需要做什么才能使bubbleSort()全局修改数组值?
这段代码可能看起来很糟糕,我是一个初学者程序,所以让我的代码变得更好的技巧会有很大帮助。我想知道如何使bubbleSort()全局修改数组值?目前我在main中填充数组,它可以用于搜索方法,但后来我使用bubbleSort()然后尝试进行搜索,bubbleSort()似乎不影响main中的数组,而只是将其保留在函数中。我一直在四处寻找,有些地方说我需要malloc,如果是这样的话,我将如何修改我的代码来解决这个问题C 我需要做什么才能使bubbleSort()全局修改数组值?,c,arrays,struct,malloc,global,C,Arrays,Struct,Malloc,Global,这段代码可能看起来很糟糕,我是一个初学者程序,所以让我的代码变得更好的技巧会有很大帮助。我想知道如何使bubbleSort()全局修改数组值?目前我在main中填充数组,它可以用于搜索方法,但后来我使用bubbleSort()然后尝试进行搜索,bubbleSort()似乎不影响main中的数组,而只是将其保留在函数中。我一直在四处寻找,有些地方说我需要malloc,如果是这样的话,我将如何修改我的代码来解决这个问题 #include <stdio.h> #include <st
#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");
}
}
#包括
#包括
#包括
#包括
类型定义结构{
字符名[10][9];
国际数据[10];
}字;
void bubbleSort(单词q);
void linearSearch(int num,单词q);
void二进制搜索(int num,Word q);
int main(int argc,const char*argv[]{
q字;
char txtName[9];/*nul char额外增加一个字符*/
智力得分;
int i=0;
int m=0;
文件*ifp,*ofp;
ifp=fopen(“Data.txt”,“r”);
而(fscanf(ifp、%8s%d)、txtName和分数)==2){
strcpy(q.name[i],txtName);
printf(“名称:%s\n”,q.Name[i],i);
q、 数据[i]=得分;
printf(“数据:%d\n”,q.Data[i],i);
i++;
}
线性搜索(320,q);
二进制搜索(320,q);
气泡运动(q);
而(上米)!=0){
中间=楼层((上部+下部)/2);
if(num==q.data[Middle]){
FoundIt=1;
位置=中间;
}否则{
if(num
输入为:约翰32
标记12
马太福音29
路加福音21
Issac 24
凯恩2
Ryan 5
亚伯10
亚当320
Eve 1当你传递一个结构时,你传递了它的一个副本。被调用函数内的任何更改仅反映在该副本中 您可以传递现有结构的地址,并在被调用函数内取消对地址的引用以获取其内容
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]) {
/* ... */
}
/* ... */
}
为了能够全局更改数组,必须将单词作为指针传递(即发送数组的地址,以便在函数中所做的任何更改都反映在数组中) 将您的代码更改为此 函数定义
void bubbleSort (Word *q);
和函数调用
bubbleSort(&q);
和函数声明
void bubbleSort (Word q)
可能重复欢迎使用堆栈溢出!请通过阅读了解我们对问题的期望。请不要一次又一次地问同一个问题。继续上一步。通过指针传递
Word
实例。由于您是通过值传递的,因此修改了传入的Word
实例的副本,因此原始实例保持不变。您不需要全局。@SouravGhosh不,这是一个不同的问题,对于另一个问题,我只是想知道如何交换它们,以便全局影响结构数组?这就是我在bubbleSort中所做的,我可以在其他任何地方使用该数组?你需要阅读有关这些内容并自己解决问题。您需要将函数签名更改为void bubbleSort(Word*q)
。当您调用它时,请使用bubbleSort(&q)
和函数中使用q的任何位置。
您需要切换到q->
。这只是一个开始。还有其他问题,但如果你读一本书或做一些教程,你就能自己识别它们。