1

I'm a little bit confused with std::array bounds checking.

Here's the code:

    const size_t studentResponses = 10; 
    const size_t surveyBound = 6;

    std::array<unsigned int, studentResponses> students{ 1, 3, 5, 5, 5, 4, 5, 4, 2, 2 };
    std::array<unsigned int, surveyBound> survey{}; //initializes it to '0'

    for (size_t answer = 0; answer < students.size(); ++answer) {
        ++survey[students[answer]];
    }

    std::cout << "Rating" << std::setw(12) << "Frequency" << std::endl;
    
    for (size_t rating = 1; rating < survey.size(); ++rating) {
        std::cout << std::setw(6) << rating << std::setw(12) << survey[rating] << std::endl;
    }

Output:

Rating   Frequency
 1           1
 2           2
 3           1
 4           2
 5           4

I've never used an array as a counter for another array. From what I understand by reading the book is that the value at 'n' element of students will become the value of survey's elemen, but when the output is displayed, that's where my confusion is.

  1. How is the array, survey, keeping a count of how many times of the value at of element in students is counted?
  2. Since the array survey is initialized to 6, how can it get the 10 values from students?

My prof didn't really explain and just read it off a PowerPoint, so I'm trying to learn and understand it myself.

1
  • The code above implements an algorithm called counting sort. You may find it helpful to look that up independently to get a better sense of how it works. Commented Jun 25, 2020 at 16:39

1 Answer 1

2

How is the array, survey, keeping a count of how many times of each element in students is counted?

By using this statement:

++survey[students[answer]];

Note that the students array must contain only values between 0 and 5 (inclusive). Then the value of students[answer] is used as an index into survey. The value at that index is then incremented.

Since the array survey is initialized to 6, how can it get the 10 values from students?

There are not 10, but only 6 positions in survey. Instead, the sum of all the values in survey will be equal to 10.

Sign up to request clarification or add additional context in comments.

4 Comments

AHHHH. This makes a lot of sense now. Thank you very much kind sir!
No that was a very thorough answer. One more quick question though, it's only inclusive because it's not initialized? I'm assuming initialization of it would render this code useless. I'm more of a student who needs to practice the code, visualize the code and then understand.
No, survey is initialized. That's what the {} is for (in fact, if it's not initialized, the ++ is broken). The 0 to 5 inclusive is because survey only has 6 elements. If you use any other index, the program is broken (it has undefined behavior).
Sorry for being unclear, you answered what I meant haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.