Doesn't that title just remind you of the Princess Bride, when the lispy priest says, "...Mawiage. That Dweam, within a Dweam." Okay, so consider this problem. You see, in order for me to know I really "Get" it, I have to write it out.
Story: I have run a survey to determine how often people eat find what they are looking for on a website. I have a database full of 100 responses, and let's say, for the sake of simplifying this story, that these 100 responses are pulled out of a database into my array which is 100 response long. I won't go into how they got there, they are just there. (Of course, these can be manually entered, but keep in mind that if the number of initialized values do not match the number in the array, there may be problems. If there are fewer values, the rest will be set to zero. If there are more - well, just make sure there aren't more values than the size of the array.)
Now, the survey asked the following question: "On a scale of 1-10, how likely are you to recommend this website to another person?" After all, isn't that the ultimate representation of satisfaction; that you will recommend the website to someone else? Okay, the responses are recorded thus:
10 very likely - - - - 5 neutral - - - - 1 not at all likely
So next we need an array of ten elements that represent the possible responses. For each element, I now want to know what the tally was for each response.
First I need to set up and initialize these two arrays, right? Array variables HAVE to be type const, but not only do the arrays need to BE type const, they also should TAKE a const variable, and here is why: If the size of the array ever needs to change, I can just change the size of the array at the VERY BEGINNING of the program, and not throughout the function. Saves time in the long run, capisce?
// THESE ARE THOSE VALUES THAT DETERMINE THE SIZE OF THE ARRAY:
const int surveyAnswerSize = 100; // size of surveyAnswer array
const int tallySize = 10; // size of tally array
// HERE IS THE FIRST ARRAY - PRETEND THERE ARE 100 RESPONSES AND THAT THESE WERE FILLED IN FROM A DATABASE:
const int surveyAnswers[ surveyAnswerSize ] = { 10, 3, 4, 7, 9, 8, ........ 9, 8, 10 };
// HERE IS THE SECOND ARRAY - THIS WILL KEEP TRACK A TALLY OF HOW MANY OF EACH RESPONSE THERE WERE:
// initialize tally counters to 0 - remember, the number of elements in
// the array was decided by tallySize above. Why set this to zero? because
// we don't know any information about any of the ten responses yet.
int tally[ tallySize ] = { 0 };
// THIS IS OUR FOR LOOP THAT WILL WALK THROUGH EACH OF THE SURVEY
// ANSWERS AND INCREMENT THE TALLY FOR EACH POSSIBLE RESPONSE. NOTICE
// IN OUR FOR LOOP THAT WE DON"T USE THE ACTUAL NUMBER OF SURVEYANSWERS.
for ( int eachAnswer = 0; eachAnswer < surveyAnswer; eachAnswer++ )
tally[ surveyAnswer[ eachAnswer ] ]++;
So what happens in the above code is this:
starting with eachAnswer, this will be zero, so surveyAnswer will run back and see what the survey value was for element 0. Oh, looks like that will be equal to 10! Okay, so then tally takes a look at that result - - 10, hmmm? Looks like we have one count for the response 10 (which in our world means, highly likely - they will be highly likely to recommed the website.
Well, going back through the for loop, since we haven't yet reached the total initialized for surveyAnswer, eachAnswer has been incremented and now equals 1. What is contained in element 1 of surveyAnswer? Let's go see! Oh, looks like that will equal 3. Which would be on the less satisfied end of the response scale. Tally accepts that three and adds one count for response 3. Again through the for loop, eachAnswer now is incremented to 2. What value does element 2 have in the array surveyAnswer??? Ta-da! It looks like 4. So tally now adds one count for the response 4. At this point if we were to print out the results, it would look like this:
(Number of responses for each possible answer:)
10 = 1
4 = 1
3 = 1
Well, maybe you don't understand it any better, but I think I've got it, Watson!
Tuesday, October 20, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment