STQS1313 Computer Programming PDF

Summary

These notes cover fundamental concepts in C++ programming, focusing on input/output (I/O) techniques and predefined mathematical functions. Topics include using the `cin` and `cout` objects, whitespace characters, and common mathematical functions like `abs`, `pow`, `sqrt`, and others.

Full Transcript

STQS1313 COMPUTER PROGRAMMING Adapted from C++ Programming: From Problem Analysis to Program Design, 8th edition by D. S. Malik TOPIC 3: INPUT & OUTPUT (I/O) TECHNIQUES (Part A) 1) Remember that we use:  cout and the insertion operator > to get data from the keyboard. 2) In C++, I/O i...

STQS1313 COMPUTER PROGRAMMING Adapted from C++ Programming: From Problem Analysis to Program Design, 8th edition by D. S. Malik TOPIC 3: INPUT & OUTPUT (I/O) TECHNIQUES (Part A) 1) Remember that we use:  cout and the insertion operator > to get data from the keyboard. 2) In C++, I/O is a sequence of bytes, called a stream, from the source to the destination. 3) The bytes are usually characters (can also be a graphic image or digital speech). 4) Thus, we have:  Input stream: A sequence of characters from an input device to the computer.  Output stream: A sequence of characters from the computer to an output device. 5) Standard input device: keyboard. 6) Standard output device: screen. 7) To receive data from the keyboard, and to send output to the screen, every C++ program must use the header file iostream. 8) This header file contains the objects cin and cout, which stand for common input and common output, respectively. 9) The variable cin has access to operators and functions that can be used to extract data from the keyboard. cin and the extraction operator >> 1) Recall that if you have double payRate; cin >> payRate; then the number typed on the keyboard will be stored as payRate. 2) If you want to input more than one value, you can do: cin >> variableName1 >> variableName2...; 3) For example: cin >> payRate >> hoursWorked; 4) This is equivalent to cin >> payRate; cin >> hoursWorked; 5) When entering more than one value, you can separate each value by using any of the whitespace characters, which are:  a space,  a tab, or  a return (enter) key. 6) Simple task: What is the difference between the codes below? char x; cout > x; 1 int x; cout > x; 2 double x; cout > x; Mathematical Library Functions (Predefined Functions) 1) You have learnt before about certain mathematical functions such as +, -, *, /, and %. 2) However, there are standard preprogrammed/predefined mathematical functions that can be included in a C++ program. 3) To use the functions, you need to know:  The name of the mathematical function  What the function does  The type of data the function requires  The data type of the result the function returns  How to include the mathematical library. 4) Accessing these functions requires including the mathematical header file cmath using the command #include at the top of the program. 5) Examples of the commonly used mathematical functions: Function name Description Returned value abs(a) |𝑎| Same data type as argument / input pow(a,b) 𝑎𝑏 Same data type as argument sqrt(a) √𝑎 double sin(a) sin 𝑎 (in rad) double cos(a) cos 𝑎 (in rad) double tan(a) tan 𝑎 (in rad) double log(a) ln 𝑎 double log10(a) log10 𝑎 double exp(a) e𝑎 double 6) In each of the function above, a and/or b are called the arguments or parameters of the function. 7) Examples of the use of the functions Function name Returned value abs(-7) 7 abs(-7.362) 7.362 pow(2,5) 32 pow(2.0,5.0) 32.0 log(18) 2.89037 log(18.697) 2.92836 log10(18) 1.25527 log10(18.697) 1.27177 exp(-3) 0.0497871 exp(-3.2) 0.0407622 8) Thus, each function has a form: functionName(data passed to the function); 3 9) Each time a function is used, it’s called into action by giving the name of the function and passing to it any data in the parentheses following the function’s name. This is referred to as invoking or calling the function. 10) You can assign the value returned by a function to another variable. For example, #include #include using namespace std; int main() { int height = 800; double time; time = sqrt(2*height/32.3); // function call cout

Use Quizgecko on...
Browser
Browser