1.
Answer 1
4.0 |
4.0 |
8.0 |
8.0 |
8.0 |
1.21 |
3 |
3 |
0 |
3.0 |
3.5 |
3.5 |
6.0 |
6.0 |
5.0 |
5.0 |
4.5 |
4.5 |
3 |
3.0 |
3.0 |
2.
Answer 2
sqrt(x + y) |
pow(x, y + 7) |
sqrt(area + fudge) |
sqrt( time + tide) / nobody |
(-b + sqrt(b * b) - (4 * a * c))/ (2 * a) |
abs(x - y) |
3.
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void main()
{
/*
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
*/
const double PI = 3.14159;
cout << sqrt(PI) << endl;
/*
1.77245
Press any key to continue . . .
*/
}
4a. Using Microsoft Visual Studio the #include <iostream> can be at any position on line.
4b. Using Microsoft Visual Studio spaces are allowed between # and <iostream>.
5. Wow
6.
int sum(int,int,int); //function declaration
void main ()
{
cout << sum(1,2,3) << endl; // function call
}
int sum (int a, int b, int c) // function header
{
return = (a + b + c);
}
7.
double div(int,double); // function declaration
void main()
{
cout << div(22,7.0) << endl; // function call
}
double div (int a, double b) // function header
{
return (a/b);
}
8.
char check(double); // function declaration
void main()
{
cout << check(6.0) << endl; // function call
}
char check (double a)
{
if (a > 0)
return 'P';
else
return 'N';
}
9. Call-by-value mechanism is when a value of function call arguments are passed &
substituted in the call-by-value formal parameters of the function header.
10. predefined functions are well tested & known. user-defined functions are
customizable to user situation which can be advantageous
11.
bool in_order (int a, int b, int c)
{
if ( a <= b ) && ( b <= c)
return true;
else
return false;
}
12.
bool even (int a)
{
if ( a%2 == 0)
return true;
else
return false;
}
13.
bool is_digit (char a)
{
if (a == '.')
return true;
else
return false;
}
14.
bool is_root_of (int a, int b)
{
if ( a * a == b)
return true;
else
return false;
}
15. The purpose of the function declaration comment is to let programmers know all conditions
that are required of the argument to the function & describe the returned value.
16. Principle of procedural abstraction simply means that the programmer should know that a "black box" (a function)
works as described but the programmer doesn't need to know the details of how a particular function definition is coded.
17. Means the programmer can use the "black box" (a function) without knowing exactly how the function definition is coded.
18. Program testing means tossing input into a program when you know what the answer should be to confirm the program
operates as intended. You would continue tossing input into program to test all the functionality that that branches
all operate as intended & loops stop when intended.
19. Yes, the denominator is functionaly equivalent. Using static_cast<double> makes your meaning more clear to some.
20. In the function definition.
21. Yes, program will compile. Yes, program will run. No, no error messages. Yes, output will be correct.
22. If the variable inches is a global variable then function should be fine. Else the compiler would not
compile code since inches is not declared.
23.
double read_filter(); // function declaration
// Prompts user to enter a value of type double.
// Function returns value entered if >= zero else returns zero.
double read_filter() // function header
{
using namespace std;
double a;
cout << "Enter a number & press Enter:\n:";
cin >> a;
if (a >= 0)
return a;
else
return 0.0;
}
24. int score(double points); would be used since only one argument is passed to function
25. double the_answer(double data1, double data2); would be used since both arguments passed are of type double
26. double the_answer(double time, int count); would be used since the function call more closely matches the function
definition (int=int & double would be converted to int).
27. double the_answer(double data1, double data2); would be used since the second argument
is of type double which matches
28. Yes, you could make the square length of type double or go the ugly route and ask the user to input each side
of a square pizza as two variables of type int. You could not use one variable for lenght of type int though as
that would confuse or error out compiler.
29. Function unitprice as written does not utilize anything from that library.
To CS200 main page