Learn C++ (The Correct Way) - Lesson 2 - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20) +---- Thread: Learn C++ (The Correct Way) - Lesson 2 (/showthread.php?tid=169) Pages:
1
2
|
Learn C++ (The Correct Way) - Lesson 2 - Code King - 10-05-2009 Hi, this is lesson 2 in learning C++ the proper way. We will discuss basic math operations and converting data. So you should have an IDE setup and ready and know the basics of input and output from lesson 1. We will use the skills we learnt there and apply them here into making programs that could be useful to someone. This tutorial is split up into two sections: Section 1 – Maths Basics, Section 2 – Conversion Basics. First I will show you a simple application and explain line by line then i will post source code for real world programs that can be customised by you to perform a different operation, so let’s begin. (Note: I welcome all criticism and comments on this tutorial). Basic Maths First open up the IDE of your choice and create a new source file with a .cpp extension, for the purpose of the tutorial I will call it BasicMath.cpp. So like last time we will have the same program structure: 1. Include Files 2. Declare Standard Namespace 3. Start the main program loop 4. Then our application code 5. Then code to prevent application from ending before we see the results 6. Then tell the compiler to stop executing the main loop For simple maths like +,-,*,/ you don’t need any extra includes. So our first line is the same as lesson 1: Code: #include <iostream> Code: using namespace std; Code: int main(){ Code: cout << "3 + 4 = " << 3 + 4 << "" << endl; Code: cout << "3 - 4 = " << 3 - 4 << "" << endl; This program does not require any input using the cin function so we can use cin.ingore(); instead of system(“PAUSEâ€);. If you try using the cin.ignore(); or cin.get(); with a program using the cin function then it will prevent the program from working correctly. Code: cin.ignore(); Code: return 0; Full source code listing for BasicMath.cpp Code: #include <iostream> So our first two lines are: Code: #include <iostream> // Basic Input/Output Code: [code]using namespace std; Code: float opposite; Code: cout << "Please enter the opposite length" << endl; Pythagoras is defined as a2=b2+c2 if we want to find the hypotenuse, if we want to find b or c then we can jus rearrange the equation to fit our needs but for this program we are going to find A (hypotenuse). So how can we write a2=b2+c2 in a way that our C++program can used to calculate A. Well we can use the sqrt() function from the math.h we included earlier. But first we need to do b2+c2, then square root our answer. This can be done by the following line: Code: cout << "The length of the hypotenuse is " << sqrt(((opposite*opposite) + (adjacent*adjacent))) << "" << endl; Code: system(“PAUSEâ€); Complete source code: Code: #include <iostream> So now we can do maths and we can create math based programs. We can now move onto just as practical programs that involve calculations. Conversion can be done exactly like how we the pythag solver but may not always require the math.h include. Instead of saying what each line of code does I will just add a comment after the line of code. This program will convert degrees to radians, the equation to do so is: Code: Pi Radians = Degrees * (Pi/180) There is one thing that is new in the code is the use of #define, this allows the programmer to define variablename value, where going to use this for our value of PI. Code: #include <iostream> // Include standard input/output functions So in this lesson you should have learned basic maths and the use of the math.h file for calculations beyond iostream. Also you should have learned that you can define values yourself so they can be used later. Also that it is simple to convert one number to another using a multi step math calculation. Lesson 3 will be about if and else statements and will use things from what we have learned in lesson 1 and 2. Thanks for reading RE: Learn C++ (The Correct Way) - Lesson 2 - immi - 10-06-2009 uuhoo wonderful tut. Thanks dude ;) RE: Learn C++ (The Correct Way) - Lesson 2 - TheDoctor - 10-07-2009 Really helpful.. Loving your tuts. Helped me out a lot. RE: Learn C++ (The Correct Way) - Lesson 2 - Code King - 10-07-2009 Hey doc, thanks. RE: Learn C++ (The Correct Way) - Lesson 2 - TheDoctor - 10-07-2009 Did you write your lesson on the pointers yet ? RE: Learn C++ (The Correct Way) - Lesson 2 - huso69 - 10-09-2009 nice code king very good code RE: Learn C++ (The Correct Way) - Lesson 2 - Akshay* - 10-09-2009 gud details thax RE: Learn C++ (The Correct Way) - Lesson 2 - bextas - 10-09-2009 make lesson 3 man. i like your tut, good job! RE: Learn C++ (The Correct Way) - Lesson 2 - charnet3d - 10-09-2009 Very good for beginners, Keep Going! RE: Learn C++ (The Correct Way) - Lesson 2 - Malevolent - 10-10-2009 hard to learn if ur not doing hands on |