Tuesday, 30 June 2015
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
Saturday, 27 June 2015
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 length( )
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
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).
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 n
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
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.
#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;
}
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
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
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
- #include <iostream>
- using namespace std;
- int factorial(int);
- int main() {
- int n;
- cout<<"Enter a number: ";
- cin>>n;
- cout<<"Factorial="<<factorial(n);
- return 0;
- }
- int factorial(int n) {
- if (n>1) {
- return n*factorial(n-1);
- }
- else {
- return 1;
- }
- }
Output
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 statementreturn num*factorial(num-1);
is executed, which calls secondfactorial()
function and argument passed isnum-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 thirdfactorial()
function and argument passed isnum-1
which is 2. - In third
factorial()
function, test expression inside if statement is true. The statementreturn num*factorial(num-1);
is executed, which calls fourthfactorial()
function and argument passed isnum-1
which is 1. - The fourth
factorial()
function, test expression inside if statement is false. The statementreturn 1;
is executed, which returns 1 to thirdfactorial()
function. - The thrid
factorial()
function returns 2 to secondfactorial()
function. - The second
factorial()
function returns 6 to firstfactorial()
function. - Finally, first
factorial()
function returns 24 to themain()
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:
- If you are at home, stop moving.
- Take one step toward home.
- "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.
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
- #include<iostream>
- #include<conio.h>
- #include<string>
- using namespace std;
- struct movies
- {
- string movie;
- };
- main()
- {
- movies m[3];
- for(int i=0;i<=2;i++)
- {
- cout<<"Enter the movie of your choice:";
- getline(cin,m[i].movie);
- }
- cout<<endl;
- cout<<"\t"<<"\t"<<"List of your favourite movies"<<"\t"<<"\t"<<endl<<endl;
- for(int i=0;i<=2;i++)
- {
- cout<<i+1<<"."<<m[i].movie<<endl<<endl;
- }
- }
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.
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
- #include<iostream>
- #include<conio.h>
- #include<string>
- using namespace std;
- struct patient
- {
- string name;
- string disease;
- };
- void getrecord(patient &p1 ,patient &p2)
- {
- cout<<"Enter the name of first patient:";
- cin>>p1.name;
- cout<<"Enter the name of disease:";
- cin>>p1.disease;
- cout<<"Enter the name of second patient:";
- cin>>p2.name;
- cout<<"Enter the name of disease:";
- cin>>p2.disease;
- }
- void displayrecord(patient p1, patient p2)
- {
- cout<<"Name of first patient:"<<p1.name<<endl;
- cout<<"Disease of first patient:"<<p1.disease<<endl;
- cout<<"Name of second patient:";
- cout<<p2.name<<endl;
- cout<<"Disease of second patient:";
- cout<<p2.disease<<endl;
- }
- int main()
- {
- patient p1,p2;
- char ch;
- int n;
- do
- {
- cout<<"Enter 1 for getting record and 2 for displaying record:";
- cin>>n;
- switch(n)
- {
- case 1:
- getrecord(p1,p2);
- break;
- case 2:
- displayrecord(p1,p2);
- break;
- default:
- cout<<"Invalid choice!";
- }
- cout<<"Enter y to continue and n to terminate:";
- cin>>ch;
- }
- while(ch!=n);
- getch();
- return 0;
- }
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".
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
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++.
Subscribe to:
Posts (Atom)