LEC03_Notes - 1 14:440:127 Introduction to Computers for...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 14:440:127– Introduction to Computers for Engineers Notes for Lecture 03 Rutgers University, Spring 2011 Instructor - Brenda V. Cortez 1 Introduction In these lecture notes, you will learn about the built-in math functions that you can use in your code to calculate things like the absolute value or the square root of a value. Following this topic, we will reconsider the previously mentioned example of the Professor who wants to know the average of the scores in his/her class. It seems more plausible that the Professor would want to access the individual student’s score after it has been entered; perhaps to compare it to the average. However, we currently do not have the tools to store individual values as a set, for later consideration. To solve this problem, we will introduce and implement arrays . Later in the semester, when we move on to MatLab scripting, it will become more evident why learning arrays now is so important - an array is the main data type in MatLab! After thoroughly reading and doing the exercises in these lecture notes, you should not only be familiar with the concept of arrays , but you should also be able to work with strings and other header files that provide further functionality to a program. 2 C++ Standard Library Header Files & Built-in Functions You already know that if you want to use the C++ standard i nput and o utput functions, you must include the <iostream> header file in your program; but this is not the only header file available for use. For instance, we can also include <cmath> , which contains function prototypes for typical mathematical operations such as the exponent x y , pow(x,y) , or the trigonometric sine of x , sin(x) . When using the <cmath> header file, include it in your code, right after <iostream> . Consider the following example illustrating the mathematical functions you have available for use: Example 1: #include <iostream> #include <cmath> using namespace std; int main(){ int x = 45, y = 25, z = 2; float v = -3.5, u = 1.5; // ceil(): Ceiling, always rounds up
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 cout << "Ceiling u = " << u << " rounds it up to " << ceil(u) << endl; cout << "Ceiling v = " << v << " rounds it up to " << ceil(v) << endl; // floor(): Floor, always rounds down cout << "Flooring u = " << u << " floors it down to " << floor(u) << endl; cout << "Flooring v = " << v << " floors it down to " << floor(v) << endl; // to calculate the cosine of x (radians) cout << "The cosine of x = " << x << " radians is " << cos(x) << endl; // to calculate the sine of x (radians) cout << "The sine of x = " << x << " radians is " << sin(x) << endl; // to calculate the tangent of x (radians) cout << "The tangent of x = " << x << " radians is " << tan(x) << endl; // to calculate exponential e to the power of variable cout << "e^"<< z << " = "<< exp(z) << endl; // to take square root of a positive number cout << "The square root of y = " << y << " is " << sqrt(y) << endl; // to calculate absolute value of variable cout << "The absolute value of v = " << v << " is " << fabs(v) << endl; // to calculate the natural logarithm of x (base e) cout << "The natural log of u = " << u << " is " << log(u) << endl; // to calculate the logarithm of x (base 10) cout << "The base 10 logarithm of x = " << x << " is " << log10(x) << endl; return 0; } You should definitely write/compile/run this code, so that you can become familiar with using the
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern