Professional Documents
Culture Documents
Problem Set 1
(Not to be graded)
1) What is a pointer in C++? What are the usages of pointers, which part of the memory
are they stored in? What are some special commands when dealing with pointers?
char **apple
char *apple[]
char apple[][]
char *p=0;
char *t=NULL;
8) The dynamically allocated space within a function call gets automatically deallocated
when the function returns (True/False)
9) What is the issue with the following code?
char *foo()
{
char *str = “hello world”;
return str;
}
char *mystring;
mystring = foo();
printf(“mystring = %s\n”, mystring);
10) Consider the following function that shall return a pointer to a Date object
representing the date of tomorrow.
Please refer to the attached date.h and date.cpp files for further information
about the Date class.
Date* tomorrow() {
Date today;
today++;
return &today;
}
11) Describe what happens when the new operator is called and there is insufficient
amount of memory available for the allocation.
12) A LinkedList class is given in the appendix. Add a new method to this class that
counts the number of elements stored in the list. Provide both a recursive and a non-
recursive implementation.
13) What does the following function do?
struct Node{
int data;
Node *next;
};
return previous;
}
14) Isolate and fix the defect(s) present in the following code segment.
*area = pi*r*r;
cout << "Area of a circle with r=" << r << " is " << *area << endl;
}
int main(){
for(int i=1; i<10000; i++){
circle_area(i);
}
}
15) Isolate the defect in the following code segment.
16) Given
int A[SIZE];
Date B[SIZE];
int idx;
for(idx = 0; idx < SIZE; idx++){ A[idx] = idx + 1; }
for(idx = 0; idx < SIZE; idx++){ B[idx] += idx; }
int *i = A + 55;
Date *d = B + 28;
describe the effect of every statement below. Assume that the statements are executed
in the order given:
a) i++;
b) *i++ = 0;
c) (*i)++ = -1;
d) d--;
e) *(d-10) += 1;
f) cout << (*d).ToString() << endl;
g) *(d - 30) += 1;
17) Isolate and fix the defect present in the following code segment.
delete [] dynamicArray;
18) The function insertFront shall insert a new node containing the element data at
the head of a linked list and return a pointer to the new head. Isolate and fix the
defect present in the implementation of this function without modifying the Node
class.
class Node{
public:
private:
int data;
Node *next;
};
return newNode;
}
Appendix
date.cpp
#include "date.h"
#include <time.h>
#include <stdio.h> // for sprintf
/********************************************************************
This code is freely distributable and modifiable providing you
leave this notice in it.
Copyright @ Owen Astrachan
********************************************************************/
Date::Date()
// postcondition: date initialized to default date (today)
{
static struct tm timeHolder;
static struct tm *date = &timeHolder;
time_t tloc;
time(&tloc);
date = localtime(&tloc);
myMonth = date->tm_mon+1;
myDay = date->tm_mday;
myYear = date->tm_year+1900; // struct tm based on 1900
}
Date::Date(long days)
// postcondition: date initialized corresponding to absolute days
// after 1 A.D.
{
// this code is taken from "Calendrical Calculations, II"
// Reingold and Dershowitz and Clamen
// Software Practice and Experience, V. 23(4) 383-404 (april 1993)
long prior = days - 1; // prior days
long years400 = prior / 146097L; // # of 400 year cycles
long days400 = prior % 146097L; // days NOT in years400
// now have year and day #, find month and day in month
myMonth = 1;
while (myMonth <= 12 && 0 < finalDay){
finalDay -= DaysInMonth(myMonth, int(finalYear));
myMonth += 1;
}
myMonth -= 1; // went one to far
finalDay += DaysInMonth(myMonth, int(finalYear));
myDay = int(finalDay);
myYear = int(finalYear);
}
return false;
}
ostream & operator << (ostream & os, const Date & d)
{
os << d.ToString();
return os;
}
long operator - (const Date & lhs, const Date & rhs)
{
return lhs.Absolute() - rhs.Absolute();
}
bool operator != (const Date & lhs, const Date & rhs)
{
return ! (lhs == rhs);
}
bool operator < (const Date & lhs, const Date & rhs)
{
return lhs.Less(rhs);
}
bool operator > (const Date & lhs, const Date & rhs)
{
return rhs < lhs;
}
bool operator <= (const Date & lhs, const Date & rhs)
{
return lhs == rhs || lhs < rhs;
}
bool operator >= (const Date & lhs, const Date & rhs)
{
return rhs <= lhs;
}
date.h
#ifndef _DATE_H
#define _DATE_H
/********************************************************************
This code is freely distributable and modifiable providing you
leave this notice in it.
Copyright @ Owen Astrachan
********************************************************************/
#include <iostream>
#include <string>
using namespace std;
// accessor functions
bool Equal(const Date & rhs) const; // for implementing <, >, etc
bool Less(const Date & rhs) const;
// mutator functions
private:
void CheckDate(int m, int d, int y); // make sure that date is valid
};
ostream & operator << (ostream & os, const Date & d);
bool operator == (const Date & lhs, const Date & rhs);
bool operator != (const Date & lhs, const Date & rhs);
bool operator < (const Date & lhs, const Date & rhs);
bool operator > (const Date & lhs, const Date & rhs);
bool operator <= (const Date & lhs, const Date & rhs);
bool operator >= (const Date & lhs, const Date & rhs);
#endif
LinkedList.h
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
#include <iostream>
#include <string>
using namespace std;
struct Node{
int data;
Node *next;
class LinkedList{
public:
LinkedList();
~LinkedList();
Node * AddNode(int _data);
void DeleteNode(int _data);
void PrintList();
void DeleteList();
private:
Node * head;
};
#endif _LINKEDLIST_H
LinkedList.cpp
#include "linkedlist.h"
LinkedList::LinkedList(){
head = new Node();
}
LinkedList::~LinkedList(){
DeleteList();
}
void LinkedList::DeleteList(){
Node *temp = new Node();
Node *start = head;
while (start != NULL) {
if(start->next != NULL) //if we are not at the last node
temp = start->next;
else
temp = NULL;
delete start;
start = temp;
}
head = NULL;
}
return temp;
}
void LinkedList::PrintList(){
int i=0;
Node *p = head;
if( p == NULL ){
cout << "There are no elements in the list!!" << endl;
}
else {
while(p != NULL){
if(p->data != NOT_EXISTS)
cout << "Node " << i << " :" << p->data << endl;
p = p->next;
i++;
}
}
}
//If there are more than one node with the same
//data content, then the last one is deleted
void LinkedList::DeleteNode(int _data){