Wednesday, May 24, 2017

C++ Jumpstart: Dealing with simple C++ programs to understand how it works.

[Programming in C++ ; Image source: edX.org ]

This post is a continuation, intended for readers who've read this previous post.We'll  jump into C++ programming with some simple example code. We'll be comparing and contrasting the way in which each of these programs follow a basic pattern. This will bs useful for writing programs during examinations, and in general.

WAP to enter any two numbers and print their product
  #include <iostream.h>
    #inlcude <conio.h>
     
    void main(){
     clrscr();
     int number1, number2, prod;
     // to print product of two numbers
     cout<<"Enter any two numbers"<<endl;
     cin>>number1;
     cin>>number2;
     prod = number1 * number2;
     cout<<"The product is:"<<prod;
     getch();
    }
 
  • If you notice carefully, you'll find that this code is awfully similar to the previous one about finding the sum of two numbers. All of the points that we discussed in the previous post, thereby, also applies to this code. Please keep that in mind as you continue reading. Everything is connected.

    Lets contrast by example: WAP to enter any two numbers and print their sum
      #include <iostream.h>
      #inlcude <conio.h>
       
      void main(){
       clrscr();
       int number1, number2, sum;
       // to print sum of two numbers
       cout<<"Enter any two numbers"<<endl;
       cin>>number1;
       cin>>number2;
       sum = number1 + number2;
       cout<<"The product is:"<<sum;
       getch();
      }
      
    If you notice carefully, you'll observe that only the operator in line-11 has changed from (+) to (*). We are ignoring the changes in variable name, strings and comments because their change does not affect the output at all.



Let's take a look at yet another program
WAP to enter any two numbers and print the third angle
  #include <iostream.h>
    #inlcude <conio.h>
     
    void main(){
     clrscr();
     int angle1, angle2, angle3;
     // to print third angle
     cout<<"Enter any two angles"<<endl;
     cin>>angle1;
     cin>>angle2;
     angle3 = 180 - (angle1 + angle2);
     cout<<"The third angle is:"<<angle3;
     getch();
    }
 
  • Again, it is apparent that only line-11 has truly changed. (Disregarding the change to variable name and comments.).
      #include <iostream.h>
      #inlcude <conio.h>
       
      void main(){
       clrscr();
       int number1, number2, sum;
       // to print sum of two numbers
       cout<<"Enter any two numbers"<<endl;
       cin>>number1;
       cin>>number2;
       sum = number1 + number2;
       cout<<"The product is:"<<sum;
       getch();
      }
      
    In this case, you'll observe that only the arithmetic operation in line-11 has changed from c=a+b to c=180-(a+b). All of the programs so far, have been VERY similar, actually. If you understand the first, you'll remember all of these extremely easily.



Let's move to another example:
WAP to enter any two numbers and print the area and perimeter
  #include <iostream.h>
    #inlcude <conio.h>
     
    void main(){
     clrscr();
     int length, breadth, area, perimeter;
     // to print area and perimeter
     cout<<"Enter length and breadth"<<endl;
     cin>>length;
     cin>>breadth;
     area = length * breadth;
     perimeter = 2 * (length + breadth);
     cout<<"Area is:"<<area;
     cout<<"Perimeter is:"<<perimeter;
     getch();
    }
 
  • Here, we can see that, for this program, 4 variable declarations are needed, so line-6 had to change. Similarly, line-11 is more or less the same. Only the arithmetic operator has changed from (+) to (*). Not that drastic of a change. line-12 is another variable initiation; which was not present in our previous code. Similarly, to show the output, line-14 is new(but is similar in every way to line-13; which has been used earlier).

    In conclusion, not much has changed in terms of code, so far.
      #include <iostream.h>
      #inlcude <conio.h>
       
      void main(){
       clrscr();
       int number1, number2, sum;
       // to print sum of two numbers
       cout<<"Enter any two numbers"<<endl;
       cin>>number1;
       cin>>number2;
       sum = number1 + number2;
       cout<<"The product is:"<<sum;
       getch();
      }
      



Next Example:
WAP to calculate Simple Interest
  #include <iostream.h>
    #inlcude <conio.h>
     
    void main(){
     clrscr();
     int principal, rate, timex, si;
     // to find Simple Interest
     cout<<"Enter Principal, Rate, Time"<<endl;
     cin>>principal;
     cin>>rate;
     cin>>timex;
     si = (principal * rate * timex)/100;
     cout<<"Simple Interest is:"<<si;
     getch();
    }
 
Note: You'll notice that I'm using the variable timex instead of time in the code above. I've done this intentionally because time cannot be used as a variable name, as it is a keyword. We've discussed about keywords in our earlier tutorial. Reference it if you are confused.

It is a good practice to use variable names that are easy to understand. Instead of just p, t, r, it is a better practice to use principal, timex, rate. The end goal of a variable name is just to help identify what it represents. Just be sure that you are not accidentally using keywords. If you're unsure, just add an "x" at the end of the name. ie. if you doubt rate is a keyword while writing an exam, it doesn't hurt to use ratex instead.
  • Here, we can see that, for this program, 4 variable declarations are needed, similar to the recent example. Line-11 is more or less the same. The arithmetic operation is basically the same type of thing in line-12. The only major difference is that here, we can see 3 cin statements. That means 3 inputs, and one calculation(by variable initiation) takes place in this program.

    Not much has changed in terms of code, so far. The basic structure is still the same.
      #include <iostream.h>
      #inlcude <conio.h>
       
      void main(){
       clrscr();
       int number1, number2, sum;
       // to print sum of two numbers
       cout<<"Enter any two numbers"<<endl;
       cin>>number1;
       cin>>number2;
       sum = number1 + number2;
       cout<<"The product is:"<<sum;
       getch();
      }
      
With this newfound information, I urge you to please go through >>>this tutorial once again<<<. You'll understand everything much more clearly. And as always, comment or message me if you have any queries.

0 comments

Post a Comment