2015-12-13 59 views
-1

編寫一個程序,讓用戶在數組中輸入10個數字。程序應該顯示數組中存儲的最大數字和最小數字。創建數組

我對之前考試的這個問題感到非常困惑,並且會在最後的考試中。任何幫助,將不勝感激!這就是我在測試中得到的結果,得到了3/15分,代碼幾乎完全錯誤,但是如果有必要,我可以發佈我的東西,謝謝!爲了創建數組,我至少可以開始這樣,所以這樣?

#include <iostream> 

using namespace std; 

int main() 

{ 
     int array(10); // the array with 10 numbers, which the user will enter 
cout << "Please enter 10 numbers which will be stored in this array" << endl; 
cin >> array; 

int smallest=0; //accounting for int data type and the actual smallest number 
int largest=0; //accounting for int data type and the actual largest number 
       //-both of these starting at 0 to show accurate results- 

,然後在我的測試中,我開始使用for循環,並從那裏出來了凌亂的,所以我在這裏,我覺得最大的問題是如何真正比較/發現的最小和最大數,在最好的可能的方式。我也只是在大學計算機科學1,所以我們保持它很簡單,或者我喜歡。我們也知道二進制搜索和其他一種搜索方法,如果其中任何一種都是使用這種方法編寫代碼的好方法。謝謝!

回答

0

首先正確地聲明一個數組。 int array(10)初始化命名的數組具有值10.一個整數變量(等於說int array = 10

如下您申報10個整數數組:

int array[10]; 

無論如何,兩個簡單的循環和你做。

int array[10]; 
cout << "Enter 10 numbers" << endl; 
for (int x = 0; x < 10; x++) 
{ 
    cin >> array[x]; 
} 

int smallest=array[0]; 
int largest=array[0]; 

for (int x = 1; x < 10; x++) 
{ 
    if (array[x] < smallest) 
    { 
     smallest = array[x]; 
    } 
    else if (array[x] > largest) 
    { 
     largest = array[x];   
    } 
} 

cout << "Largest: " << largest << endl; 
cout << "Smallest: " << smallest << endl; 

實際上,您可以將上述兩個for循環合併爲一個循環。這是一個優化的練習,我將留給你。

+0

非常感謝! @selbie – kdrumz

-1

在這種情況下,您實際上不必執行二分搜索或搜索數組。由於您將直接從用戶那裏接收輸入,因此您可以跟蹤您遇到的最小值和最大值,如下所示。你知道你收到的第一個數字是最小值和最大值。然後你比較下一個你得到的數字和那些數字。如果它更大或更小,則分別將其存儲爲最大值或最小值。等等。我包含了將數字存儲在數組中的代碼,以檢查錯誤並將數組輸出回給用戶,但由於時間有限,在考試中這可能不是必需的。我把它作爲一點點額外的信息給你。

#include <cctype> // required for isdigit, error checking 
#include <cstdlib> // required for atoi, convert text to an int 
#include <iostream> // required for cout, cin, user input and output 
#include <string> // required for string type, easier manipulation of text 

int main() 
{ 
    // The number of numbers we need from the user. 
    int maxNumbers = 10; 

    // A variable to store the user's input before we can check for errors 
    std::string userInput; 

    // An array to store the user's input 
    int userNumbers[maxNumbers]; 

    // store the largest and smallest number 
    int max, min; 

    // Counter variables, i is used for the two main loops in the program, 
    // while j is used in a loop for error checking 
    int i; 
    unsigned int j; 

    // Prompt the user for input. 
    std::cout << "Please enter " << maxNumbers << " numbers: " << std::endl; 

    // i is used to keep track of the number of valid numbers inputted 
    i = 0; 

    // Keep waiting for user input until the user enters the maxNumber valid 
    // numbers 
    while (i < maxNumbers) 
    { 
    // Get the user's next number, store it as string so we can check 
    // for errors 
    std::cout << "Number " << (i+1) << ": "; 
    std::cin >> userInput; 

    // This variable is used to keep track of whether or not there is 
    // an error in the user's input. 
    bool validInput = true; 

    // Loop through the entire inputted string and check they are all 
    // valid digits 
    for (j = 0; j < userInput.length(); j++) 
    { 
     // Check if the character at pos j in the input is a digit. 
     if (!isdigit(userInput.at(j))) 
     { 
     // This is not a digit, we found an error so we can stop looping 
     validInput = false; 
     break; 
     } 
    } 

    // If it is a valid number, store it in the array of 
    // numbers inputted by the user. 
    if (validInput) 
    { 
     // We store this number in the array, and increment the number 
     // of valid numbers we got. 
     userNumbers[i] = atoi(userInput.c_str()); 

     // If this is the first valid input we got, then we have nothing 
     // to compare to yet, so store the input as the max and min 
     if (i == 0) 
     { 
     min = userNumbers[i]; 
     max = userNumbers[i]; 
     } 
     else { 
     // Is this the smallest int we have seen? 
     if (min < userNumbers[i]) 
     { 
      min = userNumbers[i]; 
     } 

     // Is this the largest int we have seen? 
     if (max < userNumbers[i]) 
     { 
      max = userNumbers[i]; 
     } 
     } 

     i++; 
    } 
    else 
    { 
     // This is not a valid number, inform the user of their error. 
     std::cout << "Invalid number, please enter a valid number." << std::endl; 
    } 
    } 

    // Output the user's numbers to them. 
    std::cout << "Your numbers are: " << userNumbers[0]; 
    for (i = 1; i < maxNumbers; i++) 
    { 
    std::cout << "," << userNumbers[i]; 
    } 
    std::cout << "." << std::endl; 

    // Output the min and max 
    std::cout << "Smallest int: " << min << std::endl; 
    std::cout << "Largest int: " << max << std::endl; 
    return 0; 
} 
+0

你需要對代碼做一些解釋,以及你的代碼是如何導致這個問題的。 –

+0

你的代碼不會編譯,因爲C++不支持變長數組 – texasbruce

+0

我正在使用[http://cpp.sh/](http://cpp.sh/)來測試它是否會編譯,並且它工作在那裏。會像'int * userNumbers = new int [maxNumbers];'工作嗎? – hargasinski