Starting from:
$24.99

$18.99

Homework Assignment 2 Solution

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

 

Enter data value: -68.47

Running average of 1 value is -68.47

Accumulated sum of squares of 1 value is 4688.14

 

Enter data value: 94.11

Running average of 2 value is 12.82

Accumulated sum of squares of 2 value is 13544.83

 

Enter data value: 91.42

Running average of 3 value is 39.02

Accumulated sum of squares of 3 value is 21902.45

 

Enter data value: -2.9

Running average of 4 value is 28.54

Accumulated sum of squares of 4 value is 21910.86

 

Enter data value: 60.04

Running average of 5 value is 34.84

Accumulated sum of squares of 5 value is 25515.66

 

Enter data value: -71.66

Running average of 6 value is 17.09

Accumulated sum of squares of 6 value is 30650.82

 

Enter data value: -60.12

Running average of 7 value is 6.06

Accumulated sum of squares of 7 value is 34265.23

 

Enter data value: 58.44

Running average of 8 value is 12.61

Accumulated sum of squares of 8 value is 37680.46

 

Average of 8 values is 12.61

Standard deviation is 67.462

 

 

 

More sample code execution on the next page

Test cases:

1) 8 data values:        4.46   0    5.68    8.82    4.86   -1.6    5.3    6.8

Correct answers: Running average =  4.29,  Standard deviation  = 3.225



 

2) 8 data values:       -9.36  12.3 -4.16   -9  -10.25  -15.8   15.6  -20

Correct answers: Running average = -5.08, Standard deviation = 11.877



Lab 2 Assignment (30 points): The assignment will be handed out during your lab time.

More products