2015-04-03 88 views
1

我有一項任務,要求程序從用戶輸入數組中讀取20個數字。 條件要求值在10-100之間且不重複。我也只允許使用一個包含20個元素的數組。但是,它不應該提示用戶,並且不會存儲該值;最後該程序必須打印出唯一的用戶值。 正確的結果,例如:使用基於C++ 11範圍的循環驗證數組值? [沒有載體]

input = 9 10 15 15 15 0 
output = 10 15 
//this is a small example with a 6 element array instead of 20 

當我測試程序中,我只得到

input: 9 10 15 15 15 0 
output: 10 15 15 15 
//this is a small example with a 6 element array instead of 20 

我使用基於範圍的循環來檢查值,設定值寫的代碼如果它不符合條件,則爲0。所以不是零的東西不會被打印出來。我已經通過對棧溢出所有的問題消失了,我無法找到一個答案,我具體問題:

  • 如何初始化所有的數組元素使用類構造函數爲零。
  • 使其成爲「靜態」,以便當我運行另一個函數時,先前的數組值是全局的,並從用戶輸入中維護。
  • 我創建的循環看起來有點不對,但看起來很完美。我檢查了我的同學,他們也同意。

    //arrayinput.h 
    #include <array> 
    #include <string> 
    
    class arrayelimination 
    { 
    public: 
        const static size_t limit = 20; 
        arrayelimination(); 
        void inputArray(); 
        void displayArray(); 
    
    private: 
        std::array < int , limit > store; 
        int userinput; 
    }; 
    
    
    
    //arrayinput.cpp 
    #include <iostream> 
    #include <array> 
    #include "arrayinput.h" 
    using namespace std; 
    
    arrayelimination::arrayelimination() 
    { 
    array < int , limit> store = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
    } 
    
    
    void arrayelimination::inputArray() 
    { 
    
    for (size_t i = 0; i < store.size(); i++) 
    { 
        cout << "Enter number between 10-100 for array box [" 
         << i << "]: " ; 
        cin >> userinput; 
    //check if numbers is between 10-100 
         if (userinput >= 10 && userinput <= 100) 
        { 
         //MOST LIKELY ERROR check if number has previously been used. 
          for (int &check : store) 
          { 
            if (check != userinput) 
           { 
            store[i] = userinput; 
            break; 
           } 
           else 
            store[i] = 0; 
    
          } 
        } 
    
        //output if number isn't between 10-100 
    else 
        store[i] = 0; 
    
    } 
    } 
    
    void arrayelimination::displayArray() 
    { 
         cout << "Your unique array numbers stored include...\n"; 
        //output all the unique numbers that the user inputted. 
    for (size_t j = 0; j < 20; j++) 
    { 
    //if the value is NOT 0, output. 
        if (store[j] != 0) 
        { 
         cout << "array[ " << j << " ] = " << store[j] << "\n"; 
        } 
    } 
    } 
    

當我測試的程序,我只得到

input: 10 15 15 15 2 0 0 0 0 0 0 0 ... 0 
output: 10 15 15 15 

概念其設置爲零的作品,但重複的值不是唯一的。

我必須使用面向對象的設計作爲這項任務的要求。我接近死路我真的不知道這是如何工作。請幫幫我。

PS:我的壞我忘了提,我只允許使用一個陣列

回答

1

任何問題只是標準的算法?

int values[20]; 

// 1) read them in 
for (int& v : values) { 
    std::cin >> v; 
} 

// 2) sort/uniq 
std::sort(std::begin(values), std::end(values)); 
auto last = std::unique(std::begin(values), std::end(values)); 

// 3) print them 
std::for_each(values, last, [](const int v){ 
    std::cout << v << " "; 
}); 
+0

對不起,我忘了提我可以只使用一個陣列,以僅具有20個元素執行的任務 – 2015-04-03 20:39:50

+0

**一個陣列。所以對不起,我應該檢查所有條件 – 2015-04-03 20:43:29

0

你讓這個過於複雜。

  1. 創建空向量。甚至不要填充/調整它的大小。
  2. 用push_back添加新元素。根據需要添加儘可能多的。
  3. 使用std :: sort對數組進行排序。
  4. 遍歷數組,僅在以下情況下打印元素:
    4.1。它是第一個元素[i == 0]。
    4.2。或者它不等於之前的元素。

你也可以用基於範圍的循環做同樣的事情。

2

你的問題不是range-based for loop本身的結構,而是你的條件是檢查你的輸入值是否唯一。

在這個代碼塊:

for (int &check : store) 
{ 
    if (check != userinput) 
    { 
     store[i] = userinput; 
     break; 
    } 
    else 
     store[i] = 0; 
} 

您設置的userinput值遇到不匹配的元素。因此,即使userinput與數組中稍後的值匹配,第一個不匹配的元素也會導致您設置userinput。你需要確定的是userinput匹配數組中的NO元素。

例如:

for (int &check : store) 
{ 
    if(check == userinput) 
    { 
     store[i] = 0; 
     break; 
    } // a match is found so set the value to 0 and stop 
    else 
    { 
     store[i] = check; 
    } 
} 
+0

我試過了,結果發生了什麼是我猜這些搞砸值 輸入:10 15 15'輸出:-858993460 -858993460 -858993460' – 2015-04-03 22:22:26

相關問題