C Assignment-2 v3
-
Upload
mohit-grewal -
Category
Documents
-
view
291 -
download
4
Transcript of C Assignment-2 v3
FALL 2011 – SE 101 – Programming of Application Software
C++ Assignment -2
Date: 11-11-2011
Assignment due date and time: -11:55p.m., November 18, 2011
Mode of operation: -
Cheating will not be tolerated and will result in straight F grade to involved students.
You can form a team of 2 persons, but are allowed to work individually also. In order to avoid
last minute changes in teams, you are required to inform about your team members to any of
the TAs before 5p.m. on November 12, 2011.
What needs to be turned in: -
A word or pdf document that contains your answers for questions 1 and 2.
Well commented C++ source files for question 3 and 4.
Test cases used by you for verifying your programs.
All of above should be uploaded in LMS in the form of zip file named <roll_number>.zip.
------------------------------------------------------------------------------------------------------------------------------------------
Question 1 (16 points):
Mark the following statements as true or false.
a. In C++, pointer is a reserved word.
b. In C++, pointer variables are declared using the reserved word pointer.
c. The statement delete p; deallocates the variable pointer p.
d. The statement delete p; deallocates the dynamic variable to which p points.
e. Given the declaration:
int list[10];
int *p;
the statement
p = list;
is valid in C++.
f. Given the declaration
int *p;
the statement
p= new int[50];
dynamically allocates an array of 50 components of type int, and p contains the base address of
the array.
g. The address of operator returns the address and values of its operand.
h. If p is a pointer variable, the statement p= p*2; is valid in C++.
Question 2 (4 points):
What is wrong with the following code :
int *p; // Line 1
int *q; // Line 2
p= new int[5]; // Line 3
*p=2; // Line 4
for(int i=1;i<5;i++) // Line 5
p[i] = p[i-1] + i; // Line 6
q=p; // Line 7
delete [] p; // Line 8
for (int j=0;j<5;j++) // Line 9
cout << q[j] << “ “; // Line 10
cout << endl ; // Line 11
Question 3 (40 points):
Using classes, design an address book to keep track of the names, addresses, phone numbers and dates
of birth of family members, friends, and certain business associates. Your program should be able to
handle a maximum of N entries, where N should be taken as an input from the user during program
execution.
a. Assume that user has pretty good idea about how many entries he needs and number of entries
doesn’t vary at all when he is running the program. But, he certainly wants the flexibility to
change the size of his address book between different program execution runs. Which data
structure will you choose (from the ones discussed in the class till now) in this case and why?
b. Define a class, addressType that can store a street address, city, state, and a 6-digit pincode.
Define the appropriate functions to print and store the address.
c. Define a class PersonType using the class addressType and add other data variables to store
required information as defined above. Define the functions to print and store the appropriate
information.
d. Define a class addressBookType, so that an object of type addressBookType can store objects
of type PersonType. An object of type addressBookType should be able to process a maximum
of N entries, where N is defined as indicated above. Add necessary operations to class
addressBookType so that the program should perform the following operations:
I. Load N data elements into the address book from a disk. You have the flexibility of
defining the format of information that is saved on the disk. Only requirement is that
following functions should output information in a legible manner.
II. Print the address, phone number, and date of birth (if it exists) of a given person.
III. Print the names of all the people having the same status, such as family, friend or
business.
You are strongly encouraged to verify whether your program can handle a minimum of 10 entries. Since
time is limited, so testing with larger number of entries is not advised. But, smart verification ideas are
always welcome that boost confidence in your program (do not spend too much time is this respect for
current assignment).
Question 4 (40 points):
(Refer to Question 3) It can handle a maximum of N entries. But, N needs to be explicitly defined before
user actually loads the data. Redo the program to handle as many entries as required by user during
program execution.
a. Assume that user does not want to bother about how many entries are there at the relevant
place on the disk. He also wants the flexibility of adding or deleting entries without needing to
keep track of count during program execution. Which data structure will you choose (from the
ones discussed in the class till now) in this case and why?
Part b and c are similar to question 3. Part d is redefined as follows: -
d. Define a class addressBookType, so that an object of type addressBookType can store objects
of type PersonType. An object of type addressBookType should be able to process arbitrary
number of entries. Add necessary operations to class addressBookType so that the program
should perform the following operations:
I. Ideally, in such case user will interactively specify the input entries during program
execution. But, for simplicity, you are asked to load all existing data elements into the
address book from a disk. You have the flexibility of defining the format of information
that is saved on the disk. Only requirement is that following function should output
information in a user friendly manner.
II. Print the names of all the people having the same status, such as family, friend or business
on the screen.
III. Save the above information from part 4.d.II on the disk.
You are strongly encouraged to verify whether your program can handle a minimum of 10 entries. Since
time is limited, so testing with larger number of elements is not advised. But, smart verification ideas are
always welcome that boost confidence in your program (do not spend too much time is this respect for
current assignment).