简体   繁体   中英

Need help understanding code

I am taking a C# class and I need help understanding the following code.

The code has an array which represents responses to a survey, with values 1 thru 10.

The output displays these ratings and the frequency of how many times a value was selected.

The following code is from my book, but I have modified it to just a basic example.

int[] responses = { 3, 2, 5, 6, 3, 5 , 4, 5, 5, 5};
int[] frequency = new int[7];


for (int answer = 0; answer < responses.Length; answer++)
   ++frequency[responses[answer]];

for (int rating = 1; rating < frequency.Length; rating++)
   Console.WriteLine(rating + ", " + frequency[rating]);

Console.Read();

How does the line ++frequency[responses[answer]]; work? In looking at this, if I take reponses[answer] the first time through the loop, this would represent responses[0] which would be a 3, correct? This is where I get confused, what does the ++frequency part of this line do?

frequency[responses[answer]] = frequency[responses[answer]] + 1;

EDIT: I think it's pretty unclear to write it like that. As a personal preference, I don't like using unary operations (++x, x++, etc) on elements that have lots of indexes present.

It adds one to the frequency at that location in the array.

For example, the frequency at position 3 (from your example) will be increased by one after that line executes.

EDIT: So, in more detail, when answer = 0, responses[0] = 3, so frequency[3] gets one added to it.

The ++ could very easily be at the end of the command as well. In other words,

++frequency[responses[answer]];


is the same thing (IN THIS CASE) as using

frequency[responses[answer]]++;

Let's break it down: As you point out, on the first pass responses[answer] will evaluate to "3"

So this then looks like ++frequency[3]

The ++ is incrementing the value of the array at index 3 by 1

Simple enough?

I should also point out that applying the ++ before the array rather than after it does effect how the incrementing is executed (although it doesn't effect the results of this code).

For instance:

int n = 2;
int j = ++n;
int k = n++;

What are j and k?

j will be 3, and k will also be 3. This is because if you place the ++ before, it evaluates it first. If you place it at the end, it evaluates it after the rest of the expression.

If it helps, think of ++frequency[] as "frequency = frequency + 1".

If the ++ operator comes before the variable, then the increment is applied before the statement is executed. If the ++ comes afterwards, then the statement is executed and then the variable is incremented.

In this case, it doesn't matter, since incrementing before or after doesn't impact the logic.

Since "responses[answer]" evaluates to a number, that line of code is incrementing the frequency entry at that array index. So the first time through, answer is 0, so responses[answer] is 3, so the frequency[3] box is getting incremented by 1. The next time through, the frequency[2] box is incremented... etc. etc. etc.

frequency is an array, where all elements are initialized to 0 (the default value for an int). The line ++frequency[responses[answer]] will increment the frequency element pointed out by the integer found at responses[answer] . By putting the ++ in front of frequency, the array element will be incremented before the resulting value is returned.

You can read more about the ++ operator here .

In cases like this it's often useful to rewrite the code as you walk it.

When answer = 0

  • ++frequency[responses[0]]
  • ++frequency[3] since responses[0] = 3
  • frequency now looks like { 0, 0, 0, 1, 0, 0, 0 }

When answer = 1

  • ++frequency[responses[1]]
  • ++frequency[2] since responses[1] = 2
  • frequency now looks like { 0, 0, 1, 1, 0, 0, 0 }

And so on.

It means increment the value at frequency[ 3 ]. Where 3 is the return result from responses[answer]. Similarly the next iteration would increment the value at frequency[ 2 ].

The ++ operator in C# when applied to an integer will increment it by one.

The specific line you're looking at, frequency is an array of integers with 7 elements. Which is sort of confusing, because the way you explained it in your code, it would appear that this code would break with any value in the responses array above 6.

That issue aside, basically it's incrementing whichever index of the array it's accessing. So in your example responses[0] would be 3. So this line would find the value of frequency[3] and increment it by 1. Since integer arrays are initialized with all values at zero, then after the first iteration, frequency[3] would be 1. Then if there was another 3 later in your responses array, frequency[3] would be incremented again (ie responses[4]).

I hope this helps you.

The goal of the code snippet seems to be to determine the number of times each response appears in the 'responses' array. So, for your example set, frequency[3] should be 5, frequency[5] should be 5, etc.

So, the line you are asking about takes the current element from the responses array, and increments the associated value in the frequency array by 1, to indicate that the particular value has been observed in responses.

Once the entire code snippet executes, the frequency array contains the number of times each element from 0 to 7 was observed in the responses array.

It is using the frequency array to count how many times each response was entered. You could have a counter for each answer:

int numberOfOnes = 0;
int numberOfTwos = 0;
// Etc...

But that would be ugly programming and not as easy or efficient. Using the frequency array allows you do not use an if/else if block or a switch and makes your code easier to read.

Another thing about that frequency array.

int[] frequency = new int[7]; 

This initializes all the integers in the array to 0, that's why you can just start off by incrementing it instead of seeing if it was the first time for that specific response and then initializing it with 1 or something of that nature.

Good luck with all the fun C# you have ahead of you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM