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

Saturday 30 May 2015

Concept of structures

Structures


There are many data types in C++.i.e.,int,float,double,long double,bool,etc.

We know that int is used to represent integers and float is used to represent numbers with precision.

But in case of real world objects(such as car,chair,door,etc),these data types are not useful.

So,to remove this difficulty, we use user-defined data types and these data types are known as structures.

Definition:

                                       A structure is a collection of variable types grouped together.You can refer to a structure as a single variable, and to its parts as members of that variable by using the dot (.) operator. 

Remember that the variables in a structure are of different data types.

When dealing with the students in a college, many variables of different types are needed.  It may be necessary to keep track of a name, an address (street, city, state, zip code), an age, an ID number, and a grade point average, for example,below is the general syntax of declaring a structure or user-defined data type in C++.


struct STUDENT_TYPE

{

string name;

string street; 

string city;

string state;

string  zipcode;  

 int age; 

 double IDnum;    

 double grade;

} ;


(reference taken from www.MathBits.com)


Saturday 23 May 2015

Finding Armstrong number in C++

Today I am here with a new program that will input a number from the user and determine whether it's Armstrong number or not.

Armstrong number 

  1. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Write a program to find all Armstrong number in the range of 0 and 999(reference taken from http://www.cs.mtu.edu)
  2. So, below is the source code of this program.

  1. Source code



Armstrong number in C++

Output


Explanation:

In this program,we will input a number from the user.

Line # 11:

                 At this line, there is a loop that explains that if the user enters "0", this loop would be terminated.

Line # 13:

                  It will take the modulus(remainder) of the number being entered by the user.

Line # 14:

                Now it will divide the given number by 10.

Line # 18:

                If the given result would be equal to check(or given number),then it will display that it is an Armstrong number other it will display that it is not an Armstrong number.

If you have still any ambiguity in understanding the logic of this program, then feel free to contact me at "G.A.Mujtaba11@gmail.com".


Friday 15 May 2015

Calculating factorial using functions

In my previous blogs, I have calculated factorial of a given number using while,do-while and for loop.Today I will calculate factorial using function.


Below is the source code of this program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void factorial(int n);
main()
{
int n;
cout<<"Enter any number:";
cin>>n;
factorial(n);//function call

getch();

}
void factorial(int n)
{

int c,f=1;
for( c=1;c<=n;c++)
{
f=f*c;
}
cout<<"Factorial="<<f;

}


Output


Explanation:

Line #4:
             It is function declaration with a parameter and no argument.

Line #10:
                It is the function call with a parameter.

Line #13:
                It is the function definition which tells what a function actually does.It is also without argument and with a parameter.

Logic of factorial

.For your revision,I will explain what a factorial actually is.

The factorial of 4 can be calculated as

4!=4*3*2*1=24

In the same way,factorial of 5 can be calculated as

5!=5*4
   =20
   =20*3
   =60
   =60*2
   =120
   =120*1
   =120



Thursday 14 May 2015

Simple example of function

Hello and Asalam o Alikum.

I have shifted to dev C++ compiler.So, from now onward, the programs of my blog would be written in dev c++ compiler.

Today I am here with the simplest example of function. It is about printing a simple statement in C++ using function.

One concept which is important and I want to mention here is "parameters".

Parameters

Parameters are the values that are provided to a function when the function is called.If there is no parameter, only parenthesis are used.

Below is the source code of that simple example of function which I mentioned above.

Source code


simple example of function


Output

Output

Explanation:
 

Line # 4 : 

             This is actually a function declaration and the empty parameter shows that it has no argument.

Line # 7:

              It is a function call.

Line # 10:

                  it is a function definition that tells what a function actually does.

Wednesday 13 May 2015

Local and global variables

Today I am here with some more concepts related to functions.

Function call

When you want to call a person, you normally call him by his name. In the same way, when we want to call or use a function in a program, we call it by its name.

Actually function call is a statement that activates a function.Whenever we call a function by its name, the control moves to the function that is called,the statements in the function body are executed and then control returns back to the calling function.

Local variable 

A variable declared inside a function is known as local variable. The scope of local variable is just inside a function. It means it cannot be used outside that function.Local variable is also known as automatic variable.Remember that the lifetime of a local variable is limited.

Global variable

A variable declared outside a function is known as global variable. The scope of global variable is inside the function as well as outside the function. It means it can be used everywhere in the program.Remember that global variables are declared before main function.The lifetime of a global variable is not limited.

FAQ's

Q. What is the difference between local variable and global varible in C++?

A. Local variables:

                                   Local variables in main are only visible within main.These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack.

Global variables:

                    A global variable may be accessed anywhere.It has a scope of the whole program, it is accessable from any function/module.That is, a global variable is available for use throughout your entire program after its declaration.

Q. What is the difference between "call by value" and "call by reference" in C++?

A. The basic difference is that when you pass a parameter by value, the function receives only a copy of the original object, so it can't do anything to affect the original object. With pass by reference, it gets a reference to the original object, so it has access to the original object, not a copy of it -- unless it's a const reference, it can modify the original object.(reference taken from stackoverflow.com)

Also,in pass by reference, we use "&" symbol with the variable while in pass by value,we do not use this symbol.

Sunday 10 May 2015

Concepts related to a function

The process of writing a function in C++ is very simple. Actually it consists of the following parts :


  • Function header
  • Function body

Syntax

The syntax of writing a function is as follows:

return-type function-name(parameters)
{
statement(s);
}

Function header

The first line of a function definition is known as a function header(function definition will be discussed later in this blog).

Its syntax is as follows:

Syntax

return-type function-name(parameters)

Function Body

The set of statement(s) written inside curly braces are known as function body. These are actually the statement(s) that are executed inside the function.

Function declaration

Function declaration is also known as function signature or function prototype.Remember that it ends with a semi colon. It tells the compiler about the structure of the function to be used in the program. It is written before the main( ) body.Its syntax is as follows :

Syntax

return-type function-name(parameters)

Example

void display( )

Function definition

A set of statements that actually explains what a function does is called a function. Remember that function definition can be written at the following places:

  • Before main( ) function
  • After main( ) function
  • In a separate file

Syntax


return-type function-name(parameters)
{
statement(s);
}

Example

void display( )
{
cout<<"Hello world";
}

Thursday 7 May 2015

Functions

Functions

What are functions?


You are familiar with this word in Mathematics already, but what is its purpose in C++?

Today I will explain functions in simple words. In general,a function is a block of code that performs the same action again and again.

Example from daily life


Consider that you went to a library in which there are thousands of books arranged in different shelves. Suppose you want to read to particular book. You take that book out of the shelf and read it for some time. After reading it, you put that book again in that shelf.

Now, remember this situation for the concept of "functions". Suppose you made a function for calculating factorial of a given number. You are writing a program of  200 lines. At the middle of this program, you want to calculate the factorial of a given number. So, you will simply call that function. No need to write the complete code for calculating factorial. You can use the same function again and again.

This is very much similar to the above situation. Whenever we want to read a particular book, we take it out of the shelf. In the same way, whenever we want to use a function, we call it by its name.

Advantages of functions:


There are many advantages of using a function:

* A function is easier to code.

* It is easier to modify.

* It is easy to debug.

* We can save programming time.

* Program size becomes less.


Tuesday 5 May 2015

Making a multiplication table

Hello and Asalam o Alikum. I have already discussed the concept of  "nested loops" in my blogs.

As you know that a loop within another loop is known as nested loop. The outer loop is normally used for rows and the inner loop is used for columns.

So,today I am here with a very interesting program. This program is about making a multiplication table using nested loops.

Below is the source code of this program.

Source code


Output

Program's logic

1*1     1*2    1*3    1*4    1*5    1*6   1*7     1*8   1*9     1*10

2*1      2*2    2*3     2*4    2*5    2*6    2*7    2*8    2*9  2*10

3*1    3*2      3*3     3*4     3*5   3*6    3*7   3*8   3*9   3*10

4*1    4*2      4*3     4*4     4*5    4*6    4*7    4*8   4*9    4*10

5*1    5*2     5*3      5*4     5*5    5*6    5*7     5*8    5*9    5*10

Sunday 3 May 2015

Making a diamond in C++

Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.

Today I am here with a source code of making a diamond in C++.

So, don't be afraid !!! Making a diamond is not a difficult task anymore !!!

I will use 6 loops for this purpose. The division of these loops are as below:

3 loops for pyramid.

3 loops for inverted pyramid.

so, it means that

Diamond=pyramid+inverted pyramid. If you know how to make a pyramid and inverted pyramid,

you can easily make a diamond.

So,below is the source code of this program.

Source code


Output


How the program works?

The first 3 loops in the source code are used for pyramid and last 3 loops are used for inverted pyramid(the logic of making a pyramid is already described in my blog).