Friday, December 20, 2019

Playing With C++


Disclaimer:
                   Ths blog for educational purposes only.


Tools needed:
1.    PC running Windows Operating System (OS)
2.    Connection to the Internet.

Turn on the PC and log into it using your student account information.

Important Tip # 1:
Instead of using USB drive to copy your work done in the lab, log into your nu.edu.pk email id and copy the data to the associated GoogleDrive. The reason is that since USB drives get plugged into lots of systems, their probability of carrying a virus increases. It wouldn't only risk the lab computer, but in case the lab system contains viruses, your USB drive will carry it and infect your home computer/laptop too!
Save your work by pressing Ctrl+S on the PC and enable auto-sync feature to syncit to your GoogleDrive automatically. This way, if a power failure occurs, your work would be saved. Even if the PC's HardDisk crashes irretrievably, your work would still be safe.

Writing a meaningful DevC++ Code:

Run DevC++ IDE now. Open a new code file and save it as lab02_1.cpp.

1.    Write the following code verbatim into it and save it.

#include <iostream>
using namespace std;

int main()
{
int a,b,sum;
    cout<<"Enter the 1st number: ";
    cin>>a;
    cout << "Enter the 2nd number: ";
    cin>>b;
    sum=a+b;
    cout<< endl << "Sum of two numbers:"<< sum << endl;
    return 0;
}
Code: lab02_1.cpp

Task # 1:

Run the code lab02_1.cpp with several different inputs and explain what it does.
Inputs                                                             Outputs

1 , 3           1+3=                                                               4
2 , 5           2+5=                                                               7
5 , 7           5+7=                                                             12
8 , 8           8+8=                                                             16
3 , 3           3+3=                                                               9
The program takes input of two numbers from user then adds the two numbers and displays the Result







Task # 2:

Change the code lab02_1.cpp so that it also includes the functionality of subtraction, multiplication and division. The code should display the results as well.
#include <iostream>
using namespace std;

int main()
{
int a,b,sum,div,mul,sub;
    cout<<"Enter the 1st number: ";
    cin>>a;
    cout << "Enter the 2nd number: ";
    cin>>b;
    sum=a+b;
    div=a/b;
    mul=a*b;
    sub=a-b;
    cout<< endl << "Sum of two numbers:"<< sum << endl;
    cout<< endl << "Multipication of two numbers:"<< mul<< endl;
    cout<< endl << "Division of two numbers:"<< div << endl;
    cout<< endl << "Subtraction of two numbers:"<< sub << endl;
    return 0;
}



1.    Write the following code in DevC++ and try to find the logic of the code.

#include <iostream>
using namespace std;
int main()
{
     double length, width, answer;
     cout << "Enter length and width of a rectangle\n";
     cin >> length >> width;
     answer = length * width;
     cout << "Area of rectangle is   " << answer << endl;
     return 0; 
}
Code: lab02_2.cpp





Task # 3:

By making a small change in this program, you can calculate perimeter. Try it. Mention the changes you perform to obtain desired result
Code for Area:

answer = length * width;



Change:


answer = length * width;  to answer = 2*(length+width);
cout << "Area of rectangle is   " << answer << endl; to cout << "perimeter of rectangle is   " << answer << endl;


Code for Perimeter:


answer = 2*(length+width);







1.    Write the following code to find out what does this code do.

#include <iostream>
using namespace std;
int main()
{
char test_alphabet;
cout << "Enter an alphabet to check if it is a vowel: ";                    
cin >> test_alphabet;     // Taking Input character;

  if(test_alphabet == ’a’ || test_alphabet==’e’ || test_alphabet ==’i’ || test_alphabet==’o’|| test_alphabet== ‘u’)
   {
cout<<”alphabet entered is a vowel”<<endl;
   }
else
{
   cout << "alphabet is not a vowel”<<endl;
   return 0;
}
}
Code: lab02_3.cpp







Task # 4:

Test above code with different inputs. Enter a vowel e.g “ i ” and a consonant e.g “b” to test the code.
INPUT                                                                                    OUTPUT
i                                                                                              alphabet entered is a vowel
j                                                                                              alphabet  is not a vowel
e                                                                                             alphabet entered is a vowel





Task # 5:

Now try a capital vowel letter “A” and give the output? Try other capital vowel letters too.
INPUT                                                                                    OUTPUT

A                                                                                            alphabet  is not a vowel
E                                                                                            alphabet  is not a vowel
I                                                                                              alphabet  is not a vowel
O                                                                                            alphabet  is not a vowel
U                                                                                            alphabet  is not a vowel








Task # 6:
Can you try to change/modify the code so that it works for capital letters ?
Change in code:
if(test_alphabet == 'a' || test_alphabet=='e' || test_alphabet =='i' || test_alphabet=='o'|| test_alphabet== 'u')                    To                      if(test_alphabet == 'A' || test_alphabet=='E' || test_alphabet =='I' || test_alphabet=='O'|| test_alphabet== 'U')





INPUT :                                                                                   OUTPUT

A                                                                                             alphabet entered is a vowel
E                                                                                             alphabet entered is a vowel
I                                                                                              alphabet entered is a vowel








1.    Given the below template run it in IDE and try to understand what it does.

#include <iostream>
using namespace std;
int main()
{
   int i=1;
   /* The loop would continue to print
    * the value of i until the given condition
    * i<=15 is false.
    */
   while(i<=15)
   {
      cout<<"Value of variable i is: "<<i<<endl;
       i=i+1;
   }
}
Code: lab02_4.cpp



Task # 7:

Explain what does the above code does.
Explanation:

The code is for an incremental program in which the value of variable   i    keeps on changing until it reaches specified value of 15   after it reaches the specified value program stops.






Task # 8:

Change the condition in while loop so that the variable is printed until it reaches a value of 100.
Change :
int i=1;
   /* The loop would continue to print
    * the value of i until the given condition
    * i<=15 is false.
    */
while(i<=15)
To
int i=1;
   /* The loop would continue to print
    * the value of i until the given condition
    * i<=100 is false.
    */
while(i<=100)







  
1.    Write the following code to find out what does this code does.

#include <iostream>
using namespace std;
int main()
{
int Number, Reverse = 0;
cout << "Input a Number to Reverse and press Enter: ";                    
cin >> Number;     // Taking Input Number in variable number.

   while( Number!= 0 )        //while loop for repetition.
   {
      Reverse = Reverse * 10;
      Reverse = Reverse + Number%10;
     Number = Number/10;
   }
   cout << "New Reversed Number is:  "<< Reverse << endl;
   return 0;

}
Code: lab02_5.cpp



Task # 9:
Test above code. Explain what does this code do. Are there any limitations of this code. If ‘Yes’, mention them.

The above code is a program which reverses the digits of a number by using  the formula
Reverse=Reverse*10;
Reverse=Reverse+Number%10;
Number = Number/10;

Yes’s there is limitation as for instance  if we enter 123456789 program returns 987654321
But if we increase digits from 9 to 10 and beyond  e.g    12345678910   returns -1126087180
So program has a limit of 9 digits that it can reverse.


















Functions & BASIC FILE I/O

A.    Files : For storage of data for later use, files are used. Files are stored on the Hard Disk Drive (HDD). There are two types of f...