Lesson 2
The Basic Elements
Chapter 10: Applied problem solving
Subscripts to linear indices (3D)
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.
More specifically, write a program that reads in the following integer inputs from the user:
- The length of dimension 1
- The length of dimension 2
- The length of dimension 3
- The coordinate value in dimension 1
- The coordinate value in dimension 2
- 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