2015-02-07 107 views
1

對於我的學校項目,我應該用大約-10和10之間的隨機數填充20的數組。然後,我必須根據它們是否爲負值來組織這些數字,0或正數。我被告知要完成打印出原始數組的程序,以及新的數組。C++:出現在輸出中的奇怪數字

出於某種原因,有組織的數組在某些空格中打印出隨機(TYPE:long)數字。我不確定爲什麼會出現這種情況。下面是我的代碼:

#include <iostream> 
#include <stdlib.h> 
#include <time.h> 

int main(int argc, const char * argv[]) { 
// insert code here... 
srand(time(NULL)); 
int numbers[20], final[20], first = 1, second = 1; 

std::cout << "Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.\n\n"; 
for(int i = 0; i < 20; i++){ 
    std::cout << "Enter number " << (i+1) << ": "; 
    //std::cin >> numbers[i]; 
    numbers[i] = (rand()%20 -10); 
    std::cout << numbers[i] << std::endl; 
} 

//Numbers lower than 0 
for(int i = 0; i < 20; i++){ 
    if(numbers[i] < 0){ 
     final[i] = numbers[i]; 
     first++; 
    } 
} 
//Numbers equal to 0 
for(int i = first; i < 20; i++){ 
    if(numbers[i] == 0){ 
     final[i] = numbers[i]; 
     second++; 
    } 
} 
//Numbers greater than 0 
for(int i = second; i < 20; i++){ 
    if(numbers[i] == 0){ 
     final[i] = numbers[i]; 
    } 
} 

std::cout << "This is your original array: "; 
for(int i = 0; i < 20; i++){ 
    std::cout << numbers[i]; 
    if(i != 19) 
     std::cout << ","; 
    std::cout << " "; 
    if(i == 19) 
     std::cout << std::endl << std::endl << std::endl; 
} 

std::cout << "This your new, organized, array: "; 
for(int i = 0; i < 20; i++){ 
    std::cout << final[i]; 
    if(i != 19) 
     std::cout << ","; 
    std::cout << " "; 
    if(i == 19) 
     std::cout << std::endl << std::endl << std::endl; 
} 

system("pause"); 
return 0; 
} 

我的輸出是這樣的:

Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0. 

Enter number 1: -6 
Enter number 2: 0 
Enter number 3: -4 
Enter number 4: -5 
Enter number 5: 0 
Enter number 6: -4 
Enter number 7: -5 
Enter number 8: -5 
Enter number 9: -8 
Enter number 10: 5 
Enter number 11: 0 
Enter number 12: -3 
Enter number 13: 5 
Enter number 14: -5 
Enter number 15: 7 
Enter number 16: 2 
Enter number 17: 9 
Enter number 18: 9 
Enter number 19: 3 
Enter number 20: 2 
This is your original array: -6, 0, -4, -5, 0, -4, -5, -5, -8, 5, 0, -3, 5, -5, 7, 2, 9, 9, 3, 2 


This your new, organized, array: -6, 1879110449, -4, -5, 0, -4, -5, -5, -8, 1, 0, -3, 1606416384, -5, 1606423158, 32767, 1606416416, 32767, 1606416416, 32767" 

預先感謝您所有誰答覆。對此,我真的非常感激。

+0

看起來像意外值的元素從未初始化。還應考慮完全在積極領域工作,並且僅在向用戶呈現時抵消10。 – bvj 2015-02-07 03:14:52

+0

謝謝你的偏移提示。我唯一擔心的是意外值在'final [10]'數組下初始化。我是否在for循環中初始化它們時出錯?它似乎是正確的...... – iProgramIt 2015-02-07 03:22:42

+0

只有最終的[i]元素被設置在數字[i] <= 0的地方。這在你的輸出中也很明顯。如果您發表評論//數字大於0,則表示您有複製粘貼錯誤 – bvj 2015-02-07 03:29:21

回答

0

你是不是把在正確的final陣列位置的數量,檢查有現在first被用來標明在下一元素應該放在最後一個數組的位置:

而且我刪除未使用的second變量

#include <iostream> 
#include <stdlib.h> 
#include <time.h> 

