Part-1
Write your programs in your 2010/5/ directory.
Do this...
1. Log on to Odin
2. $ cd 2010
3. $ ./lab-start.sh
4. $ cd 5
Average program
---------------
Name your program:
2010/5/lab5a.cpp
In your main function...
1. Declare three integer variables.
2. Store a value in each variable.
Use the rand() function to generate values from 0 to 100.
3. Write a function that will calculate the average of 3 values.
4. Pass your 3 variables to a function that will calculate the average,
and return the average as a floating-point value.
You may use type casting with static_cast to get decimal precision
in your average value returned.
You might try dividing by 3.0
- Call your function from main.
- Display the average in main.
- Show 3 decimal-places of accuracy.
- Your function will not print anything.
- Only printing from main() is allowed.
Sample output...
$ ./a.out
Lab-5 average program.
Your three values are: 32 32 54
The average is: 39.333
Add command-line arguments
--------------------------
Copy your lab5a.cpp to lab5b.cpp
$ cp lab5a.cpp lab5b.cpp
$ vi lab5b.cpp
1. Add command-line arguments to your main() function.
Make your main function header look like this:
int main(int argc, char *argv[])
---- -------
| |
| +------ the argument text as cstrings
+------------------- the argument count
2. Check for an argument entered by the user.
if (argc > 1) {
//seed the random number generator
int seed = atoi(argv[1]);
srand(seed);
}
Sample output with argument entered by user...
$ ./a.out 16
Lab-5 average program.
Your three values are: 67 71 26
The average is: 54.667
Falling distance
----------------
Name your program
2010/5/lab5c.cpp
When an object is falling because of gravity, the following formula can be used
to determine the distance the object falls in a specific time period:
d = (1/2)gt2
Distance equals one half g t squared.
The variables in the formula are as follows:
d is the distance in meters
g is a constant value of 9.8
t is the amount of time, in seconds, that the object has been falling.
Write a function named fallingDistance that accepts an object's
falling time (in seconds) as an argument. The function should return
the distance, in meters, that the object has fallen during that time
interval. Write a program that demonstrates the function by calling it
in a loop that passes the values 1 through 10 as arguments and
displays the return value.
Call your function 10 times from main().
Sample output...
$ ./a.out
Lab-5 Object's falling distance due to gravity
time distance
---- --------
1.0 4.9
2.0 19.6
3.0 44.1
4.0 78.4
5.0 122.5
6.0 176.4
7.0 240.1
8.0 313.6
9.0 396.9
10.0 490.0