这段代码看起来真的很糟糕,我是一个初学者程序,因此使我的代码变得更好的提示会有所帮助。我想知道如何使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

最佳答案

传递结构时,传递它的副本。被调用函数内部的任何更改仅反映在该副本中。

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

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/

10-12 16:08