创建一个数组

时间:2015-12-13 06:02:15

标签: c++

编写一个程序,让用户输入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,所以我们保持它非常简单,或者我喜欢。我们也知道二进制搜索和另一种搜索方法,如果其中任何一种都是在这里用来编写代码的好方法。谢谢!

2 个答案:

答案 0 :(得分:0)

首先正确声明一个数组。 int array(10)初始化一个名为array的整数变量,其值为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循环组合成一个循环。这是一项优化练习,我将留给您。

答案 1 :(得分:-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;
}