hw2spec

hw2spec - Winter 2009 CS 32 Homework 2 Time due 9:00 P M...

This preview shows pages 1–6. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Winter 2009 CS 32 Homework 2 Time due: 9:00 P M Tuesday, February 3 1. Run the following program under control of a debugger: 2. #include <iostream> 3. using namespace std; 4. 5. struct Blark 6. { 7. char c[3]; 8. double d; 9. char e; 10. }; 11. 12. int main() 13. { 14. cout << "Enter your student number using only digits (e.g. 123456789): "; 15. int id; 16. cin >> id; 17. int n = 8821 + id % 19997; 18. Blark* bp = new Blark[30000]; 19. char* p0 = &bp[n].c[0]; 20. char* p1 = &bp[n].c[1]; 21. char* q = &bp[n].e; 22. delete bp; 23. } Use the debugger to answer the following questions about the state of the program at the point just before the delete expression is executed. a. What is the value of n ? b. What is the value of bp ? c. What is the value of p0 ? d. What is the value of p1 ? e. What is the value of q ? f. What is the value of the expression &bp[n] ? (Hint: You can use the Watch window for this in Visual Studio.) g. What is the value of the expression &bp[n+1] ? For this problem, you'll turn in either a Word document named answers.doc or answers.docx, or a text file named answers.txt, that has your answer to this problem (and Problems 3 and 5). 24.Write a C++ function named pathExists that determines whether or not a there's a path from start to finish in a 10x10 maze. Here is the prototype: 25. bool pathExists(char maze[10], int sr, int sc, int er, int ec); 26. // Return t rue if there is a path from (sr,sc) to (er,ec) 27. // through the maze; return false otherwise The parameters are o A 2-dimensional 10x10 character array of Xs and dots that represents the maze. Each 'X' represents a wall, and each '.' represents a walkway. o The starting coordinates in the maze: sr , sc ; the row number and column number range from 0 to 9. o The ending coordinates in the maze: er , ec ; the row number and column number range from 0 to 9. The function must determine if there is a path from maze[sr][sc] to maze[er][ec] that includes only walkways, no walls. The path may turn only north, east, south, and west, but not diagonally. You must use a stack data structure. Your solution will use the following simple class, which represents an (r,c) coordinate pair: class Coord { public: Coord(int rr, int cc) : m_r(rr), m_c(cc) {} int r() const { return m_r; } int c() const { return m_c; } private: int m_r; int m_c; }; (Our convention is that (0,0) is the northwest (upper left) corner, with south (down) being the increasing r direction and east (right) being the increasing c direction.) The stack you must use is a stack of Coord s. You may either write your own stack class, or use the stack class in the C++ Standard Library (pp. 309-311 in the textbook discuss that class). Here's an example of the relevant functions in the library's stack class: #include <stack> using namespace std; int main() { stack<Coord> coordStack; // declare a stack of Coords Coord a(5,6); coordStack.push(a); coordStack....
View Full Document

• Spring '08
• DavidSmallBerg
• Trigraph, Forth, Standard Template Library, Reverse Polish notation, Stack-oriented programming language

{[ snackBarMessage ]}

Page1 / 15

hw2spec - Winter 2009 CS 32 Homework 2 Time due 9:00 P M...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online