Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// In C++ // Complete the Do's (bolded) Page of 3 ZOOM #include using namespace std; int Zero = 0; void eat_white (istream & in)

// In C++

// Complete the Do's (bolded)

Page

of 3

ZOOM

#include using namespace std; int Zero = 0; void eat_white (istream & in) /* Pre: "in" is a valid input file stream. Post: Any white space skipped over to EOF or the next non-white space character.*/ { while ((in.peek()!=EOF) && isspace(in.peek())) in.ignore(); }; /*inline */int & element (int* tri2d, int NC, int i, int j) { /* DO 1: Study the following code to see how the "valid/stored elements are counted. This version (#1) has TWO nested loops: */ int row, col; int index = -1; if (ij) { col = i; row = j; } else // if (i==j) return Zero; for (int ii = 0; ii < row; ii++) for (int jj = 0; jj < NC; jj++) if (jj > ii) index++; for (int jj = 0; jj < col; jj++) index++; return tri2d[index]; /* DO 2: Study the following code to see how it simplifies the above counting using this version (#2) has a single loop: Next, comment out the above version (#1) and uncomment the following version #2 and run:*/ /* int row, col, index = -1; if (i < j) { row = i; col = j; } else if (i > j) { col = i; row = j; } else return Zero; for (int k = 0; k < row; k++) index += (NC - 1 - k); index += (col - row); return tri2d[index]; */ /* DO 3: Study the following code to see how it further simplifies the above logic using a "formula version" (#3), which can be written EITHER as a conditional expression (#3a), in which case "element" can be an "inline" function OR as a conditional statement (#3b). Next, comment out the above versions (#1 and #2) and then uncomment each of the versions 3a and 3b SEPARATELY and run:*/ // Version 3a: /* return ((ij)? tri2d[NC*j-j*(j+1)/2 +

i-j-1] : Zero)); */ // Version 3b: /* if (ij) return tri2d[NC*j-j*(j+1)/2 + i-j-1]; else // if (i==j) return Zero; */ }; void printtri (int * dist2d, int NC) { for (int i=0; i> NC; dist2d = new int[NC*(NC-1)/2]; cout << endl << "Now enter distances from each city to ONLY the remaining cities row by row in increasing order of cities " << endl; for (int i=0; i> element (dist2d, NC, i, j); char command; cout << endl << "If you want to get or set any particular distance, input" << endl << "g (for get) or s (for set) followed by two city numbers" << endl << "(with zero-based indexing) and an integer value (for s)," << endl << "p to print the whole distance array, or d (for done) to end:" << endl; do { int i, j, val; eat_white(cin); cin >> command; if ((command == 's')||(command == 'S')) { cin >> i >> j >> val; if (i!=j) { element (dist2d, NC, i, j) = val; cout << "Done setting distance from city " << i

<< " to city " << j << " to " << val << endl << "Next command: "; } else cout << "Distance from a city to itself is zero " << "and cannot be changed; sorry!" << endl << "Next command: "; } else if ((command == 'g') || (command == 'G')) { cin >> i >> j; cout << "The distance from city " << i << " to city " << j << " is " << element (dist2d, NC, i, j) << endl << "Next command: "; } else if ((command == 'p') || (command == 'P')) { cout << "Reporting distance chart:" << endl; printtri (dist2d, NC); cout << "Next command: "; } else if ((command != 'd') && (command != 'D')) cout << "Please enter a valid command (s/S, g/G, p/P, or d/D): "<< endl; } while ((command != 'd') && (command != 'D')); return(0); };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

Ensure continued excellence in people management.

Answered: 1 week ago