Briefly explain the role of the pre-processor in the


1. Briefly explain the role of the pre-processor in the compilation of a program.

2. What does the -D in the following command do and why would we want to use it? g++ -D DEBUG sourceFile.cpp

3. Consider the source files sort.h, sort.cpp, things.h, things.cpp and main.cpp. The header files sort.h and things.h are included in main.cpp. The source file sort.cpp includes the header sort.h and the source file things.cpp includes the header file things.h.

Define a makefile that makes a program task from the source files given above. It requires that the make command will not re-compile the source files if they are up to date.

4. Describe what is meant by the term white box testing and explain the advantages and disadvantages of white box testing?

5. What is a segmentation fault? Give an example of how one might occur.

6. Define the structure Record as following

struct Record {
char name[50];
int age;
double payRate;
};
Implement a function saveRecords(char *filename, Record recs[], int size) to save the records into a binary file.

7. Define the structure Employee as following
struct Employee {
int id;
char name[50};
double payRate;
};
Write a function with prototype:
int linearProbingsearch(Employee recs[], int size, int number);

to perform a hashing search with linear probing by given target id number. The first argument is the hash table to be searched, the second argument indicates the size of the array and the third argument is the value (id number) to search for. The return value should be the index of the value in the array, or -1 if the value is not in the array. Assume the hash function int hashf(int) has been defined.

8 Quicksort is a recursive function which calls partition function by using a pivot. Use the method taught in the lectures to demonstrate the quicksort by write the data sequences after each partition be called for the following data array. You may choose the first element as a pivot for each sub array. Put underline for each pivot selected in the partition.
23 15 19 28 36 11 63 7 44 17

9. What does it mean to assign a pointer to another same type of pointer?

10. I want a function which takes an integer between 1 and 7 and returns a pointer to a string containing the corresponding day of the week. The following function is proposed:
string* days (int i)
string week[7] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
int * ptr = week;
if (i<=7 && i>0)
return (&ptr[i-1]);
else
return NULL;

a. What is wrong with this function?

b. How could you fix it?

11. A data file contains a database of records. It may be considered to be a binary file. A program is required to read the record which is the third from the end of the file (note, the last record is considered first from the end) and edit its authorlD. The record is defined as follows
struct Record
{
char name[12];
int authorlD;
int pubId;
1;
a. If the file is called magazine.dat, how might we open the file so that the resulting stream fin can both read and write the file in binary?

b. Write the code to get the number of bytes in the file and store the value in the variable fileSize. (1 Mark)

c. To read in the record third from the end, the following pseudocode is developed:

    1. Position read marker 3 records before the end of the file

    2. Read in a single record into the variable author.
Write the two C++ statements needed to perform these tasks. You may assume that author is already declared as a Record.

d. A new authorlD has been successfully requested from the user and is stored in an integer newId. Assuming that the write marker of the stream fin is currently positioned at the start of the correct record, write the code required to reposition the write marker and then write newId only into the correct location.

12. You have some code for an old linked list implementation which takes the name and booking size for a customer. It works well, but only works for customer records. It has the following prototypes:
struct customer
{
char name[21];
short int bookingSize;
customer * next;
} ;
void init(custPtr &head);
void add (const char name[], int size, customer* &head); bool remove (char name[], int &size, customer* &head);

a. Explain how we might use a typedef to make this linked list more generic and rewrite the above code to show how the prototypes might change.

b. What is the main limitation of using typedef to achieve generic code rather than another method such as void* and function pointers?

13. The following function returns the volume of a rectangular prism, or -1 if the dimensions are out of range.
Int volume(double w, double h, double d) {
if (w < 0 ll h< 0 II d< 0)
return -1;
vol = w*h*d;
return vol;
}
a. Rewrite it so that it throws an exception if the inputs are out of range.

b. Write a code fragment which tries to call your rewritten volume function with and prints "invalid arguments" if it catches an out_of_range exception.

2015 Sept 16 CSCI124 WG Spring 2015 Page 5 of 9

14. Write short answers to the following questions:

a. What does a mutator function mean for a class?

b. List three member functions which will be implicitly declared if they are not explicitly declared.

c. What does a friend function mean for a class?

15. Write C++ code for the following questions:

a. Define a class Department. A Department has name, building number, manager name and budget. Define a manager function that set all data members' values from parameters. Define an access function to print out all the data members to an ostream.

b. Implement the member functions that defined in the question a. just above. These implementations should be placed outside of the class definition.
c. Write a sketch code to declare a Department instance with the proper values and print out the attributes of the Department. The values are up to you.

16. Write C++ code for the following questions:

a. Define a class MyString consist of two data members. One is a char pointer; another one is an integer stores the size of the dynamic array. Define the default constructor, initialization constructor, copy constructor, destructor and print function for the class.

b. Implement the constructors, destructor and the print function that defined in the question a just above outside the class definition. The copy constructor makes a deep copy from a MyString object.

c. Write a sketch code to declare a MyString instance by initialization constructor, declare another MyString instance with a copy constructor. Print out the two MyString instances.

17. Define a structure Node and a class Stack as following struct Node {
int data;
Node *next;
1 ;
class Stack {
private:
Node *head; int number; public:
Stack() f
head = NULL;
number = 0;

1 ;

Write C++ code for the following questions:

a. Implement the member functions defined above for the class Stack.
b. Define and Implement the destructor for the class Stack.

18. Define classes as following
struct DNode f int data; DNode *prev;
DNode *next;
1 ;
class Deque {
private:
DNode *head;
DequeNode *tail; public:
Deque();
Deque(const Deque &); -Deque();
void push front(int); void push_back(int); int front();
int back();
void pop_front(); void pop back(); void insert(int); void printAll(std::ostream &);
1 ;
Write C++ code for the following questions:

a. Implement the member function insert(int) for the class Deque. The function inserts a new entry in the deque according to the ascending order. The deque might be empty.

b. Implement the copy constructor of the class Deque to make a deep copy from a Deque object. Assume the other member functions except insert(int) have been implemented.

c. Implement the member function printAll(std::ostream &). The function prints all the data values that stored in the deque to the output stream.

19. Define classes as follows
struct BTreeNode 1
int data;
BTreeNode *left;
BTreeNode *right;
I ;
class BST {
private:
BTreeNode *root; public:
BST();
-BST();
bool isEmpty(); void insert(int);
void preorderTraversal();
void inorderTraversal();
void postorderTraversal();
} ;
Write C++ code for the following questions:

a. Implement the member function insert(int). The function inserts a new entry into the binary search tree.

b. Implement the member function preorderTraversal(). The function prints out all the data values of a binary search tree by preorder traversal.

Hint: It is easier to write the function in a recursive manner. You may declare additional functions if you wish.

20. Write short answers to the following questions:
a. What is a complete binary tree?
b. Given the initial data as following
119 21 49 202 135 421 127
Sort the data above using the radixsort taught in the lectures. Demonstrate your answers by draw the buckets and extract elements sequences for each pass.

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Briefly explain the role of the pre-processor in the
Reference No:- TGS01696101

Expected delivery within 24 Hours