[SOLVED] CECS282 -Lab 3

30.00 $

Programming resource
Digital learning resource
Category:
Practical programming resource
Suitable for guided study and reference
Tutor guidance available when needed

Description

5/5 - (1 vote)

Reading

ReadingfromC++HowtoProgram:

  1. Important: Chapter8.1,8.2,8.3,8.4
  2. 10
  3. 13
  4. 1,15.2(focusonlyonvector),15.3 IntroductiontoIterators
  5. 5(again,onlyvectors)

Assignment

  1. WhatistheoutputofthefollowingC++codefragment? Tracethecode’soperationtosupportyour answer: keeptrackofeachvariable’scurrentvalue,andindicatewhatactualvaluesthepointerspoint 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;

}

  1. WriteaC++functionSolveQuadraticwhichsolvesaquadraticequationoftheformax2+bx+c=0 when given the coe cients a, b, and c. Your function should take three double parameters for the threecoe cients, plustwodoublepointer parametersthatyouwillusetosavethetwosolutionsto theequation. Youwillreturnanintegerindicatingthenumberofrealsolutionstotheequation.

Yourfunctionshouldnotdoanyinputoroutput;itshouldonlycalculateandsetthesolutionvariables, andreturnthenumbeofsolutions.

Exampleusage: 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); Testyourcodetomakesureitworksinthefollowingscenarios:

a b c numberOfSolutions xSolution1 xSolution2 Explaination
1 2 1 1 -1 0 For x2+2x+1=0,the

onlysolutionis x=−1

1 0 1 0 0 0 Norealsolutionto x2+1=0
1 -3 -4 2 4 -1 For x2−3x−4=0,the solutionsare x=4and

x=−1

1

  1. Inthefollowingcodefragment:

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 forthesevariables.

  1. Foreachfunctiondescription, decide if the function should accept its parameter as a string, a string*, a string&, or a const string&:
    • In FuncA, Ada will read individual characters from the string to make a computation. She will notmutatethestring.
    • In FuncB, Ada will mutate the string parameter, but doesn’t want the original string passed to thefunctiontomutate.
    • InFuncC,Adawillmutatethestringparameter,andthestringcannotbe null/nullptr.
    • In FuncD, Ada does not want to duplicate the string passed to the function, and is prepared to handleanullvalueaswell.
  2. GiventhefollowingC++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];

}

  • By the time the comment is reached at run time, how many actual vector objects exist in automaticstorage? Identifythem.
  • At what point in the program will the parameter p2 be destroyed and removed from memory? Bespeci c.
  • Whatistheprintedoutputofthisprogram?
Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.