$25
Reading
Reading from C++ How to Program:
1. Chapter 1.6, 1.9
2. Skim Chapter 2.2, 2.3, 2.5, 2.6, 2.7
3. Skim Chapter 4 and 5 if you need the review
4. Skim Chapter 6.1 and 6.2
5. Review Figure 6.2 in Chapter 6.3
6. Chapter 6.4
7. Chapter 6.6 (don’t need to memorize, just a good reference)
8. Chapter 6.18
9. Important: Chapter 8.1, 8.2, 8.3, 8.4
10. Skim Chapter 6.10
11. Chapter 6.13
Assignment
1. The following code does not compile, citing an error of identi er Funk is unde ned. Show/explain with code how to resolve the compile-time error.
int main() {
// call Funk #49 Funk(49);
}
void Funk(int f) { f = f * 2;
}
2. Write a C++ function RoundToNearest, which takes a positive double parameter and returns the integer nearest that double by rounding it. (Example: RoundToNearest(6.4) = 6, RoundToNearest(6.5) = 7.) Write your function using only a single line of code in the function body, and without using any rounding functions from the C++ standard library. (Your code should not call any functions at all and should consist entirely of arithmetic.)
3. Ada downloads the le doors.h from the Lecture 2 and saves it to her Desktop on her Windows PC. She then sets up a C++ project in Visual Studio and notes that Visual Studio saved the project on her hard drive at the location C:\AdaProjects\Question6. She adds doors.h to her project by using the Add Existing Item option in Visual Studio; this option references the le doors.h in the project, but does not copy it to the project folder.
She adds a new le to the project called main.cpp, and notes that Visual Studio puts the new le in the folder C:\AdaProjects\Question3. She writes this code in main.cpp:
#include <iostream> #include doors.h
int main(int argc, char* argv[]) { // call function from doors.h int p = getPrizeDoor();
}
1
When Ada compiles her le, she gets the following error message: Could not open include file: doors.h’: No such file or directory
(a) Where is the compiler looking for the le iostream? (You may need to research this.) (b) Where is the compiler looking for the le doors.h?
(c) Why can’t the compiler locate doors.h?
(d) What’s the moral of the story here?
4. In the program below, identify the order that each variable enters automatic storage when the program executes. Also identify the line of code in which each variable is removed from automatic storage.
void test(int x) { int a = x; if (a > 0) { int b = a;
b++;
}
}
int main() { int x = 100; test(x);
int y = 8;
}
5. In the following code fragment:
int a = 9; int b = a; int *c = &a; int *d = c; int e = *c;
How many instances of the int type exist in memory? Draw a picture of automatic storage for these variables.