Sale!

Lab 3 Reading

$30.00

Category:
Rate this product

Lab 3
Reading
Reading from C++ How to Program:
1. Important: Chapter 8.1, 8.2, 8.3, 8.4
2. Skim Chapter 6.10
3. Chapter 6.13
4. Chapter 15.1, 15.2 (focus only on vector), 15.3 ?Introduction to Iterators?
5. Chapter 15.5 (again, only vectors)
Assignment
1. What is the output of the following C++ code fragment? Trace the code’s operation to support your
answer: keep track of each variable’s current value, and indicate what actual values the pointers point
to.
#include <iostream
using namespace std;
double Blah(int *p) {
*p = *p + 5;
double local = *p / 2;
return local;
}
int main() {
int x = 10;
int *y = &x;
double z = Blah(y);
cout <?< x <?< endl <?< z;
}
2. Write a C++ function SolveQuadratic which solves a quadratic equation of the form ax2 +bx+c = 0
when given the coe?cients a, b, and c. Your function should take three double parameters for the
three coe?cients, plus two double pointer parameters that you will use to save the two solutions to
the equation. You will return an integer indicating the number of real solutions to the equation.
Your function should not do any input or output; it should only calculate and set the solution variables,
and return the numbe of solutions.
Example usage:
double xSolution1, xSolution2;
double a, b, c;
// suppose a, b, and c are given values from the user
int numberOfSolutions = SolveQuadratic(a, b, c, &xSolution1, &xSolution2);
Test your code to make sure it works in the following scenarios:
a b c numberOfSolutions xSolution1 xSolution2 Explaination
1 2 1 1 -1 0 For x
2 + 2x + 1 = 0, the
only solution is x = −1
1 0 1 0 0 0 No real solution to
x
2 + 1 = 0
1 -3 -4 2 4 -1 For x
2 − 3x − 4 = 0, the
solutions are x = 4 and
x = −1
1
3. In the following code fragment:
string a = ?hello?;
string b = a;
string *c = &a;
string &d = *c;
string e = *c;
How many instances of the string type exist in memory? Draw a picture of automatic storage
for these variables.
4. Ada is writing several functions that each accept a string parameter. For each function description,
decide if the function should accept its parameter as a string, a string*, a string&, or a const
string&:
(a) In FuncA, Ada will read individual characters from the string to make a computation. She will
not mutate the string.
(b) In FuncB, Ada will mutate the string parameter, but doesn’t want the original string passed to
the function to mutate.
(c) In FuncC, Ada will mutate the string parameter, and the string cannot be ?null?/nullptr.
(d) In FuncD, Ada does not want to duplicate the string passed to the function, and is prepared to
handle a null value as well.
5. Given the following C++ program:
# include < vector
# include < iostream
using namespace std ;
void FA ( vector < int & p1 ) {
// see part ( a )
p1 . pop_back ();
p1 . push_back (0);
}
void FB ( vector < int p2 ) {
FA ( p2 );
}
int main () {
int x = 0;
vector < int v = {1 , 2 , 3 , 4};
FB ( v );
cout << v . size () << ” ” << v [3];
}
(a) By the time the comment is reached at run time, how many actual vector objects exist in
automatic storage? Identify them.
(b) At what point in the program will the parameter p2 be destroyed and removed from memory?
Be speci?c.
(c) What is the printed output of this program?
How to Get Credit
Show me your answers to each question.
2

Open chat
Need help?
Hello
Can we help you?