Collwyr
Collwyr

Reputation: 3

How to check how many times a number has been entered

I'm trying to solve one of the questions on a task sheet I've been received, to help me further in my understanding of C++ code from my class.

The question is (and I quote):

Write a program that:

The problem is how to go about checking how many times a number was entered. I was thinking of a for loop, but the way I wrote it is fundamentally incorrect, so I find myself struggling to see the mistake I am having. Perhaps I am missing something simple? Any help would be great.

Here is my (terrible) for loop attempt, so you can see my error.

#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;
}

Upvotes: 0

Views: 11518

Answers (3)

Jeff
Jeff

Reputation: 125

#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;
}

OUTPUT:

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

Upvotes: 1

jeff carey
jeff carey

Reputation: 2373

Put

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

in your first loop (after validation). Once you do that, you don't need a second loop to tally up the results.

This works from the inside out by first getting what input[i] is, then using it to modify the (input[i]-1)'th location in the COUNT array. If the user enters 4 on the first run of the loop, then i == 0 and input[i] == 4. Since arrays are 0-based, it will increment COUNT[input[i]-1] which in this case is COUNT[4-1] == COUNT[3].

After your initial loop runs the number of 1's will be in COUNT[0], the number of 2's will be in COUNT[1] and so on.

Upvotes: 1

fuzzything44
fuzzything44

Reputation: 701

for (int i = 1; i <= 5; i++)
{
    if (input[i] == i)
    {
        COUNT[i]++;
    }
}

Let's examine what this is doing. It starts by checking input[1]. (This should be input[0] as array indices start at 0). Then it checks if input[1] is equal to 1. If it is, then it increments COUNT[1]. Next, it checks input[2]. Then it checks if input[2] is equal to 2. If it is, it increments COUNT[2]. And so on until it has gone through input[5].
Do you see the problem with this? You're only checking the first 5 inputs and only checking them against a single value.

Upvotes: 0

Related Questions