Question
Plz help me find solutiona for this cs 115 question . [5 marks] Suppose binary search was applied to search for 24 in the unsorted
Plz help me find solutiona for this cs 115 question .
[5 marks] Suppose binary search was applied to search for 24 in the unsorted array shown below. Explain step by step what the method will do. Will the method find 24?
24 |
17 |
75 |
51 |
31 |
25 |
99 |
2. [4 marks] Trace the operation of the Insertion Sort algorithm on the following array (show the result of every iteration, even if there is no change):
Original Array A | A after 1 iteration | A after 2 iterations | A after 3 iterations | A after 4 iterations | A after 5 iterations | A after 6 iterations |
23 |
|
|
|
|
|
|
74 |
|
|
|
|
|
|
62 |
|
|
|
|
|
|
17 |
|
|
|
|
|
|
99 |
|
|
|
|
|
|
52 |
|
|
|
|
|
|
3. [3 marks] Programmer X versus Programmer Y
Programmer X declared a global variable in a file named Counter.cpp:
unsigned int counter_value;
Programmer Y wrote main.cpp to attempt to change counter_value in Counter.cpp:
unsigned int counter_value;
int main()
{
counter_value = 1909;
}
(a) Write on the above code to show a change that programmer Y can make to gain access to counter_value (the one in Counter.cpp) and mark your change with Y.
(b) Write on the above code to show a change that programmer X can make to prevent Y from accessing counter_value (the one in Counter.cpp) and mark your change with X.
(c) If the changes from (a) and (b) are both made, which programmer succeeds, X or Y? ___
4. Mammal Class
Suppose a C++ class is needed to represent a mammal in a computer program. The class should have three fields for a birth year (represented by a positive integer), a species name, and weight (a real value representing the number of kilograms), all inaccessible outside the class.
(a) [6 marks] Write a declaration for this class using the most suitable data types, including a default constructor and an initializing constructor. The constructors should be accessible outside the class.
(b) [4 marks] Write the implementation of a function that sets a mammals weight to a suitable value passed as a parameter. Assume that the prototype for this function has been added to the class described in part (a). Make the function constant if appropriate.
(c) [1 marks] Show how the main function could declare a variable of the Mammal type using the initializing constructor for a dog weighing 18.09 kg born in 2013.
(d)[1 marks] Show how the main function could declare an array called myZoo to contain 125 mammals.
(e) [2 marks] Write the prototype declaration for a function named searchMammals that searches an array of mammals for a mammal with a particular species name and returns the index of the first instance of that species it finds (and -1 if it is not found). For efficiency, use pass-by-reference for the array, but guarantee that the array is not changed. Pass the number of mammals in the array as a parameter. You do not have to write the function.
5. Structured Record
(a) [3 marks] Write a declaration for a C++ structured-record type named Counter that includes two integer fields named counter_value and counter_upper.
(b) [6 marks] The counterInitialize function can be used to initialize any Counter variable to any appropriate given values for the counter value and its upper bound. The provided values should be nonnegative and the counter value should be less than its upper bound. Write a comment that describes the counterInitialize function in the style described in the CS 115 Program Organization Principles notes.
// counterInitialize
//
// Purpose:
//
// Parameter(s):
//
//
//
//
// Precondition:
//
//
// Returns:
//
// Side Effect:
//
6. Definitions
[5 marks] Define the following terms clearly and concisely. Point form may be used.
(a) Layering
(b) Modular programming
(c) Top-down design
(d) Loop invariant
(e) Selection sort
7. Two-Dimensional Array
(a) [2 marks] Use typedef to define a Spreadsheet type corresponding to a 2-dimensional array of positive or negative integers with dimensions NUMROWS and NUMCOLS, assuming NUMROWSand NUMCOLS have been defined as constants.
(b) [6 marks] Write a complete C++ function called writeSpreadsheet that, for example, can be called by
writeSpreadsheet (my_array, 21, 5, "my_file.txt");
to write 21 lines of 5 integers each into a file called "my_file.txt" from a Spreadsheet my_array.
(c) [1 mark] EITHER explain why your writeSpreadsheet function in part (b) facilitates design by contract OR write a line of code that could be added to facilitate design by contract and explain its purpose.
(d) [1 mark] EITHER explain why your writeSpreadsheet function in part (b) facilitates defensive programming OR write a line of code that could added to facilitate defensive programming and explain its purpose.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started