Docsity

Assignment on Load Database - Design Abstraction | CS 215, Assignments of Computer Science

Toggle sidebar
bg1
CS 215 Fall 2009
Assignment Sheet
Week 3
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. Explain the difference between a vector and an array.
2. What is an “index” when referring to a vector or array? What are the legal index values? What
is a bounds error?
3. The C++ runtime system can very quickly find a value stored in an array (or vector) structure
by simply knowing the starting address of the array and the index of the value requested. Why
is this?
4. Explain what the difference between an implementation and interface for a class and why this
separation is made.
5. Explain some of the benefits of using a class to encapsulate a group of related data objects
instead of several parallel arrays/vectors.
Lab: It’s All Bytes to Me
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
pf2

Partial preview of the text

Download Assignment on Load Database - Design Abstraction | CS 215 and more Assignments Computer Science in PDF only on Docsity!

CS 215 Fall 2009

Assignment Sheet

Week 3

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. Explain the difference between a vector and an array.
  2. What is an “index” when referring to a vector or array? What are the legal index values? What is a bounds error?
  3. The C++ runtime system can very quickly find a value stored in an array (or vector) structure by simply knowing the starting address of the array and the index of the value requested. Why is this?
  4. Explain what the difference between an implementation and interface for a class and why this separation is made.
  5. Explain some of the benefits of using a class to encapsulate a group of related data objects instead of several parallel arrays/vectors.

Lab: It’s All Bytes to Me

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.