In this lab assignment you will practice:
for
, while
, and do … while
looping statementsDownload the following source code and add it to your lab source folder.
Download interest.cWrap the entire program in an infinite loop so that you can enter many different principal amounts without having to restart the program each time.
You can create a loop that will run indefinitely by using a constant, non-zero condition value. For example:
while (1) {
// inside here will be repeated forever
}
If you import <stdbool.h>
, you could also use the true
constant to make it a little more readable.
Many C programmers prefer to use for
loops instead. Since we want the loop to run forever we can leave out a lot of the usual details and distill the syntax down to just this:
for (;;) {
// inside here will be repeated forever
}
These two examples are functionally identical, so choose the one that makes the most sense to you.
There are many other ways you can make an infinite loop, but these two approaches are the most idiomatic way that other programmers will recognize and will make it clear that you really meant to do this!
Consider which of the variables in this program need to be freshly created each time your program loops, and which can be created just once when your program starts. Move variables outside of the infinite loop if it makes sense to do so!
Variables that are only used to control a loop (iteration variables) should usually be declared with the loop. Constants should always be declared at the beginning of your program.
Use a second, nested inifinite loop, along with conditional branches and the break
keyword to make sure that the user enters a usable dollar value for the pricipal amount (ie. greater than 0) before allowing the program to continue calculating interest.
This example requires the user to enter a negative integer before continuing:
Download input-check.c
Create a program that asks the user to enter a number and prints an inverted triangle with that many rows (or some other interesting pattern) using nested for
loops.
Given "5", output:
***** **** *** ** *
Given "13", output:
************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** *
for
loop.Wrap this code in another for
loop to repeat it and create a square of asterisks.
for
loop to limit the number of asterisks printed based on the state of the outer loop's variables.Experiment with different looping conditions, conditional branches, and extra inputs to create different patterns.
Let the user choose what symbols to use for drawing
Replace the asterisks on the diagonal edge with a different character
$ *$ **$
Print the triangle facing different directions
* ** ***
Or like:
* *** *****
Output a diamond instead of a triangle
* *** ***** *** *
Create a program that will calculate the height and velocity of a falling object over time, taking into account non-linear atmospheric drag. To keep things relatively simple, assume that the object starts at rest and moves in one direction due to a constant gravitational force, and that the atmospheric density is similarly constant throughout its path of travel. Use an interative numerical approximation approach for your solution.
The force of drag due to atmospheric resistance is given by the formula:
Where C is the drag coefficient, 𝜌 is the atmospheric density, A is the cross-sectional area, and v is the velocity.
Because the drag forces on the falling object depend on the velocity, which in turn depends on the net forces, a purely analytical approach to solving this problem would be quite complicated! Instead, we can simulate the problem much more simply on a computer using numerical approximations at discrete time intervals. We can calculate the acceleration of the object due to the net forces acting on it at a given moment and then assume that this acceleration stays constant over a short period of time. This allows us to approximate the velocity at the end of this time interval, and then use that velocity in the calculation of forces for the next interval.
At any given instant in time, the acceleration is given by:
Where m is the mass of the object, and g is the acceleration due to gravity. Over a small enough interval of time, this acceleration is approximately constant, so the change in velocity and height of the object after a time interval t will be:
The resulting velocity can then be fed back into the acceleration equation for the next time interval!
Your program should minimally accept user inputs for:
You will also need a cross-sectional area measurement, drag coefficient, and atmospheric density, but you can hard-code these values using symbolic constants if you like to keep the program a little simpler.
For each time step, output the simulated time elapsed and the object's instantaneous height and velocity. Your program should end when the object reaches height 0, or when the simulation has reached 2000 iterations, whichever comes first.
Feel free to experiment with different output formatting and different ways of taking in user input. This is just an example!
Gravitational force: 9.806 m/s^2 Atmospheric density (kg/m^3): 1.225 Drag coefficient: 0.5 Mass of falling object (kg): 1.00 Drop height (m): 2.00 Time (s) Height (m) Velocity (m/s) 0.000 2.00 0.00 0.100 1.95 0.98 0.200 1.81 1.93 0.300 1.57 2.80 0.400 1.25 3.54 0.500 0.87 4.14 0.600 0.43 4.59 0.700 0.00 0.00
Completing all tasks in this lab should result in 3 separate files within a single folder named like LastnameFirstname_lab3. Compress the folder into a ZIP archive and upload it to the D2L assignment.
The marks for this lab are heavily weighted towards good coding practice and style. Pay attention to your code formatting, use comments to clarify your code, and make sure you are using defines, arrays, loops, and functions effectively.
NOTE: This assignment is to be done individually. You can help one another with problems and questions, but in the end everyone must write and submit their own code.
Criteria | Marks |
---|---|
Programs compile and run without error | 3 |
Good coding style | 3 |
Task requirements met or exceeded | 3 |
Learning Outcomes achieved | 3 |
Total | 12 |