int main(int argc, const char * argv[]) { 
// insert code here... 
srand(time(NULL)); 
// changed first = 1 to first = 0 and eliminated second 
int numbers[20], final[20], first = 0; 

std::cout << "Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.\n\n"; 
for(int i = 0; i < 20; i++){ 
    std::cout << "Enter number " << (i+1) << ": "; 
    //std::cin >> numbers[i]; 
    numbers[i] = (rand()%20 -10); 
    std::cout << numbers[i] << std::endl; 
} 

//Numbers lower than 0 
for(int i = 0; i < 20; i++){ 
    if(numbers[i] < 0){ 
     // now it is put in final[first] instead of final[i] 
     final[first] = numbers[i]; 
     first++; 
    } 
} 
//Numbers equal to 0 
//changed i to start from 0 again 
for(int i = 0; i < 20; i++){ 
    if(numbers[i] == 0){ 
     final[first] = numbers[i]; 
     first++; 
    } 
} 
//Numbers greater than 0 
for(int i = 0; i < 20; i++){ 
    if(numbers[i] > 0){ // Yeah, here was the typo... replaced `==` with `>` 
     final[first] = numbers[i]; 
     // added this increment 
     first++; 
    } 
} 

std::cout << "This is your original array: "; 
for(int i = 0; i < 20; i++){ 
    std::cout << numbers[i]; 
    if(i != 19) 
     std::cout << ","; 
    std::cout << " "; 
    if(i == 19) 
     std::cout << std::endl << std::endl << std::endl; 
} 

std::cout << "This your new, organized, array: "; 
for(int i = 0; i < 20; i++){ 
    std::cout << final[i]; 
    if(i != 19) 
     std::cout << ","; 
    std::cout << " "; 
    if(i == 19) 
     std::cout << std::endl << std::endl << std::endl; 
} 

system("pause"); 
return 0; 
} 
+0

謝謝你的幫助!不幸的是,控制檯中的輸出仍然顯示奇怪的數字,即使有您的更改。是否有一個原因?再次感謝。 – iProgramIt 2015-02-07 03:33:53

+0

查看我的評論下面「你的代碼的另一個問題是if語句中的部分//數字大於0的拼寫錯誤。我會讓你爲你自己找出一個:)」 – tofi9 2015-02-07 03:36:09

+0

是的,有一個==代替>的數字大於0循環:),現在應該運行得很好。 – Roberto 2015-02-07 03:39:19

0

對於numbers陣列,final陣列的索引器應該從索引器(i)中獨立增加。換句話說:

int idxFinal = 0; // determines where on the 'array' index to insert next 

for(int i = first; i < 20; i++){ 
    if(numbers[i] < 0){ 
     final[idxFinal] = numbers[i]; 
     first++; 
     idxFinal++; 
    } 
} 

... 

//Numbers equal to 0 
for(int i = first; i < 20; i++){ 
    if(numbers[i] == 0){ 
     final[idxFinal++] = numbers[i]; 
     second++; 
    } 
} 

的另一個問題與您的代碼是一款//Numbers greater than 0內的if聲明一個錯字。我會讓你的身影,一個爲自己:)

+0

感謝您的快速響應!只是一個問題,什麼時候將變量'second'用於你將它放在你的代碼版本中的位置? – iProgramIt 2015-02-07 03:31:05

+0

如果要跟蹤分爲第一組(小於0)和第二組(多於0)的數量,則只需要變量「first」和「second」。我會初始化它們到零的初始值。如果這不是要求,那麼你不需要這兩個變量 – tofi9 2015-02-07 03:33:42

0

你的輸出越來越陌生號碼,因爲您的代碼不完全填充陣列(它打印出那些已經對這些存儲位置的內容)。

,不會產生代碼(你幾乎沒有):

  • 你應該通過整個數組的大小(0〜20)的迭代循環for(他們三個),請記住,在這些你試圖檢查值的循環,所以你需要經歷所有的值。
  • 根據您的計數器(first,second),而不是基於i,分配給您的最終陣列。再次,i只是用來遍歷數組。這會導致您的陣列在這裏出現漏洞。當您輸入if報表並且僅在其各自的職位(i)中時,您只將值分配給final
  • 您的if對於數字大於零的語句不正確,它會檢查是否等於。
  • 初始化firstsecond 0,而不是1

應該這樣做。作爲一個方面說明,你並不需要兩個單獨的計數器,firstsecond。你可以只使用一個,在你的第一個循環中增加它,然後在第二個循環中繼續增加它,等等。

+0

謝謝你的幫助!爲什麼我會迭代第三個for循環?我不知道這將如何改變控制檯的輸出...... – iProgramIt 2015-02-07 03:37:23

+0

你仍然需要在你的'final'數組中添加大於零的值。不過,不要分配給'final [i]'。繼續使用你的計數器,如'final [second ++]'或'final [first ++]',如果你第二次被拋棄的話。 – teealgo 2015-02-07 03:40:33