簡體   English   中英

為什么我的 arrays 在這個 C 程序上不起作用?

[英]Why are my arrays not working on this C program?

所以我必須創建一個程序,用戶可以在其中輸入整數列表,然后程序將評估列表的前半部分是否與另一半相同(所以像 1 2 3 1 2 3 這樣的列表會產生真),如果列表包含奇數個字符,則只打印一條錯誤消息。

到目前為止,我已經能夠將用戶輸入作為一個數組,然后將這個大數組分成兩個較小的 arrays,它們代表整個列表的兩半。 我的教授指定程序應該處理的最大列表是 100 個字符長,所以我聲明了一個包含 100 個元素的數組來存儲初始列表,如果列表小於這個值,那么只需使用用戶輸入的整數.

然后我的計划是聲明兩個長度為 50 的 arrays 來代表列表中每一半的最大長度。 但是,當我聲明三個 arrays (列表的 100 長數組和兩個 50 長的 arrays 的一半)時,我的程序就不起作用了。 它應該提示用戶輸入一個列表,並且當且僅當我聲明第一個數組的長度為 100 個整數並且其他兩個數組的長度為 10 個整數時才有效。 除此之外的任何事情都會導致程序跳過輸入部分並突然完成而不做它應該做的事情。

這是我的代碼:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int getList(int* list);
void checkList();

int main(int argc, char *argv[]) {
    checkList();
    return 0;
    }
    

int getList(int* list) {              
    int input;
    int count;
    int i = 0;
    
    printf("Enter the list you want to evaluate. When you're done, enter 0 twice:\n");
    
    while(input != 0 && count < 100) { //the program will prompt the user to enter numbers as long as 0 is not entered
        scanf("%d\n", &input);
        if (input == 0) {
            printf("Done! Now the program will evaluate the list\n");
        }
        else {
            list[i] = input;
            i++;
            count++;
        }
    }
    return count;
}

void checkList() {  
    int list[100]; //representing the biggest the list can ever be
    int listA[10]; //should be 50 each, but as stated above, it won't work
    int listB[10];
    int count;
    int a = getList(list);
    printf("%d\n", a); //for this part I was just printing the count variable from the method above, to see if it was returning it properly
    if (a % 2 == 0) { //checking if the number of elements of the list is odd or even
        int i;
    for (i = 0; i < a / 2; i++) {
        listA[i] = list[i];
        printf("%d ", listA[i]);
        count++;
    }
    int j;
    for (j = count; j < (a / 2) + count; j++) {
        listB[j] = list[j];
        printf("%d ", listB[j]);
    }
    }
    else {
        printf("The list entered contains an odd number of elements and therefore cannot be evaluated\n");
    }   
}

(附帶說明,當用戶輸入單個 0 時,我無法讓程序停止,而是只有在用戶輸入兩個 0 時才會停止,這是為什么呢?)

知道我的程序可能有什么問題嗎? 提前致謝

我懷疑這是因為 count 變量是未啟動的。 即使這不是直接原因,它仍然會阻止您的程序工作。 當您有一個未初始化的變量時,對程序其他部分的看似隨機的更改可能會導致它失敗。 在您的情況下,似乎更改放置在堆棧(10 與 50)元素上的值的大小會導致 getList 中的 count 變量大於 100,這意味着 while 循環將不會運行(或者,它可以將輸入初始化為0)。

我看到至少四個問題。

首先:應該在 getList 和 checkList 內部初始化 count

第二

if (a % 2 == 0) { //checking if the number of elements of the list is odd or even
        int i;
for (i = 0; i < a / 2; i++) {

如果數字 a 是奇數,則不會聲明變量 i,但仍會使用 i 進入 for 循環。 不會喜歡的。

第三:在聲明 i 之前檢查 a 是否為奇數,但不檢查 j。 如果 a 是奇數,我猜你只是忘了從 checkList() 返回。

第四:getList() 中的變量 i 和 count 有點多余。

您不需要getList function。 將數組名稱發送到指針參數有點令人困惑。 我建議您不要在代碼中混合使用數組和指針。

另外, scanf("%d", &input); 我沒有使用換行符,我可以用一個零結束列表。

這是正確的程序:

int main(int argc, char *argv[]) {
checkList();
return 0;
}




void checkList() {  
int list[100]; //representing the biggest the list can ever be
int listA[10]; //should be 50 each, but as stated above, it won't work
int listB[10];
int count;
int input;

int i = 0;

printf("Enter the list you want to evaluate. When you're done, enter 0 twice:\n");

while(input != 0 && count < 100) { //the program will prompt the user to enter numbers 
as long as 0 is not entered
    scanf("%d", &input);
    if (input == 0) {
        printf("Done! Now the program will evaluate the list\n");
    }
    else {
        list[i] = input;
        i++;
        count++;
    }
}

printf("length of list %d\n", count); //for this part I was just printing the count 
variable from the method above, to see if it was returning it properly
if (count % 2 == 0) { //checking if the number of elements of the list is odd or even
int a;
a=count;
count=0;
for (i = 0; i < a / 2; i++) {
    listA[i] = list[i];
    printf("%d ", listA[i]);
    count++;
}
int j;
for (j = count; j < (a / 2) + count; j++) {
    listB[j] = list[j];
    printf("%d ", listB[j]);
}
}
else {
    printf("The list entered contains an odd number of elements and therefore cannot 
be evaluated\n");
}   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM