3

I am writing a C++ program that prompts the user for an input and then keeps track of how many times that input is entered. I am currently using a do-while loop and a switch statement. The part I am having trouble with is the switch statement. I can't figure out how to keep track of how many times an input is entered. For example:

Enter Value: 4
Enter Value: 4
Enter Value: 4
Enter Value: 3
Enter Value: 3

// I then want the program to be able to know and then eventually output, how many times the number '4' and '3' were entered. I thinking possibly using some sort of increment counting form, but not 100% sure. Thanks!

4

3 に答える 3

7

You'll probably want to use a std::map<int,int>. Here's why.

Let's look at alternatives, starting with the obvious:

int count0;
int count1;
int count2;
int count3;
int count4;

...

switch(input) {
case 0: ++count0; break;
case 1: ++count1; break;
case 2: ++count2; break;
case 3: ++count3; break
case 4: ++count4; break;
}

This does what you ask: you evaluate the input, and keep track of the number of times that specific input has been seen. This form does suffer from many problems:

  • It requires one line of source code for each alternative. This becomes a problem when the user can enter any value, say, from 0 to 10,000!
  • It has duplicate, virtually identical lines.
  • It has many variables, each of which has to be entered independently, but uses identically.

We can reduce the variable count by specifing an array:

int count[5];
...
switch(input) {
case 0: ++count[0]; break;
case 1: ++count[1]; break;
case 2: ++count[2]; break;
case 3: ++count[3]; break;
case 4: ++count[4]; break;
}

This still suffers from too many almost-but-not-quite identical lines of code. Let's try to get rid of the switch statement:

int count[5];
...
++count[input];

Ah, now we are getting somewhere! By eliminating the switch statement, we have one easily-maintained line of code. But what if the user (accidentally or maliciously) enters a 6? Then we will increment count[6], which does not exist. This is a Bad Thing. We could increase the size of the array:

int count[50000];
...
++count[input];

Now we are safe from the user. If he enters a 6, the Bad Thing no longer happens. Uh-oh, what about if the user enters 51000? We will increment count[51000] which does not exist. It should be obvious that we can't win this game -- for any number we choose, the user might choose that number plus 1.

Even if we could win, we'd still lose. If we are only asking the user to enter a few numbers, then we will have wasted the other 49,997 entries in the arary.

Fortunately C++ has a data structure that we can use which:

  • can take arbitrary numbers as its range, and
  • is space-efficient (compared to a large wasted array).

That data structure is called a map:

std::map<int,int> count;
...
++count[input];

A map is sort of like an array, but grows itself in a special way. Only the entries that we use are ever allocated, and every entry that we use is automatically allocated.

于 2011-04-10T23:05:32.853 に答える
5
std::map<int, int> frequency;
int value_entered_by_user = f();

frequency[value_entered_by_user]++;
于 2011-04-10T22:46:36.620 に答える
0

If your range of input values is limited, you can use an array. Each element of the array represents an input value. Initialize the elements to 0 at the beginning and increment the appropriate element when its corresponding input value is entered.

于 2011-04-10T22:42:50.203 に答える