levi.ackerman1732's blog

By levi.ackerman1732, history, 5 years ago, In English

Hello all

1201A - Important Exam

My solution, was to store the number of responses(among A,B,C,D,E) for each question and choose the maximum among them and multiply with the marks awarded. I am getting a Runtime error. I am not able to figure out why.

The part of code where it's failing is:

s is string, j is the question number

	temp = (int)(s[j]-'A');

	v[j][temp]++;

I have encountered this problem quite a few times, but have avoided every time somehow. Please do let me know what is wrong with this.

My solution: 70953260

I am extremely sorry if I am asking something that is fairly obvious. Thank you for your time.

  • Vote: I like it
  • +13
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 2   Vote: I like it +13 Vote: I do not like it

You are accessing the jth index of v where j < m. You declared v to have n elements. But there is no restriction that says m <= n. So what happens if m > n? Undefined behavior.