如何检查输入的次数

时间:2015-11-20 21:49:36

标签: c++ arrays

我正在尝试解决我收到的任务单上的一个问题,以帮助我进一步理解我班上的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;
}

3 个答案:

答案 0 :(得分:1)

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

在你的第一个循环中(验证后)。一旦你这样做,你不需要第二个循环来计算结果。

首先获取input[i]是什么,然后使用它来修改(input[i]-1)数组中的COUNT位置,这从内到外。如果用户在第一次循环运行时输入4,则i == 0input[i] == 4。由于数组是从0开始的,因此它将增加COUNT[input[i]-1],在这种情况下为COUNT[4-1] == COUNT[3]

在您的初始循环运行后,1的数字将在COUNT[0]中,2的数字将在COUNT[1]中,依此类推。

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

输出:

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

答案 2 :(得分: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个输入,只检查单个值。