Help with understanding some code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joshna31
    New Member
    • Oct 2007
    • 3

    Help with understanding some code

    Hi there I have come accross some C code that I would like to be explained in very simple terms hope somebody can help me.
    [code=c]
    #include <stdio.h>
    #define response_size 10
    #define frequency_size 11

    int main()
    {
    int answer;
    int rating;

    int frequency[frequency_size] = {0};
    int responses[response_size] = {1,2,6,4,8,5,9, 7,8,10};
    for(answer=0; answer<response _size; answer++)
    {
    ++frequency[responses[answer]];
    }
    printf("%s%17s\ n","Rating ,"Frequency" );

    for(rating =1; raring <frequency_sixe ;rating ++)
    {
    printf("%6d%17d \n",rating,freq uency[rating]);
    }

    }
    [/code]
    10 students were asked to rate the food in a canteen on a scale of 1-10 where 1 is awful and 10 is excellent, Place the 10 responses in an integer array and summarize the result of the poll, count how many 1's 2's etc etc

    This piece of code "++frequenc y[responses[answer]];" is what i dont understand could someone please help







    }
    Last edited by sicarie; Oct 3 '07, 06:31 PM. Reason: Code tags
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    All it is doing is incrementing the frequency of a certain answer. it is getting the choice from the answer array, 0-10, then incrementing the frequency at that place in the array, eg. each place in frequency array represents a different answer. If everyone answered 5, then the array would look like this in the end

    | 0 | 0 | 0 | 0 | 0 | 10 | 0 | 0 | 0 | 0 | 0 |
    where the 10 is at frequency[5]

    if everyone picked a different answer then it would look like this

    | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |

    Comment

    • joshna31
      New Member
      • Oct 2007
      • 3

      #3
      Originally posted by mschenkelberg
      All it is doing is incrementing the frequency of a certain answer. it is getting the choice from the answer array, 0-10, then incrementing the frequency at that place in the array, eg. each place in frequency array represents a different answer. If everyone answered 5, then the array would look like this in the end

      | 0 | 0 | 0 | 0 | 0 | 10 | 0 | 0 | 0 | 0 | 0 |
      where the 10 is at frequency[5]

      if everyone picked a different answer then it would look like this

      | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |

      Hi there thanks for the response.Increm enting frequency i understand--could you explain how does the tallied value get stored in frequency array.Which variable is it using or rather how is it using it. The frequency array has no values right it is using the subscript response[answer] to point to the value it wants to tally, but what is holding that incremented value or rather how/where is it holding that value.

      Comment

      Working...