answers to quiz #2 1. 25 ==> 11001 50 ==> 110010 (multiply by 2 by adding a 0) FFFF ==> 10000(base16) - 1 ===> 65535 1010101100 ==> 10,1010,1100 ==> 2AC (break into groups of 4 digits) 1010101100/4 ==> 10101011 (divide by 2 twice by dropping right hand 0s) 2 x 55F ==> 55F + 55F ==> ABE rank ==> 8 suit ==> 4 2. One difficulty is how to count the factors. For n=1, does that mean x, or x * (1-x^2/pi^2) ? Assuming n=1 means the result is x: double sinApprox(double x, int n) { double pi = atan2(1.0, 0.0) * 2.0; double result = x; int i = 1; double term = 0; // can initialize to anything while ( i < n ) { term = 1 - x*x/(i*i*pi*pi); // careful with parens result = result * term; i = i + 1; } return result; } If you took "one factor" to mean x * something: double sinApprox(double x, int n) { double pi = atan2(1.0, 0.0) * 2.0; double result = x; int i = 0; double term = 0; // can initialize to anything while ( i < n ) { i = i + 1; term = 1 - x*x/(i*i*pi*pi); // careful with parens result = result * term; } return result; } 3. bool ImPositive(vector x) { int pos=0, neg=0; for (int i=0; i 0) // excluding 0s as neither pos nor neg { pos = pos + 1; } } return (pos > neg); } The return line is equivalent to: if (pos > neg) { return true; } else { return false; } 4. Note there are various ways to fix this program #include using namespace std; int main() { int N; <==== moved before cin cout << "Will calculate Fibonacci(N). Input N: "; <==== added ; cin >> N; <==== should be N < 1 //if (N > 1) <==== removing line fixes {while(N < 1) {cout << "F(" << N << ") does not exist. N must be >= 1. Try again." << endl; cin >> N; } } int F, FMinus1 = 1,FMinus2 = 1; if (N == 1 || N == 2) //; <==== was && (and) {F = 1;} <==== also removed ; else //if <==== removed "if" {for (int i = 3; i <= N; i = i + 1) <==== added "int" {F = FMinus2 + FMinus1; FMinus2 = FMinus1; <==== switched order FMinus1 = F; <==== of these two lines } } cout << "F(" << N << ") = " << F << endl; <==== added << before F return 0; } 5. #include using namespace std; int main() { double a, b, c; cout << " Enter quadratic coefficients a, b, and c " << endl; cin >> a >> b >> c; double disc = b*b - 4*a*c; double r1 = -b/(2*a); if (disc == 0) { cout << " This has one real root at x = " << r1 << endl; } else if (disc < 0) { cout << " This has no real roots " << endl; } else { double r2 = sqrt(disc) / (2*a); cout << " This has two real roots at x = " << r1 + r2 << " and at x = " << r1 - r2 << endl; } }