Exam 2 Short Answers

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Implement a function void my_swap(int& a, int& b). It swaps two values of the actual parameters.

void my_swap(int& a, int& b) { int temp; temp = a; a = b; b = temp; }

Implement a function void root(double a, double b, double c, double& x1, double& x2) which assigns two solutions of quadratic equation ax^2 + bx + c = 0 to variables x1 and x2.

void root(double a, double b, double c, double& x1, double& x2) { double d = b*b - 4.0 * a * c; if(d<0) { cout << "no real roots" << endl; } elseif (d==0) { x1 = -b/(2.0*a); x2 = x1; } elseif (d>0) { x1 = (-b + sqrt(d))/(2.0*a); x2 = (-b - sqrt(d))/(2.0*a); } }

Implement a function: double root(double a, double b, double c) which returns the quadratic formula.

#include <iostream> #include <cmath> using namespace std; doubleroot(double a, double b, double c) { double root1=0; double d = b*b - 4.0 * a * c; if(d < 0) { cout << "no real roots" << endl; } elseif(d == 0) { root1 = -b/(2.0*a); } elseif(d > 0) { root1 = (-b + sqrt(d))/(2.0*a); } return root1; }

Write a stub for function double totalPrice(double single_price, int total_items)

#include <iostream> using namespace std; double totalPrice(double single_price, int total_items) { return single_price*total_items) } int main( ) { double price, total_items; cout << "Enter the item price: "; cin >> price; cout << "Enter the number of items: "; cin >> total_items; cout << "Total cost: " << totalPrice(price, total_items) << endl; return 0; } Output: Enter the item price: 5.5 Enter the number of items: 10

Write an if statement to halt to program if the program fails to open a file. Assume the file to be opened has name stats.dat.

inStream.open("stats.dat"); if(inStream.fail()) { exit(1); }

Write a void function definition for a function called zero_both that has 2 reference parameters, both of which are variables of type int, and sets the values of both variables to 0.

void zeroBoth(int& a, int& b) { a = 0; b = 0; }


Set pelajaran terkait

Consumer Behavior (Ch. 8-11 Quizzes)

View Set

SCOM372/4 Ch. 14 Transportation MCQ

View Set

Nursing Assessment: Immune Function

View Set

Communication 101 Final Study Guide (Quizzes)

View Set

CSET 215 Domain 2: Physical Sciences

View Set

Leaderships in Organizations Exam 2: 6, 7, 10, and 11

View Set

Lesson 7 - Estructura 7.4 - El viaje - InstructionsYou and your friend are packing and planning your upcoming vacation to the Caribbean. Rewrite her sentences, substituting the subject with the one in parentheses. Make all the necessary changes.

View Set