Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Critical Thinking Questions 1. Convert the following mathematics expressions to C++ expressions. Use the declarations provided. Indicate the appropriate header file for any library function

Critical Thinking Questions

1. Convert the following mathematics expressions to C++ expressions. Use the declarations provided. Indicate the appropriate header file for any library function used. Be sure you initialize any variables whose values you are using to reasonable values for the library functions you are using.

int x, y; //declaration for a) and b)

a) y = x3

b) y <= |x| double x, y, z, area; //declaration for c) through e).

c) z = x^1.6

d) z = area^sqrt(area)

e) p = (x^2+y) / (x^2-y)

2. Write code that declares x, y, and z as double variables. Then write code that causes z to be assigned the result of x divided by y, rounded as indicated below. Be sure to #include the header file that declares the library functions you use.

a) round up

b) round down

c) round to the nearest integer. Does your code round an exact half up or down? say 2.5?

3. Define a function named average_grade. This function returns a double and has four double arguments, test1, test2, test3, test4. The return value should be the average, or arithmetic mean of the four arguments. Be sure to include a comment that tells briefly what the function does.

4. Given the function declaration (prototype), does the compiler complain or compile if you call this using the following line? If the compiler complains, what is the complaint?

//if score >= min_to_pass, returns 'P' for passing,

//else returns 'F' for failing.

char grade (int score, int min_to_pass);

int main()

{

double fscore;

char fgrade;

int need_to_pass;

//omitted code to get values for variables

//fscore and need

fgrade = grade(fscore,need);

return 0;

}

5. Here is a complete function that purports to return one of the roots of a quadratic given suitable parameters. It looks good, but fails to compile. Why?

//returns one of the roots of the quadratic equation

//a*x*x + b*x + c = 0 a!= 0

double root1 (double a, double b, double c)

{

return (-b + sqrt(b*b-4*a*c))/(2*a);

}

6. Write a prototype and prototype comments for the sqrt library function.

7. Give the output from this code fragment:

int *p1, *p2;

p1 = new int;

p2 = new int;

*p1 = 10;

*p2 = 20;

cout << *p1 << << *p2 << endl;

*p1 = *p2;

cout << *p1 << << *p2 << endl;

*p1 = 30;

cout << *p1 << << *p2 << endl;

8. Your program creates a dynamically allocated array as follows:

int *entry; entry = new int[10];

so that the pointer variable entry is pointing to the dynamically allocated array. Write code to fill this array with 10 numbers typed in at the keyboard.

9. Write a recursive version of this iterative function:

int g(int n)

{

int h = 1;

while (n > 1)

{

h = h * n; n--;

}

return h;

}

10. Here is a recursive function that is supposed to return the factorial. Identify the line(s) with the logical error(s). Hint: This compiles and runs, and it computes something. What is it?

int fact( int n ) //a

{

int f = 1; //b

if ( 0 == n || 1 == n ) //c

return f; //d

else

{

f = fact(n - 1); //f

f = (n-1) * f; //g

return f; //h

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions