
|
|
[
Pre-packaged Functions ::
Style Considerations ::
Exercise #6 ]

|
When given a problem, you many time see that you need a specific function in order to accomplish your task.
Such a function may be the sin of an angle. For this reason we can use Pre-packaged Functions.
The only thing we need to do is to scan the documentation of a particular library and try to find the
function that meets our needs. After that we have to include the library that contains this function and then
we can call it directly.
|
Example using (sin) from (cmath).
/* sin example */
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
void main ()
{
double param, result;
param = 45;
result = sin (param*PI/180);
cout << "Sine of << param << " degrees is :" << result;
}
Output:
Sine of 45 degrees is 0.707107
|
( Use these libraries and add powerful functionality to your programs )
[ ::
::
::
::
::
]
|
Top

|
(See C++ Style Guidelines handout posted on Pr.Kelsey web page.)
- Use blank lines to separate set of related instructions.
- Liberally use clear and helpful comments.
- Make comments stand out - end of line (lined up) or blank line above comment.
- Type each statement on a separate line.
- Avoid running a statement over multiple lines.
- Avoid line splicing (line too long)
- Indent lines after a curly brace by the same amount until the end curly brace.
- Use whitespace in typing statements (cout<<"Hello all"; cout << Hello all;)
|
Top

|
New! - Download lab6.exe (win32) here and see how your program should behave to various inputs,
See Solution - C++ solution here (will be available on Saturday)
Write a program that randomly generates test questions. Let the user choose whether the questions will be addition or multiplication questions. Then your program should randomly generate two operands each for 5 test questions. Print out each test question (ex. 34 + 18 = ). The range of values for addition questions should be 1-100 and for multiplication questions should be 0-12. Allow the user to choose a test type and print 5 question of that type as many times as they want.
Notice: Be sure to put all your information (login, lab section, ) at the top of your program. Your lab programs DO NOT get turned in electronically. I will come around and check you off individually.
|