Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part A: This part is just for your understanding. You will just study, understand, and run the given program. C++ does have 2-dimensional arrays and
Part A: This part is just for your understanding. You will just study, understand, and run the given program. C++ does have 2-dimensional arrays and it (as any programming language has to) stores them in the linear memory of a computer. If you were interested in a custom 2-d data structure, e.g., one that looks like a right triangle, CH has no built-in way to do that. However, you can always program these things yourself! In this lab, for part A, you will study a program that creates a plain 2-d array by dynamically allocating a single-dimensional array, and storing all elements in there. You would, however, have to program ways to print all elements row by row, so they appear to the user as a 2-d array, and allow for getting or setting an element at any specified row and column, etc. Your program maybe interactive or read from a file in the following format (assuming zero-based indexing): g s d As can be seen, your program is expected to get values for the two dimensions first, and then read and store the values of the 2-d array (or matrix, whichever way you may think of it) row by row. After that, it responds to any combination of print (p), get (g), or set (s) inputs. The print command (p) prints the whole 2d array. Get specifies row and column numbers seeking to know what value is stored there, and set seeks to change the value stored at the specified row and column. Finally, d says done to end. How do you store 2-d array elements linearly, i.e., in sequence? Think about row major order, i.e., all elements of row 0 one after another, followed by all elements of the next row, followed by elements from row after row, in the order of increasing rows. Hint: Think first about where you would get an element value from, and/or where you would store a value upon the set command. That should also be useful in reading and storing the entire array contents, and finally, reporting them all at the end. You need a formula to identify the linear position of the element at row i and column j. Now study the code (2d - with prompts.cpp), run the program (use the data suggested in in2d.txt), and see and understand how it works. Your real work is in Part B. Part B: In this part, you will create a triangular data structure that stores distances between pairs of n cities. First think about it: If you have n, or say 5, cities, for the first city, you need to store only 4 distances; Why? For the second city, however, you need to store ONLY 3 distances! Why? As you consider each of the following cities, you need to store one fewer distance. That's what I mean by a triangular structure. See the illustration below: The X's indicate trivial/already known values. From To cities 0 X 1 2 3 4 1 X X 2 3 4 2 X X X 3 4 3 X X X X 4 4 NONE! !!!! [NOTE: The answer to the first why is that the distance from any city to itself, e.g., city #0 to city #0, is zero, and need not be stored! The answer to the second why is that the distance from any city (e.g., #1) to a smaller numbered city (e.g., #0) has already been stored previously for the lower numbered city (i.e., #0 to #1)! You only need a total of (N-1) rows, for cities #0 through #)N-2).] Your program maybe interactive or read from a file in the following format (assuming zero-based indexing); check out sample inputs in intrii.txt and intri2.txt: p g s d Again, how do you store this triangular data linearly, i.e., in sequence? You may once again use row major order, i.e., all elements of row 0 one after another, followed by all elements of the next row, etc. HOWEVER, unlike with the 2-d array, these rows are not all of the same size; they decrease in size! Part A: This part is just for your understanding. You will just study, understand, and run the given program. C++ does have 2-dimensional arrays and it (as any programming language has to) stores them in the linear memory of a computer. If you were interested in a custom 2-d data structure, e.g., one that looks like a right triangle, CH has no built-in way to do that. However, you can always program these things yourself! In this lab, for part A, you will study a program that creates a plain 2-d array by dynamically allocating a single-dimensional array, and storing all elements in there. You would, however, have to program ways to print all elements row by row, so they appear to the user as a 2-d array, and allow for getting or setting an element at any specified row and column, etc. Your program maybe interactive or read from a file in the following format (assuming zero-based indexing): g s d As can be seen, your program is expected to get values for the two dimensions first, and then read and store the values of the 2-d array (or matrix, whichever way you may think of it) row by row. After that, it responds to any combination of print (p), get (g), or set (s) inputs. The print command (p) prints the whole 2d array. Get specifies row and column numbers seeking to know what value is stored there, and set seeks to change the value stored at the specified row and column. Finally, d says done to end. How do you store 2-d array elements linearly, i.e., in sequence? Think about row major order, i.e., all elements of row 0 one after another, followed by all elements of the next row, followed by elements from row after row, in the order of increasing rows. Hint: Think first about where you would get an element value from, and/or where you would store a value upon the set command. That should also be useful in reading and storing the entire array contents, and finally, reporting them all at the end. You need a formula to identify the linear position of the element at row i and column j. Now study the code (2d - with prompts.cpp), run the program (use the data suggested in in2d.txt), and see and understand how it works. Your real work is in Part B. Part B: In this part, you will create a triangular data structure that stores distances between pairs of n cities. First think about it: If you have n, or say 5, cities, for the first city, you need to store only 4 distances; Why? For the second city, however, you need to store ONLY 3 distances! Why? As you consider each of the following cities, you need to store one fewer distance. That's what I mean by a triangular structure. See the illustration below: The X's indicate trivial/already known values. From To cities 0 X 1 2 3 4 1 X X 2 3 4 2 X X X 3 4 3 X X X X 4 4 NONE! !!!! [NOTE: The answer to the first why is that the distance from any city to itself, e.g., city #0 to city #0, is zero, and need not be stored! The answer to the second why is that the distance from any city (e.g., #1) to a smaller numbered city (e.g., #0) has already been stored previously for the lower numbered city (i.e., #0 to #1)! You only need a total of (N-1) rows, for cities #0 through #)N-2).] Your program maybe interactive or read from a file in the following format (assuming zero-based indexing); check out sample inputs in intrii.txt and intri2.txt: p g s d Again, how do you store this triangular data linearly, i.e., in sequence? You may once again use row major order, i.e., all elements of row 0 one after another, followed by all elements of the next row, etc. HOWEVER, unlike with the 2-d array, these rows are not all of the same size; they decrease in size
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started