// Question 1 (15 points)// demonstrate some basic knowledge of syntax#include <iostream>#include <cstdlib>using namespace std;int main () { int x; double y; bool z; x = rand(); y = 3.1415926; z = true; x += 1000; y *= 2.718281828; cout << x << endl << y << endl << z << endl; return 0;}// Question 2 (15 points)// demonstrate some basic knowledge of flow control (loops)#include <iostream>#include <cstdlib>using namespace std;int main () { for (int c = 0; c < 1000; ++c) cout << "hello" << endl; for (int c = 1000; c <= 0; c -= 5) cout << c << endl; double sum = 0; for (int c = 0; c < 1000; ++c) sum += rand(); cout << sum << endl; return 0;}// Question 3 (15 points)// demonstrate some basic knowledge of flow control (conditionals)#include <iostream>#include <cstdlib>using namespace std;int main () { int x, y; x = rand(); y = rand(); x += rand(); y += rand(); if (x % 3) x += 100; if (y < 1000) y += 1000; cout << (x+y)/2 + (x+y)%2 << endl; // "If x and y are both even or both odd, // print (x+y)/2, else print (x+y+1)/2". // About the only vaguely interesting thing // on this exam. return 0;}// Question 4 (15 points)// demonstrate some basic knowledge of STL vectors and file operations// read some values from a file and compute their sum and product#include <fstream>#include <iostream>#include <vector>using namespace std;int main () { ifstream in ("C:\\MAS110\\numbers.txt"); vector<double> x; double y; while (in >> y) x.push_back(y); double sum = 0, prod = 1, n = x.size(); for (int c = 0; c < n; ++c) { sum += x[c]; prod *= x[c]; } cout << "Sum: " << sum << endl << "Product: " << prod << endl; return 0;}// Question 5 (10 points)// demonstrate some basic knowledge of object instantiation and utilization// uses a given class definition to solve equation systems of the form// ax + by = 1 and x + y = 1#include <iostream>#include <cstdlib>using namespace std;#include "given.h" // see belowint main () { SolveSystem x; cout << "Solutions for a = 10, b = 15: " << endl; x.SetCoefficients(10, 15); x.Solve(); cout << "Solutions for a = -20, b = 9: " << endl; x.SetCoefficients(-20, 9); x.Solve(); return 0;}// "given.h" as given to us on the exam paper for question 5, verbatim, including// weird indentation and bracingclass SolveSystem{private: double a,b;public: void SetCoefficients(double a1,double b1) { a=a1; b=b1; } void Solve() { if(fabs(a-b)<1e-10) cout << "no unique solution"; else { cout << "x= " << (1-b)/(a-b) << endl; cout << "y= " << (a-1)/(a-b) << endl; } }};// Question 6 (15 points)// demonstrate some basic knowledge of STL lists and iterators#include <iostream>#include <list>#include <iterator>using namespace std;int main () { list<int> L1, L2; for (int c = 0; c < 10; ++c) { L1.push_front(c); L2.push_front(c+5); } L1.push_front(100); L2.push_back(-100); L1.pop_back(); L2.pop_front(); for (list<int>::iterator I = L1.start(); I != L1.end(); ++I) cout << *I << endl; for (list<int>::iterator I = L2.start(); I != L2.end(); ++I) cout << *I << endl; return 0;}// Question 7 (15 points) part a// demonstrate some basic knowledge of how to write a function and maybe some "ingenuity"// write a function that tests whether twice a number is equal to the sum of the positive// factors of the number#include <iostream>using namespace std;bool IsPerfect (int n) { if (n <= 5) return false; int a = n - 1; for (int c = 2; c < n; ++c) if (!(n % c)) a -= c; return !a;}int main () { for (int c = 1; c <= 10000; ++c) if (IsPerfect(c)) cout << c << endl; return 0;}// Question 7 (15 points) part b// demonstrate some "ingenuity"// write a function that returns the ternary representation of a given integer, LSB first#include <vector>vector<int> Ternary (int n) { vector<int> x; while (n) { x.push_back(n % 3); n /= 3; } return x;}// end