Answer to Question #275117 in C++ for Pooja

Question #275117

Test #2:



Find the bug and write the correct version of the program



#include <iostream>



class Utils



{



public:



static double findAverage (int number1, int number2)



return number1 + number2 / 2;



};



}



int main()



std::cout << Utils::findAverage(5, 2); // it should return 3.5



}



Test #3 (optional):



Please find and explain all the errors in the following function. There is at least one of the



following:



1. Compilation error



2. Segmentation fault



3. Performance issue



Flight minFlight(std::vector<Flight> flights)



{



std::sort(flights.begin(), flights.end()); return flights.begin();



}



Also, write the correct version of the program.

1
Expert's answer
2021-12-03T06:27:55-0500

Test # :2

#include <iostream>
using namespace std; // This line was missing, for input/ouput declaration
class Utils
{
public:
static double findAverage (double number1, double number2){ // The function should have a body, so use opening  { and  closing } 
return (number1 + number2) / 2; // use brackets to follow order of operations
}
};

int main() { // This opening brace was put in wrong place
std::cout << Utils::findAverage(5, 2); // it should return 3.5
}


Test #: 3

Flight minFlight(std::vector<Flight> flights)
{
    std::sort(flights.begin(), flights.end());
    Flight min = *flights.begin();
    return min;
}

// In your code, the line return flights.begin(); incorrect leading to compilation error
// You did not include the line Flight min = *flights.begin(); to calculate flight minimum which results in segmetation and performance issues

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment