-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Tuesday, 30 June 2015

The password of a programmer

And remember that the password of a programmmer is toughest !!!

Programmer vs other people
Programmer password

Monday, 29 June 2015

Reversal of string


Lets play a game !!!!

Write your name in reverse order.

Ok wait,let me write my name first.

Khawaja

Reversed: ajawahK

Isn't it interesting?

So,today I am here with an interesting program that will input a string from the user and then display its reverse.

Have a look at its source code.

Source code

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
main()
{
char abc[20];
cout<<"Enter any string:";
gets(abc);
int len=strlen(abc); //used to find the string length
for(int i=len-1;i>=0;i--) //reverse loop
cout<<abc[i];
return 0;
getch();
}


Output

Reversal of string
Reversal of string

Saturday, 27 June 2015

Evaluation of engineering student


Have a look at the life of an engineering student !!!

Evoluation of engineering student
Evaluation of engineering student



Friday, 26 June 2015

String characteristics

In my previous blog, I have given you an introduction to strings.Today I will discuss on some string characteristics or you can say,"string operations".

String size( )

If you want to find the length of any string, then its syntax is as follows:
 str.length( );

String length( )

If you want to find the length of any string, then its syntax is as follows:
 str.length( );

String capacity( )

If you want to find the capacity of any string, then its syntax is as follows:

str.capacity( );

Maximum size( )

If you want to size the maximum size of any string, then its syntax is as follows:

str.max_size( );

String empty or not

If you want to check whether the string is empty or not,then its syntax is as follows:

(str.empty?"yes":"no") ; (it is same as conditional operator)

So, to implementation of these operations is as follows:

Source code

#include<iostream>
#include<string.h>
using namespace std;
main()
{
string s1;
cout<<"Enter any string:";
getline(cin,s1); // syntax for getting the input of string
cout<<"Size="<<s1.size()<<endl<<endl;
cout<<"Length="<<s1.length()<<endl<<endl;
cout<<"Capacity="<<s1.capacity()<<endl<<endl;
cout<<"Maximum size="<<s1.max_size()<<endl<<endl;
cout<<"Empty="<<(s1.empty()?"yes":"no")<<endl<<endl;
}

Output

C++,khgamujtaba,string characteristics

Monday, 22 June 2015

Concept of string

Today I am here with the concept of string in C++.

Remember that the header file for string is <string.h> in C++(may vary in different compilers).

String

A string is used to represent a sequence of characters regarded as a single data item. In C++ strings of characters are held as an array of characters, one character held in each array element. In addition a special null character, represented by `\0', is appended to the end of the string to indicate the end of the string. Hence if a string has characters then it requires an n+1 element array (at least) to store it. Thus the character `a' is stored in a single byte, whereas the single-character string "a" is stored in two consecutive bytes holding the character `a' and the null character.

Remember that the difference between a string data type variable and character data type variable is that a variable with character data type can store single character and it does not include space or the characters after space but a varible with string data type  can store multiple characters and it includes space or the characters after space 

In the below example, the string variable has data type "string".

Source code

#include<iostream>
#include<string.h>
using namespace std;
main()
{
string s1;
s1="Hello welcome to my blog";
cout<<s1;
}

Output

C++,khgamujtaba,strings in C++

Remember that the characters written a string are enclosed in curly braces.

Thursday, 18 June 2015

Fibonacci series using recursion

Hello and Asalam o Alikum.I hope you have read my previous blogs about recursion in C++.So, today is my last blog about recursion.

Today I will provide you the source code of generating fabonacci series using recursion.Remember that when you planned to find fibanacci series in C++, simply use this method.This method is easier to understand than other methods.

Below is the source code of this program.

Source code


#include <iostream>
using namespace std;

int fibonacci(int num)
{
if(num==0)
return 0;
if(num==1){
return 1;
}
else{
return fibonacci(num-1)+fibonacci(num-2);
}
}

int main()
{
int n;
cout<<"Enter the number of items you want to display: ";
cin>>n;
for(int i=1;i<=n;i++){
cout<<fibonacci(i)<<"\t";
}
return 0;
}

Output

C++,fibonacci series,khgamujtaba,recursion,fibanacci series using recursion

Remember that the basic idea behind solving problems via recursion is to break the instance of the problem into smaller and smaller pieces until the pieces are so small they can be solved trivially. 

Thursday, 11 June 2015

Finding factorial by recursion

As I have discussed in my previous blog that a function that repeats itself is called recursion.So, today I am here with  a very common example of recursion in C++. In this example, I will find the factorial of a given number by recursion.Below is the source code of this program.

Source code

  1. #include <iostream>
  2. using namespace std;

  3. int factorial(int);

  4. int main() {
  5.     int n;
  6.     cout<<"Enter a number: ";
  7.     cin>>n;
  8.     cout<<"Factorial="<<factorial(n);
  9.     return 0;
  10. }

  11. int factorial(int n) {
  12.     if (n>1) {
  13.         return n*factorial(n-1);
  14.     }
  15.     else {
  16.         return 1;
  17.     }
  18. }

Output

C++,programming,recursion,khgamujtaba,factorial

Explanation
Suppose user enters 4 which is passed to function factorial(). Here are the steps involved:
  • In first factorial() function, test expression inside if statement is true. The statement return num*factorial(num-1); is executed, which calls second factorial() function and argument passed is num-1 which is 3.
  • In second factorial() function, test expression inside if statement is true. The statementreturn num*factorial(num-1); is executed, which calls third factorial() function and argument passed is num-1 which is 2.
  • In third factorial() function, test expression inside if statement is true. The statement return num*factorial(num-1); is executed, which calls fourth factorial() function and argument passed is num-1 which is 1.
  • The fourth factorial() function, test expression inside if statement is false. The statementreturn 1; is executed, which returns 1 to third factorial() function.
  • The thrid factorial() function returns 2 to second factorial() function.
  • The second factorial() function returns 6 to first factorial() function.
  • Finally, first factorial() function returns 24 to the main() function and is displayed.        ( The above reference has been taken from www.programiz.com)

Tuesday, 9 June 2015

Recursion in C++

Hello and Asalam o Alikum. Many students are confused with the concept of recursion in C++.So,today I will try to discuss all the basics of recursion in C++.

Recursion

A recursive function is a function that calls itself,either directly or indirectly(through another function).A recursive function is called to solve a problem. If the function is called with a base case, then function simply returns a result. If a function is called with a more complex problem, it typically divides the problem into two conceptual pieces,a piece that the function knows how to do and the piece that the function doen not know.

To make recursion feasible, the latter piece must resemble the original problem, but be a slighty smaller version. This new problem looks like the original problem, so the function launches a fresh copy of itself to work on the smaller problem. This is referred to as recursive call and is also called the recursive step.

The basic idea behind solving problems via recursion is to break the instance of the problem into smaller and smaller pieces until the pieces are so small they can be solved trivially. 

General example

For example, we can define the operation "find your way home" as:
  1. If you are at home, stop moving.
  2. Take one step toward home.
  3. "find your way home".
Here the solution to finding your way home is two steps (three steps). First, we don't go home if we are already home. Secondly, we do a very simple action that makes our situation simpler to solve. Finally, we redo the entire algorithm.(reference of general example has been taken from reference)

Sunday, 7 June 2015

Using loops in structures


Hello and Asalam o Alikum. Today I am here with a new blog that will take demonstate the use of loops in structures.

In the program,I will take input from the user of his/her favourite movies and finally display the list of his/her favourite movies using structures in C++.I will use a loop to input 3 movies from the user.

So,below is the source code of this program.

Source code

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4. using namespace std;
  5. struct movies
  6. {
  7. string movie;
  8. };
  9. main()
  10. {
  11. movies m[3];
  12. for(int i=0;i<=2;i++)
  13. {
  14. cout<<"Enter the movie of your choice:";
  15. getline(cin,m[i].movie);
  16. }
  17. cout<<endl;
  18. cout<<"\t"<<"\t"<<"List of your favourite movies"<<"\t"<<"\t"<<endl<<endl;
  19. for(int i=0;i<=2;i++)
  20. {
  21. cout<<i+1<<"."<<m[i].movie<<endl<<endl;
  22. }
  23. }

