Home >  T.A >  cs10 ( Winter 2002 ) >  Lab 6

  CS10 Sec#25, # 29 : Topics covererd in Lab #6
T.A: Demetris Zeinalipour
(Winter 2002)
Print this Page   
 [  Pre-packaged Functions  ::   Style Considerations  ::   Exercise #6 ]



Pre-packaged Functions

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

C Standard Libraries

( Use these libraries and add powerful functionality to your programs )

stdio.h :: stdlib.h :: string.h :: time.h :: iomanip.h :: math.h  ]
Top


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


Lab 6 Assignment - Exercise
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.

Top