2015-11-20 42 views
0

我試圖解決我收到的任務表上的一個問題,以幫助我進一步瞭解我的課程中的C++代碼。如何檢查輸入數字的次數

的問題是(我引述):

寫一個程序:

  • 要求用戶輸入1和5之間10個號碼爲一個陣列並在屏幕上顯示陣列
  • 創建第二個大小爲5的數組,並用零填充
  • 計算第一個數組中已輸入多少個1s,2s,... 5s並將此數字存儲在第二個數組中。
  • 顯示第二個數組,如下例所示。

問題是如何去檢查數字輸入的次數。我正在考慮一個for循環,但是我寫這個循環的方式根本上是不正確的,所以我發現自己正在努力看到我所遇到的錯誤。也許我錯過了一些簡單的東西?任何幫助都會很棒。

這是我的(可怕的)for循環嘗試,所以你可以看到我的錯誤。

#include <iostream> 
#include <windows.h> 

using namespace std; 

int main() 
{ 
    int input[10]; 
    const int MAX_NO = 5; 
    int COUNT[5] = { 0,0,0,0,0 }; 
    int count = 10; 

    for (int i = 0; i < count; i++) 
    { 
     cout << "Please enter a number for value " << i + 1 << " :"; 
     cin >> input[i]; 

     while (input[i] < 1 || input[i] > 5) 
     { 
      cout << "Error: Enter another number between 1 and 5: "; 
      cin >> input[i]; 
     } 
    } 

    cout << endl << "You entered "; 

    for (int i = 0; i < count; i++) 
    { 
     cout << input[i] << " "; 
    } 

    cout << "\n"; 
    // show how many times 1 number appears 

    for (int i = 1; i <= 5; i++) 
    { 
     if (input[i] == i) 
     { 
      COUNT[i]++; 
     } 
    } 


    for (int i = 0; i < MAX_NO; i++) 
    { 
     cout << i + 1 << " appears " << COUNT[i] 
      << " times in the input" << endl; 
    } 
    cout << endl; 

    system("pause"); 
    return 0; 
} 

回答

1

COUNT[ input[i]-1 ]++; 

在你的第一個循環(驗證後)。一旦你這樣做了,你不需要第二個循環來計算結果。

這從內到外首先得到什麼input[i],然後用它來修改COUNT陣列中的(input[i]-1)'th位置。如果用戶在循環的第一次運行中輸入4,則i == 0input[i] == 4。由於數組是從0開始的,因此它將增加COUNT[input[i]-1],在這種情況下它是COUNT[4-1] == COUNT[3]

在您的初始循環運行後,1的數量將在COUNT[0]之間,2的數量將會在COUNT[1]等等中。

+0

我刪除了我的消息,純粹是因爲我沒有正確讀取你的信息。因爲這是漫長的一天,非常感謝你在編輯它並解釋它之後,我更好地理解了你的邏輯。是的,我可以看到你是如何做到的。謝謝。 – Collwyr

0
for (int i = 1; i <= 5; i++) 
{ 
    if (input[i] == i) 
    { 
     COUNT[i]++; 
    } 
} 

讓我們來看看它在做什麼。它從檢查input[1]開始。 (這應該是input[0],因爲數組索引從0開始)。然後它檢查input[1]是否等於1.如果是,則它增加COUNT[1]。 接下來,它會檢查input[2]。然後它檢查input[2]是否等於2.如果是,則增加COUNT[2]。等到它經歷了input[5]
你看到這個問題嗎?你只是檢查前5個輸入,只檢查一個值。

+0

是的,我知道我做錯了什麼,我在描述中陳述它,我只是認爲我最好留下它,所以人們知道我現在正在考慮在這個時刻如何去做,甚至如果我知道它的不正確 – Collwyr

+0

你說你知道這個循環是錯誤的,它是。但這是否更好地說明了它爲什麼錯了?您需要檢查輸入中的每個值(從0到9的循環)並遞增正確的位置。 – fuzzything44

+0

是的,你解釋它的方式是我得出的結論,當我將斷點放入並按照代碼運行時。 – Collwyr

1
#include <iostream> 
#include <windows.h> 
using namespace std; 
int main() 
{ 
//declare a constant values 
int input[10]; 
int count = 10; //all constant MUST be in capital letters 

//second array filled with zeros 
const int MAX_NO = 5; 
int COUNT[5] = { 0, 0, 0, 0, 0 }; 

//ask user for 10 input values 
for (int i = 0; i < count; i++) 
{ 
    cout << "Please enter a number for value " << i + 1 << " :"; 
    cin >> input[i]; 
    //check if input numbers are between 1 and 5 inclusive 
    while (input[i] < 1 || input[i] > 5) 
    { 
     cout << "Error: Enter another number between 1 and 5: "; 
     cin >> input[i]; 
    } 
    /* show how many times 1 number appears. 
    this section should be in the main loop which would enable the program to check how many times a 
    number is entered so that it is stored in the second array. changed i to secondCount because this is the counting index of the second array not the first which you've called i (one of the reason you'd all zero as output when u ran your code)*/ 

    for (int secondCount = 1; secondCount <= MAX_NO; secondCount++) 
    { 
     if (input[i] == secondCount) 
     { 
      COUNT[secondCount-1]+= 1; //use minus 1 from i and increment. += 1 is the same as COUNT++ 
     } 
    } 
} 

//display number entered in the first array 
cout << endl << "You entered "; 

for (int i = 0; i < count; i++) 
{ 
    cout << input[i] << " "; 
} 

cout << "\n"; 

//display how many times a number is entered. 

for (int secondCount = 0; secondCount < MAX_NO; secondCount++) 
{ 
    cout << secondCount + 1 << " appears " << COUNT[secondCount] 
     << " times in the input" << endl; 
} 
cout << endl; 

system("pause"); 
return 0; 
} 

OUTPUT:

Please enter a number for value 1 = 1 
Please enter a number for value 2 = 1 
Please enter a number for value 3 = 1 
Please enter a number for value 4 = 2 
Please enter a number for value 5 = 3 
Please enter a number for value 6 = 2 
Please enter a number for value 7 = 4 
Please enter a number for value 8 = 4 
Please enter a number for value 9 = 3 
Please enter a number for value 10 = 2 

You entered: 1 1 1 2 3 2 4 4 3 2 

1 appears 3 times in the input 
2 appears 3 times in the input 
3 appears 2 times in the input 
4 appears 2 times in the input 
5 appears 0 times in the input