This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 10: Applied problem solving

Subscripts to linear indices (3D)

face Josiah Wang

For your final challenge, implement a three-dimensional version of the first version of the problem.

That is, convert (x_1, x_2, x_3) to s.

Remember that the coordinates start from 0.

Convert 3D subscripts to linear indices

More specifically, write a program that reads in the following integer inputs from the user:

  1. The length of dimension 1
  2. The length of dimension 2
  3. The length of dimension 3
  4. The coordinate value in dimension 1
  5. The coordinate value in dimension 2
  6. The coordinate value in dimension 3

The program should output the linear index s corresponding to the given 3D coordinate.

Sample run #1:

Enter dimension 1 length: 2
Enter dimension 2 length: 3
Enter dimension 3 length: 4
Enter index for dimension 1: 0
Enter index for dimension 2: 2
Enter index for dimension 3: 1
9

Sample run #2:

Enter dimension 1 length: 6
Enter dimension 2 length: 2
Enter dimension 3 length: 5
Enter index for dimension 1: 5
Enter index for dimension 2: 1
Enter index for dimension 3: 4
59