ACCIS Online Portfolio
Intermediate C++ Assignment 1
Grade: 94 (32/34)
Grader: Leslie Ellis
Comments: You did a great job on this assignment. See the item in red below and keep up the good work.
Name: Janine Bouyssounouse
Student ID: 4782181
E-mail: jcbouyss@yahoo.com
Course Number: CS111
Course Code: 04A
Assignment Number: 1
Compiler Version: CodeWarrior 9
Version of Word: 2002
Answers to multiple choice questions are in bold.
1.1 Procedural abstraction refers to: b
a. the concept that coding a function requires abstract thought
b. the separation of the purpose of a function from its implementation
c. the linking of the purpose of a function to its implementation
d. saving definition and implementation of class in same file
1.2 Which of the following is not an advantage of using a module for a repeated task rather than duplicating the code? d
a. eases modifying the code later
b. streamlines the code, enhancing readability
c. simplifies debugging
d. increases speed of execution
1.3 Debugging is easiest when: d
a. all of the code is inline
b. modules are extensively used
c. fail-safe techniques have been used
d. both b and c
1.4 Which of the following does not enhance readability? b
a. good indentation style
b. using global variables
c. use of subprograms
d. using meaningful variables names
1.5 Object-oriented programming does not usually focus on: d
a. separating the interface and implementation of a class
b. ease of program modifiability
c. information hiding
d. client-side access to implementation details
1.6 Specifications for an abstract data type: c
a. specify how the data is stored
b. specify how to carry out the operations
c. specify the effect the operations have on the data
d. all of the above
Assignment 1 Errata (Posted 12/22/04)
Please remove this question from assignment one:
1.7 What is the main reason for not violating the walls of an ADT in an application program? Before answering this be certain you understand what it means to violate the walls of an ADT.
a. the program won't run
b. the information about the ADT should be private
c. a change in ADT implementation requires a change in the application program
d. a change in ADT implementation requires that the application program be recompiled
1.8 Which of the following is an example of an ADT? Before answering this review the definition for ADT. a
a. an array of strings to store names and operations to insert and delete names from the list
b. an array of strings used to store a list of names
c. operations for manipulating sets of objects and a data structure for storing sets of objects Correct answer (-2)
d. an algorithm for sorting a list of names
1.9 Consider the following code segment to average the non-negative integers in the array A of N integers.
int sum = 0;
int count = 0;
for (int I= 0; I< N; I++)
if (A[I] > 0) {
sum = sum + A[I];
count += 1;
}
int average = sum / count;
Describe the error checking that should be added to this code segment.
There should be a check to make sure that N is a valid value before going into the for loop. There should be a check to make sure count is not zero before the division is performed. There should be an invariant comment placed into the code before entering the loop, so that people can tell what is to be expected before and after the loop execution. There should be a cout statement stating the error or exception thrown if any values don’t meet the invariant conditions.
2.1 A recursive function is one that c
a. returns a double
b. takes three parameters
c. calls itself with a smaller problem size
d. is inside another function
2.2 What value is returned by the call F(3) where F is the recursive function given by the following code? b
int F (int N)
{
if (N == 0)
return 1;
else
return (2 * F(N - 1));
}
a. 6
b. 8
c. 4
d. none of the above
Questions 2.3 and 2.4 use the following function:
int mystery(int number) {
if (number == 1)
return 1;
else
return number * mystery(number -1 );
}
2.3 What value does function mystery return when called with a value of 4? b
a. 1
b. 24
c. 0
d. 4
2.4 What is the base case for function mystery? c
a. 0
b. 2
c. 1
d. -1
2.5 We say recursion is memory–intensive because b
a. it must occur numerous times before it terminates
b. previous function calls are still open when the function calls itself and the arguments of these previous calls still occupy space on the call stack
c. many copies of the function code are created
d. it requires large data values
2.6 All of the following are reasons to use recursion except: d
a. an iterative solution is not apparent
b. the resulting program is easier to debug
c. it more naturally mirrors the problem
d. it maximizes software performance
2.7 Implement maxarray, discussed in the section “Finding the Largest Item in an
Array,” as a recursive C++ function.
// -------------------------------------------------------------
// Program CS111ques2_7.cpp
//
// Purpose: Implements maxArray outlined in text.
//
// Author : Janine Bouyssounouse
// Date : January 22, 2005
// -------------------------------------------------------------
#include
using std::cout;
using std::cin;
using std::endl;
void showPurpose();
void fillArray(int anArray[], int arraySize);
int maxArray(const int anArray[], int arraySize, int maximum, int index);
void displayResults(int maxOfArray);
void showPurpose()
// -------------------------------------------------------------
// This function displays the purpose of the program.
// Preconditions : none.
// Postconditions: The purpose of this program has been written
// to standard output.
// -------------------------------------------------------------
{
cout << "This program will ask for positive integers to be \n";
cout << "entered into an array. Then it will find the maximum\n";
cout << "value of the integers in the array.\n\n";
}
void fillArray(int anArray[], int arraySize)
// ------------------------------------------------------------
// This function asks user to enter integers for the array.
// Preconditions : arraySize is set to the size of the array.
// Postconditions: The array is filled with integers.
// ------------------------------------------------------------
{
// initializes elements of array anArray to 0
for (int j = 0; j < arraySize; j++)
anArray[j] = 0;
// asks for array elements from user
// integers should be positive
for (int i = 0; i < arraySize; i++)
{
cout << "Enter positive array element " << i << ": ";
cin >> anArray[i];
while (anArray[i] <= 0)
{
cout << "Please enter only positive integers." << endl;
cout << "Enter positive array element " << i << ": ";
cin >> anArray[i];
}
}
}
int maxArray(const int anArray[], int arraySize, int maximum, int index)
// ------------------------------------------------------------
// This function uses recursion to find the maximum value of
// the elements of array anArray.
// Preconditions : anArray[] has been filled with integers.
// arraySize is set to the size of the array.
// maximum is set to the current max value.
// index is set to the current position in anArray.
// Postconditions: The value of the maximum element is returned.
// ------------------------------------------------------------
{
if (index >= arraySize)
return maximum;
else if (anArray[index] < maximum)
return maxArray(anArray, arraySize, maximum, ++index);
else
return maxArray(anArray, arraySize, anArray[index-1], ++index);
}
void displayResults(int maxOfArray)
// ------------------------------------------------------------
// This function displays the largest value in the array.
// Preconditions : maxOfArray is set to the largest value of
// the array anArray.
// Postconditions: The results are displayed.
// ------------------------------------------------------------
{
cout << "The largest element of the array is: "
<< maxOfArray << ". " << endl;
}
// function main begins program execution
int main()
{
const int sizeOfArray = 6; // used to hold the number of elements in array
int maxOfArray = 0; // used to hold the element with the max value
int maximum = 0; // maximum element in array
int index = 0; // variable for placement in array
showPurpose();
int anArray[ sizeOfArray ]; // declares array with specified size
fillArray(anArray, sizeOfArray);
maxOfArray = maxArray(anArray, sizeOfArray, maximum, index);
displayResults(maxOfArray);
return 0; // indicates that program ended successfully
} // end of function main
Program run:
This program will ask for positive integers to be
entered into an array. Then it will find the maximum
value of the integers in the array.
Enter positive array element 0: 5
Enter positive array element 1: 12
Enter positive array element 2: -4
Please enter only positive integers.
Enter positive array element 2: 2
Enter positive array element 3: 15
Enter positive array element 4: 7
Enter positive array element 5: 9
The largest element of the array is: 15.
website created by Janine Bouyssounouse.
Last updated 02/08/07