Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建数组_C++ - Fatal编程技术网

C++ 创建数组

C++ 创建数组,c++,C++,编写一个程序,让用户在数组中输入10个数字。然后,程序应显示数组中存储的最大数字as和最小数字 我对上一次考试和期末考试的这个问题感到非常困惑。任何帮助都将不胜感激!这是我在测试中得到的,得到了3/15分,代码几乎完全错了,但是如果需要,我可以发布我得到的,谢谢!为了创建数组,我至少可以开始,像这样 #include <iostream> using namespace std; int main() { int array(10); // the array wi

编写一个程序,让用户在数组中输入10个数字。然后,程序应显示数组中存储的最大数字as和最小数字

我对上一次考试和期末考试的这个问题感到非常困惑。任何帮助都将不胜感激!这是我在测试中得到的,得到了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-
#包括
使用名称空间std;
int main()
{
int数组(10);//用户将输入的包含10个数字的数组
cout阵列;
int minimest=0;//说明int数据类型和实际最小数
int max=0;//说明int数据类型和实际最大数
//-这两项都从0开始,以显示准确的结果-

然后在我的测试中,我开始使用for循环,从那以后它变得很混乱,所以我想我这里的大问题是如何以最好的方式比较/找到最小和最大的数字。我还在大学读计算机科学1,所以我们保持它相当简单,或者说我喜欢。我们也知道二进制搜索和其他搜索方法,如果这两种方法中的任何一种都是编写代码的好方法。谢谢!

从正确声明数组开始。
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;
int数组[10];
cout数组[x];
}
int最小=数组[0];
int最大=数组[0];
对于(int x=1;x<10;x++)
{
if(数组[x]<最小值)
{
最小=数组[x];
}
else if(数组[x]>最大值)
{
最大=数组[x];
}
}

cout在这种情况下,实际上不必进行二进制搜索,也不必搜索数组。由于您将直接从用户接收输入,因此您可以在遇到最小值和最大值时跟踪它们,如下所示。你知道你收到的第一个数字是最小值和最大值。然后你将得到的下一个数字与这些数字进行比较。如果它较大或较小,则分别将其存储为最大值或最小值。然后等等。我包含了将数字存储在数组中、检查错误以及将数组输出回用户的代码,但由于时间有限,在考试中可能不需要这样做。我把它作为一点额外的信息给你

#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;
}
#包括//isdigit、错误检查所需
#include//atoi所需,将文本转换为int
#包括//cout、cin、用户输入和输出所需
#包含//字符串类型所需,更易于操作文本
int main()
{
//我们需要用户提供的号码数。
int maxNumbers=10;
//在检查错误之前存储用户输入的变量
std::字符串用户输入;
//存储用户输入的数组
int userNumbers[maxNumbers];
//存储最大和最小的数字
int max,min;
//计数器变量,i用于程序中的两个主循环,
//而j在循环中用于错误检查
int i;
无符号整数j;
//提示用户输入。

STD::你需要对代码进行解释,以及代码如何对问题做出贡献。你的代码不会编译,因为C++不支持可变长度ARRAY,它用来测试它是否会编译,并且它在那里工作。会有一些东西吗?比如<代码> int * USER数=新的int [Max Ne];< /Cord>工作?