Output


Explanation
I have used <string> header file in line # 3 because I have "string" data type in line # 7.

In line # 11, I have declared an array of size 3 and data type movies.

In line #12, a "for" loop is used for 3 iterations.

Remember that if we do not use the header file <string>, then we cannot use getline( ) function as used in line # 16.

Friday, 5 June 2015

Maintaining records in structures

As discussed earlier, structures are of great important for user-defined data types.Today I will discuss the use of functions in structures in C++ programming.

Below is the source code of a program in which I will maintain the record of two patients using structure in C++.I will use two functions in it;one for getting the record of the patients and one for displaying the record of the patients.So,before going into the further details of the program,lets discuss its source code first.

Source code

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4. using namespace std;

  5. struct patient
  6. {
  7. string name;
  8. string disease;
  9. };

  10. void getrecord(patient &p1 ,patient &p2)
  11. {
  12. cout<<"Enter the name of first patient:";
  13. cin>>p1.name;
  14. cout<<"Enter the name of disease:";
  15. cin>>p1.disease;
  16. cout<<"Enter the name of second patient:";
  17. cin>>p2.name;
  18. cout<<"Enter the name of disease:";
  19. cin>>p2.disease;
  20. }

  21. void displayrecord(patient p1, patient p2)
  22. {
  23. cout<<"Name of first patient:"<<p1.name<<endl;
  24. cout<<"Disease of first patient:"<<p1.disease<<endl;
  25. cout<<"Name of second patient:";
  26. cout<<p2.name<<endl;
  27. cout<<"Disease of second patient:";
  28. cout<<p2.disease<<endl;
  29. }
  30. int main()
  31. {
  32. patient p1,p2;
  33. char ch;
  34. int n;
  35. do 
  36. {
  37. cout<<"Enter 1 for getting record and 2 for displaying record:";
  38. cin>>n;
  39. switch(n)
  40. {
  41. case 1:
  42. getrecord(p1,p2);
  43. break;
  44. case 2:
  45. displayrecord(p1,p2);
  46. break;
  47. default:
  48. cout<<"Invalid choice!";
  49. }
  50. cout<<"Enter y to continue and n to terminate:";
  51. cin>>ch;
  52. }
  53. while(ch!=n);
  54. getch();
  55. return 0;
  56. }

Output


Explanation:

In this program,I have declared a structure named "struct".

In line # 13, I have declared a function for getting the record from the user in which I have passed the values by reference.

In line # 24, I have declared a function for displaying the data entered by the user.

In line # 42, I have declared a "switch" in which there are two cases;one case for getting the record from the user and one case for displaying the record from the user.

Monday, 1 June 2015

Simple example of structures

Feeling difficulty in understanding the concept of structures in C++?
Simply e mail me at "G.A.Mujtaba11@gmail.com".

Today I am here with a simple example of structures in C++.Before explaining the example,let me tell you a very important concept about structures,"member access operator/dot operator".

Dot operator/Member access operator

If you want to access a variable in structures,then you should use "dot operator".
For example,
struct structure
{
int data
};
As you see in the above example that a structure is declared that contains a variable named as "data".
Suppose if you want to access that variable in main( ) function,then you should use dot operator as given below

structure s1;  //////s1 has data type structure
s1.data;   ///////this "." is pointing to the variable being declared in the structure.

Hope you understand this concept.Now below is the simple example of structures in C++.

Source code

structures example in C++

Output



Remember that the data type of employ is "database" in the above example.I am not taking any input from the user in the above program.In my next blog, I will inshallah (if Allah Almighty wants) concentrate on little complex program in which I will take inputs from the user.

I will recommend you to practice this simple code which will help you to better you concepts of structures in C++.