GATE Computer ScienceModerateGATE CS (PYQ pattern)

struct N { int v; int next; }; struct N a[6] = {{10,3},{20,5},{30,-1},{40,1},{50,2},{60,4}}; int i = 0, s = 0, k = 1; while (i != -1) { s += a[i].v * k; k++; i = a[i].next; } What is s, and what is the order of visited indices?

  1. A. 780; [0, 3, 1, 5, 4, 2]
  2. B. 820; [0, 1, 2, 3, 4, 5]
  3. C. 880; [2, 4, 5, 1, 3, 0]
  4. D. 820; [0, 3, 1, 5, 4, 2]

Correct answer: D. 820; [0, 3, 1, 5, 4, 2]

Follow the next fields from index 0: [0, 3, 1, 5, 4, 2]. Weighted sum is 10*1 + 40*2 + 20*3 + 60*4 + 50*5 + 30*6 = 820.

More Programming and Data Structures (GATE) questions

Exams that ask this topic