Tag Archives: easy

TEA BREAK WITH C++: DATA TYPES

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/ This series you may follow and everyday by reading 10 mins in your tea break you can get the essence.

These are the ones that define type of variable or data in general. As we did previously with; 

int sameVar; 

Here the “int” section is a data type. We define it as “integer” by putting “int”. 

There are of course different types of data are possible which are simply; 

char: This is used to store a single character like ‘t’, ‘b’, ‘L’. For the definition of char data type content, you can use ‘’. 

float: To store fractional numbers, up to 7 decimals. 

double: To store fractional numbers, up to 15 decimals. 

bool: To store logical values or Boolean values. This can store “true” or “false” (1 or 0). 

wchar_t: This means “wide character”. This is also like char but has greater size. 

void: This means “without a value”. Mostly used for functions where we do not want to return any value. 

string: String is like character but stores group of characters. To define string content, you need to use “”.

Here you can see the difference between using char and string; 

PROGRAM:

  1. #include <iostream>
  2. using namespace std; 
  3. int main(){
  4. string i = “hello world”; //this is how to define string
  5. char x = ‘h’; // this is how to define character
  6. cout<< i<<endl; 
  7. cout<<x<<endl;
  8. system(“pause”);
  9. }

 OUTPUT: 

Here you also need to be aware that multiple strings can be added to each other with + operator; 

  1. #include <iostream>
  2. using namespace std; 
  3. int main(){
  4. string s1 = “Hello “, s2=”World”, s3;
  5. s3 = s1 + s2; 
  6. cout<<s3<<endl;
  7. system(“pause”);
  8. }

 OUTPUT:

Data Type Modifiers (signed, unsigned, long, short)

Data types also can be defined by signed, unsigned, long or short. 

So you can define data types as; 

unsigned short int, long int, long double, short int etc. 

Signed here means including the negative numbers. If it is unsigned it is only positive numbers and you will have larger positive numbers. So you need to use unsigned when you are not using negative numbers. This is a better approach. On the other hand, you are free to use general int as a data type but as a programmer you need to think about being efficient while coding. Since a program with a less storage is better. 

Long and short are about having a larger or less number range. For example for a 32 bit system, with long int you can work between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. But with short int you can only work between -32,768 and 32,767. On the other hand, normal “int” data type will have the range in between these long and short which is between -2,147,483,648 and 2,147,483,647. So, you can learn these data type storages in deep and use one of them whenever you need. 

EXERCISE: Write a program to calculate a tax from price entered by user. The tax rate must also be defined by the user. 

