

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Red - Due Tuesday 09/08/ 09 Carefully review the specification for Program 1. Implement the loadDatabase() function. This function should read in all of the information from the database file provided and store it into parallel vectors. This should be very similar to the code you wrote last week, but now you will use vectors instead of arrays. Note that vectors dynamically increase their size, so your functions should work on any number of data points. Storing the first and last names should be similar to your previous code. In addition, you will now have to read in an answer array vector per the program specification. Creating a third parallel vector of vectors would be the way to go about this. Read the Program 1 specification for more information. Blue - Due Thursday 09/10/ 09 Answer the following questions:
1. Goals and Objectives The claim from last weekâs lab, which may have seemed outrageous, was that every C++ data type must ultimately be mapped in a predictable way to a fixed number of linearly numbered bytes. This weekâs lab will let you explore that idea: The Great Oz will be unmasked. Think about it⊠One of the most fundamental types ( int ) has a size of 4 bytes. So even an integer wonât fit in a single memory location (only one byte), and we have to break it up across four different memory locations to store it. In class you are learning about arrays, multidimensional arrays, and vectors. Let's take a closer look at how data types are stored in memory. 2. Concepts and Tools Seeing the bytes behind the more abstract types will require some new concepts and tools. printf âThe printf function gives us better control over the formatting of output values than the âcout << âŠâ youâre accustomed to. For example, the statement cout << 65 << endl; always outputs the base-10 integer 65. And the statement printf(â%d\nâ, 65); will do the same
thing. The documentation for printf() and âformat specification fieldsâ will explain it in gory detail, but in a nutshell: The first argument is a character string with format specifiers (stuff beginning with %) and actual string data (in my example, the â\nâ new-line character) to be used in translating the arguments that follow (in my example, just the integer 65) into output. The â%dâ format specifier indicates that the first argument (integer 65) should be output as a decimal integer. So we get â65â and a new-line, just like we did with the statement cout << 65 << endl; Exercise 1: Change the format specifier in the above example to â%câ and what do you get? Why? (Hint: have a look at the Wikipedia entry for âASCIIâ. What character does byte 65 map to?) Exercise 2: Bytes and addresses are easier to interpret if we print them in hexadecimal (base-16) notation, so we will usually use â%08xâ for 32-bit values. The Windows Calculator program in âScientificâ view mode will allow you to convert between different number bases easily. Change the format specifier in Exercise 1 to â%08xâ and what do you get? Why? Exercise 3: The âaddress ofâ operator in C++ is â&â. As you know, a variable is a place for storing a value. Everything is stored in this great long linear array of bytes called memory. So it should not be terribly surprising that you can get from a variable name to that variableâs address in memory using⊠the unary â&â operator in C++. Try this: int ival = 65; printf("%d\n", &ival); What value is output? Why was it that, and not 65? Exercise 4: Allocate an array âivalarrayâ and give it a size that can hold exactly four int variables. What is the âsizeofâ this array? Why? Exercise 5: Print the addresses of each of the four values in the array (use the statement printf("%08x\n", &ivalarray[i]); ) What are they? What does this tell you about how arrays are stored in memory? Exercise 6: Allocate a 3x3 (two-dimensional) array âcvalarrayâ of charâs and populate it with unique values (an assignment statement like cvalarray[i][j] = i * 16 + j; is handy). Use the techniques like we did above to output the addresses and contents of each cell. Explain how two- dimensional matrices are stored in memory. Turn in your answers to this lab electronically using the CS215 class web page.