Conventions: Name your C programs as hwxpy.c where x corresponds to the homework number and y corresponds to the problem number.
For example, the C program for homework 2, problem 1 should be named as hw2p1.c.
Write comments to your programs. Programs with no comments will receive PARTIAL credit. For each program that you turn in, at least the following information should be included at the top of the C file:
- Author:
- Date created:
- Brief description of the program:
- input(s):
- output(s):
- brief description or relationship between inputs and outputs
Submission Instructions: Use the Dropbox on D2L to submit your homework. Submit your .c file named hw2p1.c and hw2p2.c via the D2L drop-box
Problem 1 (35 points): A solid ball is fired from a cannon at some angle, α, from the ground with a known initial velocity. Find the horizontal distance, x, that the ball will travel from the cannon, neglecting air friction.
Given that the distance x = 2 vi2 cos(α)sin(α)/g
where vi is an initial velocity which cannot exceed 100 m/s
α is a firing angle (degrees) in which 0 < α < 90 degrees
g is a universal gravitational acceleration constant = 9.81 m/s2
You program
- should ask the user for an initial velocity and a firing angle in degrees and display the horizontal distance, x, that the ball will travel.
- must use if-else statement to make sure that vi and α do not exceed their limit, i.e.
0 ≤ vi ≤ 100 and 0 < α < 90
Note: sin(x) and cos(x) in the math library function should be used. Keep in mind that sin( )
and cos() assume that its input x is in
(degrees/ radians) – find this answer in
your textbook or lecture note if you do not remember/know.
Sample program execution: Red text indicates information entered by the user
Note: Below shows 5 samples of code execution
Enter firing angle (in degrees): 45
Enter initial velocity (in m/s): 40
The horizontal distance that the ball travels is 163.10 m
Enter firing angle (in degrees): 120
Firing angle must be between 0 and 90 degree
Enter firing angle (in degrees): 50
Enter initial velocity (in m/s): 125
Initial velocity must be between 0 and 100 m/s
Enter firing angle (in degrees): 30
Enter initial velocity (in m/s): 75
The horizontal distance that the ball travels is 496.57 m
Enter firing angle (in degrees): 50
Enter initial velocity (in m/s): -5
Initial velocity must be between 0 and 100 m/s
Read me: This is how my code handles the cases when the user entered values that are not in the range that we accept (message is displayed).
Another option is for your program to let a user enter both
values before it makes a decision
whether the entered value(s) are out of range.
Problem 2 (35 points): Finding Average and Standard deviation
Given a list of n data values: m1, m2, … , mn, the average measurement value is defined as
�1 + �2 + ⋯ + ��
�̅̅̅�̅ = �
We are interested in taking the average value of incoming data without having to store all
previous data values in memory. This can be accomplished in the following way (using 3 and 4 data values as an example)
�̅̅̅3̅ =
�1 + �2 + �3
= 3�̅̅̅3̅ = �1 + �2 + �3
3
�̅̅̅4̅ =
�1 + �2 + �3 + �4
=
4
3�̅̅̅3̅ + �4
4
In general, given the nth data value �̅̅̅�̅ , and a new data value mn+1, the new average can be
found by
This is known as a running average.
�̅̅̅�̅̅+̅1̅ =
� ∙ �̅̅̅�̅ + ��+1
� + 1
Once a user enters all numbers, the value of standard deviation can also be calculated as
standard deviation =√
�𝑢�_��𝑢������
�
− 𝑎𝑣𝑒��𝑎��𝑒 2 = √
�𝑢�_��𝑢���𝑒�
�
− �̅̅̅�̅2
Note: sum_squares = (�1 )2 + (�2)2 + ⋯ + (�� )2. Your program should also find the
accumulated sum of each data squared after each data is entered.
Write a C program that:
(i) Prompts the user for a measurement value.
(ii) Displays the current running average for all data entered thus far.
(iii) Displays the accumulated sum of each data squared for all data entered thus far.
(iv) Terminates after 8 values of data are entered and display the running average and standard deviation
Sample code execution: Red text indicates information entered by the user