PROGRAM

  1. /* 
  2. Program that calculates tax of any price entered
  3. version 0.0
  4. author ilker <info@biawd.com>
  5. */
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. int main(){
  10. int rate;
  11. float result, price;
  12. cout<<“Enter the rate for the tax”<<endl;
  13. cin>>rate;
  14. cout<<“Enter the price that tax will be calculated:”<<endl;
  15. cin>>price;
  16. result=price*rate/100;
  17. cout<<“The tax of the price entered is:”<<result<<endl;
  18.  

 OUTPUT

NOTE: You may buy this course in paperbook book format from https://www.amazon.com/dp/B0892HQTGS

TEA BREAK WITH NANOCAD: COMMAND LINE BAR AND DRAWING YOUR FIRST LINE

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/books/nanocad/

At the bottom side you will also see command line bar where you can see the commands you entered. Command is a way of interacting with nanoCAD. Instead of selecting the command on screen from bars, you can write command here. On top section of command line bar, you will see the history of commands and bottom of bar you see the command you are entering now.

Click “Ctrl+N” or “File>Open” to open a new nanoCAD drawing. You see now your bars are activated to get your commands and you now have a “+” shaped arrow on black side of the screen.

If you still have “inspector” bar on the left opened, you may close it for now, we will get into deep with it later. 

You see that on the left bottom side you have x, y and arrows. This is for axis system. x-y axis system is used throughout the drafting. This is to define how much where you need to draw a line etc. Software actually can’t understand you by words but it actually understands if you say “draw a line with 5mm to the x direction”. 

So, let’s do it.

Write “l”. You see now that the command line is waiting for you to specify the first point.

Here you can write 0,0 to say the program that you want to start to draw a line starting from x=0 and y=0.

Now you can see also that you can see the line is going through your +. You need to enter the next point as the command line asks you. Here you get it that you can also click to specify the second point, or you can write the point as we did here. While selecting freely you can see that on the bottom left side some numbers are changing; 

Here the first number is the length of the line and the second one after “<” is the angle from the axis. 

So, write to command line 5,0. And click “enter” on your keyboard. So, it has finished the drawing line. So, if you can’t see the line don’t worry it is under the axis system arrows. To see it you need to “zoom in”. To zoom in you can use the wheel on your mouse. 

As you zoom in you see the line under x-y axis is coming on screen. This is because as you zoom in the length of the x-y axis symbol does not change but the lines you draw will change. Anyway, you are getting used to with the environment fast. You can now want to see other sides of the screen. What you do with a real piece of paper? You can pan it right? Here at nanoCAD you can easily do it by using wheel button. By clicking on wheel, you see that your pointer on screen changed to hand. 

With this state you have ability to pan the drawing anywhere you want so that you can easily get a better view of your drawing. 

So, these two movements at the nanoCAD, are very much used pan and zoom in/out. You can use them as much as you can so that you can easily see the drawing you draw. 

If you double click the wheel button, nanoCAD automatically centre the drawing you drew. 

At NanoCAD there are more than one way generally. Other easy and fast way to draw a line is to click to the first point and move your pointer to the direction you want to draw the line, 

As you can see it will also show you the direction that it will draw the line. Then you can just enter the length from keyboard, so enter 5 and enter. So, you get the idea. 

NOTE: You may buy this course in paperbook book format from https://www.amazon.com/Learn-Drafting-NanoCAD-5-0-ECO/dp/B08Y4HB68H/ref=mp_s_a_1_3?dchild=1&keywords=nanocad&qid=1633261854&sr=8-3

TEA BREAK WITH C++: VARIABLES

Since we learned basic input function at programming with C++, we need to talk a little more about the variables. 

Variables are simply naming some data location in your pc. With variables you can store a value. 

Variables can be global, local or block based. Actually, you can use the same variable name in these locations at the same time. 

  1. #include <iostream>
  2. using namespace std;
  3. int sameVar=1; //defining a global variable at header location
  4. int main(){
  5.     int sameVar=2;    //defining a local variable inside int main
  6.     cout<<“local variable:”<<sameVar; //printing a local variable in int main 
  7.     {
  8.     int sameVar=3; //here we define a variable in any block. 
  9.     cout<<“block variable:”<<sameVar; //printing the block variable
  10. }
  11. //reaching a global variable inside program
  12. cout<<“global variable:”<<::sameVar; //we print global variable in a program
  13. }

 Output;

Here the most important thing we need to talk is the 

variable = value logic. 

Always variable is on the left and the value is on the right of the equivalent sign. 

TEA BREAK WITH C++: Basic Input & endl

On line 9 in the previous example we give 

    int firstNumber, secondNumber, sum;

We define integer variables. These are first number with the definition firstNumber, and the second number defined as secondNumber. And the sum. These we defined as integer by writing “int” at the beginning.  

Continue reading TEA BREAK WITH C++: Basic Input & endl

TEA BREAK WITH C++ : Basic Comment Section for All programs

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Here these lines are comment section giving information about the program, and you need to include these lines so that another programmer can understand what it is about easily. 

Continue reading TEA BREAK WITH C++ : Basic Comment Section for All programs

TEA BREAK WITH C++: Basic Comment Section for All programs

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Here these lines are comment section giving information about the program, and you need to include these lines so that another programmer can understand what it is about easily. 

Continue reading TEA BREAK WITH C++: Basic Comment Section for All programs

TEA BREAK WITH C++: BASIC INPUT / OUTPUT

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

As we have seen in the previous section, library we are using for basic input / output operations is the iostream library. That is why we need to include that at the beginning. 

Continue reading TEA BREAK WITH C++: BASIC INPUT / OUTPUT

TEA BREAK WITH C++: MY FIRST C++ PROGRAM

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

  1. //my first c++ program
  2. #include <iostream>
  3. using namespace std; 
  4. int main(){
  5. cout<<“hello world!”;
  6. return 0;
  7. system(“pause”);
  8. }
  9. /* This ends 
  10. the file */
Continue reading TEA BREAK WITH C++: MY FIRST C++ PROGRAM

TEA BREAK WITH C++: Paragraph Comments (Multiple Line Comment)

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Another type of comment is paragraph comment (or multiple line comment) where you start commenting with “*/” and end with “/*”

Continue reading TEA BREAK WITH C++: Paragraph Comments (Multiple Line Comment)

TEA BREAK WITH C++: Line Comments

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

The “//” sign means you are adding a comment. This is called line comment. The things you write after these “//” will not be taken as a command (means these things will not be compiled). It is always a good approach to write down the properties of the code at the beginning like author, for which aim this code is written etc. 

Continue reading TEA BREAK WITH C++: Line Comments