1 / 11

Math Library Preprocessor Directive

Math Library Preprocessor Directive. #include < cmath > provides access to quite a few mathematical functions. We have already looked at pow ( value,exp ). Here is an example of an integer absolute value function abs ( number ) where number is of type int and abs returns int

elana
Télécharger la présentation

Math Library Preprocessor Directive

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Math Library Preprocessor Directive #include <cmath> provides access to quite a few mathematical functions. We have already looked at pow(value,exp). Here is an example of an integer absolute value function abs (number) where number is of type int and abs returns int ex. int a, b, c, d; a = -33; b = 5; c = abs(a); d = abs(b); cout << “Absolute value of a is “ << c << endl; cout << “Absolute value of b is “ << d ; Output would be: Absolute value of a is 33 Absolute value of b is 5

  2. The Math Library #include <cmath> provides access to the math library of functions. Here are just a few that you may find useful. int abs(int x) – returns the absolute value of integer x double ceil(double x) – returns the smallest integral value that is not less than x double floor(double x) – returns the largest integral value that is not greater than x double pow(double x, double y) – returns value of x to the power of y double sqrt(double x) – returns the positive square root of x Go to http://mathbits.com/MathBits/CompSci/LibraryFunc/Math.htm for a more complete list

  3. Others Available • Trigonometric Functions • Logarithmic Functions • Square Root Function • Modulus with double (% only works with integers)

  4. Using the ‘cin’ Object to Store Values in Variables • cin is the C++ standard input object • reads input from the console • needs cout statement(s) preceding it so that the user can be prompted what to enter and when to enter it • program will wait for keyboard entry until the Enter key is pressed Example: cout << “Please enter the test scores when prompted.\n”; cout << “Please enter score for test number 1: “; cin >> score_test1; cout << “\nPlease enter score for test number 2: “; cin >> score_test2;

  5. Getting Multiple Values cin can be used to get more than one value at a time. HOWEVER • The user must enter the values in the correct order • The values must be separated by a valid blank separator: space, tab, newline • Users can (and will) enter incorrect data Syntax example: intfirst_value, second_value; cin >> first_value >> second_value;

  6. Formatting Your Output So far we have only had one stream manipulator, endl, and an escape sequence, \n, to control the appearance of output. As you might expect there are additional manipulators that can be applied to format the appearance of your output. You will need the following preprocessor directive to use these. #include <iomanip> Stream Manipulator Description setw(n) Sets a field width of a minimum n spaces for next value setprecision(n) Sets the number of significant figures (n) to display except when followed by fixed (Rounding occurs) fixed Displays floating point numbers with a fixed number of decimal places (as set by setprecision) showpoint Used with setprecisionto cause trailing 0’s to be displayed left Causes subsequent output to be left-justified right Causes subsequent output to be right-justified (this is the default)

  7. File Input/Output To get data from or output data to a file requires a sequence of events: • A connection between the file and your program must be established. This is called ‘opening’ a file. • The file needs to be accessed for reading or for writing or for both • Access can be sequential (start at top of file and process line by line) or random (jump around to different points in the file). We will use sequential file access. • File stream objects (similar to console stream objects) are used to associate your program with a specific file • ofstream – used only for output files • ifstream – used only for input files • fstream – used for files requiring both input and output • #include <fstream> preprocessor directive must be included in your program

  8. Example of Output to a File // This is an example of outputting text to a file #include <iostream> #include <fstream> using namespace std; int main() { // Create an output file (ofstream) object ofstreamfout; // Open the output file by passing the file name to the open function of the // ofstream object fout.open(“example.txt”); //Write text to the file object in a fashion similar to cout fout << “Hello everyone!”; //Close the file fout.close(); return 0; }

  9. Combine Create & Open The file stream object create and open can be done in a single statement. (Note: Remember out_file is a name YOU have given to the file stream object.) Syntax: ofstreamout_file(“myoutput_file.txt”); The above statement creates a file stream object out_file and the file myoutput_file.txt is opened in the current default directory. Note: When the above is executed any file in the default location with the same name (myoutput_file.txt) is deleted and then recreated.

  10. Reading Data from a File • The >> operator expects data to be separated by spaces, tabs or newlines (\n). • Data is read sequentially so it must be in the correct order. • There is a hidden character (eof) at the end of every file that indicates the end of the file. • While output files are created, input files MUST exist in the expected location.

  11. Paths & Filenames If a file (input or output) is not located IN THE SAME FOLDER in which the program is executed then you must specify the full path so that the program can locate the input file or create an output file where you want it to be. Note: Since Windows paths require the \ character, you will have to ‘escape’ the \ so the path would have two \’s for every actual \ in the path. Remember that path names are case sensitive. Ex. ifstreamfile_in; //Input file object file_in.open(“C:\\Files\\data_in.txt"); Beware the Unix/Windows newline issues when viewing your output files! On Windows systems, Notepad does not handle this well – use Wordpad.

More Related