EGR 221 --- Data Structures in C++
Homework 1
California Baptist University
Summer 2018
Due: at the beginning of class, Friday, July 13, 2018
Please typeset your answers to the following homework problems and turn in a hard copy of your homework to the instructor by the due time.
1.The following is the prototype of a function concatenateArrays which concatenates two integer arrays A and B to form a new array C.
void concatenateArrays(const int A[], const int B[], int C[],
const int sizeA, const int sizeB, int& sizeC);
// Function: concatenate integer arrays A and B to form C
// Inputs: integer arrays A, B, sizes of A and B
// Outputs: integer array C, size of C
// Precondition: none
// Postcondition: C is the array formed by appending B after A
// size of C is the sum of sizes of A and B
Write the function definition for the above function.
2.The following is the prototype of a function evenIndexedSubarray which extracts the even-indexed elements from the double array A to form a double array B.
void evenIndexedSubarray(const double A[], double B[],
const int sizeA, int& sizeB);
// Function: extracts the even-indexed elements from the
// double array A to form a double array B
// Inputs: double array A, size of A
// Outputs: double array B, size of B
// Precondition: none
// Postcondition: B is the array formed by extracting all
// elements in A with even indices (e.g., A[0],
// A[2], ...). size of B is the number of
// even indexed elements in A
Write the function definition for the above function.
3.Mark the following statements as true or false.
a)The life cycle of software refers to the phases from the point the software was conceived until it is retired.
b)The three fundamental stages of software are development, use, and discard.
c)The expression 4n+2n2+5 is O(n).
d)The instance variables of a class must be of the same type.
e)The function members of a class must be public.
f)A class can have more than one constructor.
g)A class can have more than one destructor.
h)Both constructors and destructors can have parameters.
4.What is black-box testing? (Hint: see pages 7-8 in the supplementary reading on Software Development and Algorithm Analysis posted on Blackboard.)
5.What is white-box testing? (Hint: see pages 7-8 in the supplementary reading on Software Development and Algorithm Analysis posted on Blackboard.)
6.Explain the difference between a C++ struct and class.
7.Why is it good practice to put a class declaration in one file and the implementation in another?
8.Each of the following expressions represents the number of operations for certain algorithms. What is the order of each of these expressions?
a)(n2 + 1)(3n + 5)
b)n + 2 log2n - 6
c)4n log2n + 3n +8
9.Consider the following function:
int func(int list[], int size)
{
int sum = 0;
for (int index = 0; index < size; index++)
sum = sum + list[index];
return sum;
}
a)Find the number of operations executed by the function func if the value of size is 10.
b)Find the number of operations executed by the function func if the value of size is n.
c)What is the order of the function func?
10.Consider the following function prototype:
int func1(int x);
The function func1 returns the value as follows: If 0 <= x <= 50, it returns 2x; if -50 <= x <0, it returns x2; otherwise it returns -999. What are the reasonable boundary values for the function func1?
11.Characterize the following algorithm in terms of Big-O notation.
for (int i = 1; i <= 2 * n; i++)
for (int j = 1; j <= n; j++)
cout << 2 * i + j
cout << endl;
12.Find the syntax errors in the definitions of the following classes:
a)
class AA
{
public:
void print();
int sum();
AA();
int AA(int, int);
private:
int x;
int y;
};
b)
class BB
{
int one;
int two;
public:
bool equal();
print();
BB(int, int);
};
c)
class CC
{
public:
void set(int, int);
void print();
CC();
CC(int, int);
bool CC(int, int);
private:
int u;
int v;
};
13.Consider the following declarations:
class xClass
{
public:
void func();
void print() const;
xClass ();
xClass (int, double);
private:
int u;
double w;
};
xClass x;
a)How many members does class xClass have?
b)How many private members does class xClass have?
c)How many constructors does class xClass have?
d)Write the definition of the member function func so that u is set to 10 and w is set to 15.3.
e)Write the definition of the member function print that prints the contents of u and w.
f)Write the definition of the default constructor of the class xClass so that the private data members are initialized to 0.
g)Write a C++ statement that prints the values of the data members of the object x.
h)Write a C++ statement that declares an object t of the type xClass, and initializes the data members of t to 20 and 35.0, respectively.
14.Consider the definition of the following class:
class CC
{
public:
CC();//Line 1
CC(int);//Line 2
CC(int, int);//Line 3
CC(double, int);//Line 4
.
.
.
private:
int u;
double v;
};
a)Give the line number containing the constructor that is executed in each of the following declarations:
i.CC one;
ii.CC two(5, 6);
iii.CC three(3.5, 8);
b)Write the definition of the constructor in Line 1 so that the private data members are initialized to 0.
c)Write the definition of the constructor in Line 2 so that the private data member u is initialized according to the value of the parameter, and the private data member v is initialized to 0.
d)Write the definition of the constructors in Lines 3 and 4 so that the private data members are initialized according to the values of the parameters.
15.Write the definition of a class that had the following properties:
a)The name of the class is secretType.
b)The classs secretType has four instance variables: name of type string, age and weight of type int, and height of type double.
c)The class secretType has the following member functions:
print --- Outputs the data stored in the instance variables with the appropriate titles
setName --- Function to set the name
setAge --- Function to set the age
setWeight --- Function to set the weight
setHeight --- Function to set the height
getName --- Value-returning function to return the name
getAge --- Value-returning function to return the age
getWeight --- Value-returning function to return the weight
getHeight --- Value-returning function to return the height
Default constructor --- Sets name to the empty string and age, weight, and height to 0
Constructor with parameter --- Sets the values of the instance variables to the values specified by the user
d)Write the definition of the member functions of the class secretType as described in Part c. (Be sure to include data validation in the set functions.)
16.Assume the definition of the class personType as given below:
#include <string>
using namespace std;
class personType
{
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName.
void setName(string first, string last);
//Function to set firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of firstName is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of lastName is returned.
personType();
//Default constructor
//Sets firstName and lastName to null strings.
//Postcondition: firstName = ""; lastName = "";
personType(string first, string last);
//Constructor with parameters.
//Sets firstName and lastName according to the parameters.
//Postcondition: firstName = first; lastName = last;
private:
string firstName;//variable to store the first name
string lastName;//variable to store the last name
};
void personType::print() const
{
cout << firstName << " " << lastName;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
//Default constructor
personType::personType()
{
firstName = "";
lastName = "";
}
//Constructor with parameters
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}
a)Write a C++ statement that declares student to be a personType object, and initialize its first name to “Buddy” and last name to “Arora”.
b)Write a C++ statement that outputs the data stored in the object student.
c)Write C++ statements that change the first name of student to “Susan” and the last name to “Miller”.
17.Consider the Time class given in Handout 2 Example Programs. What is the expected output of the following C++ code (without actually executing the program on the computer)?
Time time1;
Time time2(23, 13, 25);
Time time3;
time1.write();
cout << endl;
time2.write();
cout << endl;
time1.setTime(25, 5, 59);
time1.write();
cout << endl;
time3 = time1.increment();
time3.write();
cout << endl;
switch (time3.comparedTo(time2))
{
case BEFORE: cout << "time3 comes before time2" << endl;
break;
case SAME : cout << "time3 and time2 are the same" << endl;
break;
case AFTER : cout << "time3 comes after time2" << endl;
break;
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。