# Control Structure using C++ Assignment Solution

## Calendar, momentum and collision Assignment Solution

1.

January 1st, 2000 was a Saturday. Write a C++ assignment to calculate the day of the week for any date between January 1st, 2000 and December 31st, 2999. What will the day of the week be on March 3rd, 2783? Do not use any material not yet covered in the course in your answer.

Note: Remember to account for leap years, which occur in every year divisible by 4 unless also divisible by 100 and not divisible by 400.

Note: Many formulas are readily available on the internet to solve this question. The point of this question is to develop your own implementation, learning how to use logic and loops. As such, any answers to this question that are in any way derived from solutions on the internet will receive zero marks, this includes implementations of Zeller’s formula, Guass’s algorithm, etc.

2. Consider figure 1. Two masses, m1 and m2, sit on a frictionless surface in a vacuum, such that all collisions are elastic. m2 is moving towards m1 with velocity v2. Behind m1 sits a stationary wall of infinite mass such that any mass that hits the wall will rebound with equal but opposite momentum. Write a computer program to calculate the number of collisions, c, that will occur in the system before m2 and m1 are both moving away from the wall with the velocity of mass, m2, greater than the velocity of mass, m1. A collision is considered to be any time that the masses collide or whenever m1 collides with the wall. Your program should begin the simulation with v2 towards m1 and the wall e.g. v1 = 0 and v2 = −1. How many collisions occur when:

- m2 = 100× m1

- m2 = 10, 000× m1

-m2 = 1, 000, 000× m1

-m2 = m1 × 10^10

Without running your program, how many collisions will occur if m2 = m1 × 10^30?

The above results should be added to your written answers pdf file. Your program should include a std::cin statement to allow the user to set the masses of m1 and m2. Additionally, your program should contain logic to allow the user to rerun the program (and enter new masses) without having to recompile.

Note: No knowledge of distances between masses and the wall is needed, additionally the number of collisions is independent of the initial velocities (other than requiring m2 have a velocity in the direction of m1).

Hint: If m1 = m2, c = 3; if m2 = 50 × m1, c = 22.

3.

The provided file ‘sampledata.txt’ contains a sample of data thought to be generated by a process known to produce values with a mean of 40. Write a program to read in sampledata.txt and perform a two-tailed single value ‘t-test’ to verify if this sample has a significantly different mean from the overall population. For the purposes of this test, we will use significance level α of 0.05 as the benchmark for proof. 2 To calculate the test statistic t use the following formula:

where ¯x is the sample mean, µ0 is the population mean, s is the sample standard deviation, and n is the sample size. To test the hypothesis compare the value of your test statistic to the table available in the following link:

http://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm The ‘degrees of freedom’ index in the table is simply one less than the number of entries in your dataset. If the test statistic is greater than the value in the table, known as the ‘critical value’, or less than it if the statistic is negative, then you have established that the difference in means is significant. State your conclusion with reference to your calculated statistics. Your code should be able to safely handle problems with any incoming data.

Solution:

Question 1:

```#include < iostream> #include < cmath> using namespace std; void calcu(int, int, int); int main () { calcu(2783, 3, 3); int day = 0, month = 0, year = 0; //for user input int totaldays; while (day > 31 or day < 1) { cout << "Enter Day"; cin >> day; } while (month > 12 or month < 1) { cout << "Enter Month"; cin >> month; } while (year < 2000 or year > 2999) { cout << "Enter Year"; cin >> year; } calcu(year, month, day); } void calcu(int year, int month, int day){ int totaldays = 0; if (year > 2000) { totaldays = (year - 2001) * 365.25 + 366; } //counts total days up til your year, accounting for yr 2000 else { totaldays = 0; } const int StartOfEachMonth[] = { 0,31,59,90,120,151,181,212,243,273,304,334 }; //day number each month starts on totaldays = totaldays - int(((year - 2001) / 100) + int(year - 2001) / 400); totaldays = totaldays + StartOfEachMonth[month - 1]; if (month > 2 and year % 4 == 0) { totaldays = totaldays - 1; } const string DaysOfWeek = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; totaldays = totaldays + day; //cout << "Total days since January 1st 2000 are " << totaldays << endl; totaldays = totaldays - 1; int remainder = totaldays % 7; cout << year << "/" << month << "/" << day << " is " << DaysOfWeek[remainder]<```

Question 2:

```#include < iostream> #include < cmath> using namespace std; int cal_Collision(double); int main() { while (1) { cout << "Input M/m " << endl; double times = 0; cin >> times; if (times == 0) break; cout << "number of collisions are " << cal_Collision(1 / times) << endl; } } int cal_Collision(double m) { double ini_v = 0, ini_V = 1; double collision = 0; while (ini_V > ini_v) { collision++; double old_v = ini_v; double old_V = ini_V; ini_v = ((m - 1) * old_v + 2 * old_V) / (1 + m); ini_V = (2 * m * old_v + (1 - m) * old_V) / (1 + m); if (ini_v > 0) { collision++; ini_v = -ini_v; } } return collision; } ```

Question 3:

``` #include < iostream> #include < fstream> #include < string> #include < vector> #include < math.h> using namespace std; int main() { vector array; ifstream File; File.open("sampledata.txt"); float n; while (File >> n) { array.push_back(n); } File.close(); float sum = 0.0; for (auto n : array) { sum += n; } float pmean = sum / (array.size() - 1); float smean = sum / array.size(); float pop = 0.0; for (auto n : array) { pop += pow((n-smean),2); } float ssd = sqrt(pop / (array.size() - 1)); float tst = (smean - 40) * sqrt(array.size()) / ssd ; //cout << "smean " << smean << endl; //cout << "std " << ssd << endl; cout << "the test statistic t " << tst << endl; return 0; } ```