Archive for October 2014
Fundamental data types, variable and variable scope.
VARIABLE
When we want to use any value or data in our program we want to save it in our memory so that it can be used again and again. For that purpose we store it in a variable. Variable is the actually the address of a particular memory where we can save our data. two location could not have same address that's why we could not create two variable with same name.
We can store create different types of variable depending upon the type of data we want to save. If we want to save a digit we will create a integer var, if we want to store a character we will create a character type var.
A variable must be declared before it could be used in our program. It could be declared by defining its type and its name its value or data is not necessary.
type identifier = initial_value ;(int is type in below example, identifier is name and initial value may or may not be assigned as required)
Line 1 is declaring a integer type variable with name a and it have no value initially.
Line 2 is declaring a integer type variable with name b which have value 3 so b =3.
Line 3 is declaring a integer type two variable with name a and b. we could declare multiple var in single type to reduce our space.
DATA TYPES
Scope of variables
in line 2 we created an integer a which is global variable it scope is around the whole program any where it could be used or modified as desired. No other function could have var with name (a) because (a) is already declared at every place of function.
in line 5 the var (b) and (c) are the local copies inside main. these variable could be used every where in main function but not outside.
in line 9 we have created var (b) and (d) which are the local copies of myFuntion could only be used in this function. but not outside.
In the program you could see that we have created (b) in main and also in myFunction both are the local copies so we can create a var with same name outside the scope of it.
we can not create var (a) any where in our program because its scope is all over the program and vars with same name could not be created within its own scope.
When we want to use any value or data in our program we want to save it in our memory so that it can be used again and again. For that purpose we store it in a variable. Variable is the actually the address of a particular memory where we can save our data. two location could not have same address that's why we could not create two variable with same name.
We can store create different types of variable depending upon the type of data we want to save. If we want to save a digit we will create a integer var, if we want to store a character we will create a character type var.
A variable must be declared before it could be used in our program. It could be declared by defining its type and its name its value or data is not necessary.
type identifier = initial_value ;(int is type in below example, identifier is name and initial value may or may not be assigned as required)
1. int a;
2. int b = 3;
3. int c,d;
Line 1 is declaring a integer type variable with name a and it have no value initially.
Line 2 is declaring a integer type variable with name b which have value 3 so b =3.
Line 3 is declaring a integer type two variable with name a and b. we could declare multiple var in single type to reduce our space.
DATA TYPES
NAME
|
Description
|
Size
|
Range
|
char
|
Character or small
integer.
|
1byte
|
signed: -128 to 127
unsigned: 0 to 255
|
short int
(short)
|
Short Integer.
|
2bytes
|
signed: -32768 to 32767
unsigned: 0 to 65535
|
int
|
Integer.
|
4bytes
|
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
|
long int
(long)
|
Long integer.
|
4bytes
|
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
|
bool
|
Boolean value. It can take one of two
values: true or false.
|
1byte
|
true or false
|
float
|
Floating point number.
|
4bytes
|
+/- 3.4e +/- 38 (~7 digits)
|
double
|
Double precision floating point number.
|
8bytes
|
+/- 1.7e +/- 308 (~15 digits)
|
long double
|
Long double precision floating point
number.
|
. 8bytes
|
+/- 1.7e +/- 308 (~15 digits)
|
wchar_t
|
Wide character.
|
2 or 4 bytes
|
_ 1 wide character
|
Scope of variables
1. #include <iostream>
2.. int a; //global variable
3.
4. void
main () {
5. int b,c; //local variable
6. }
7
8. void myFunction () {
9. int b,d; //local variable
10. }
in line 2 we created an integer a which is global variable it scope is around the whole program any where it could be used or modified as desired. No other function could have var with name (a) because (a) is already declared at every place of function.
in line 5 the var (b) and (c) are the local copies inside main. these variable could be used every where in main function but not outside.
in line 9 we have created var (b) and (d) which are the local copies of myFuntion could only be used in this function. but not outside.
In the program you could see that we have created (b) in main and also in myFunction both are the local copies so we can create a var with same name outside the scope of it.
we can not create var (a) any where in our program because its scope is all over the program and vars with same name could not be created within its own scope.
Lets Get Started with programming.
Writing your
first program in C/C++
According to me programming is not something to learn it
comes to you automatically by practicing. Writing program is more affectionate
in this respect then becoming bookish.
So here is your first program to start with try writing it
and understanding it.
1. // my first
program in C
2. #include <stdio.h>
3. void
main () {
4. printf("My
first program");
5. }
1. // my
first program in C++
2. #include <iostream>
3. using namespace std;
4. void
main () {
5. cout <<
"My first program";
6. }
OUTPUT PANEL
My first program
Explaining of program in C
Line
1: // my first program in C++
// indicates the start of comment all
the text written in the line next too this will be ignored as programming line
and itt will juxt be added in programming as it is. This is used for adding a
comment to tell the other programmer about any instruction or guiding him.It is
also used for the programmer to remember its own program. In this particular
case our comment is about the description of our program.
Line 2: #include
<stdio.h>
Lines that start with # are not the
regular programming lines these are the directives for pre-processor to add the
piece of code already made by different programmer which are most often used so
they are already added to the software so that time may be saved and all
programmer don’t have to write that same code every time. A programmer could
also make its own directives.
In our particular case we have added
<stdio.h> which is standard input
output header file. As we have to produce output on screen.
Line 3: void main () {
Every
program must have at least one function that is its main function. No program
could run without a main function you may add multiple function in a
program but the program will start from
its main function. Consider the construction of our main function in this
program void
main () {}
main
is the name of our function followed by parenthesis () because it is the
declaration of function.
In
parenthesis we may define any parameter for our function they are empty because
our program have no parameter. void is the return type of function return type
of any function must be defined it is the expression or value which the
function will return after its execution. We have used (void) which mean empty
our function will not return any value after its execution. The next thing is
{} anything written inside this will be the statement of our main program and
our function consist of these instruction.
It
is also called body of the function.
Line
4: printf("My first program");
This
line is statement. A statement is expression that produces some effect. In our
program this is the only line that produces some effect. printf() is command to
take out put in C lang. inside parenthesis () we write what to output. Anything written within ”” will be shown as it. printf is the function
statement that comes from the <stdio.h> that is why we have included that
file. If we want to take output in any other form other than standard output on
screen then we have to use the different directive or make one of our own.
;
is the most important part of the statement this is the indication of the end
of the statement to the compiler every single statement must be followed by
this semi colon otherwise there will be an error.
Line
5: }
Is
the end of our main function body.
Explanation in C++ for same program.
All
the things remains the same as in C. the changes are as follow.
·
Iostream is the standard input output directive
in C++.
·
Instead of printf(“”); structure
cout << “ ” ; syntax is used.
·
3rd line in C++ that is using
namespace std;
All the elements
of standard C++ library are stored in namespace std so most of source file will
contain this statement in start as we want to use C++ element cout is one of
them used in our program.
Try writing this
program to check your self. Practice make a man perfect. Best of luck. Hope our
blog will help you to reach to the destination of a good programmer.
Difference between post increment, decrement and pre increment,decrement
Most of the new programmers are stuck between post and pre increment.
What is the difference between ++a and a++. This post will clear your confusion.
++a is a pre increment type in which first of all 1 is added in a and then its value is considered in expression. where ever in pre increment type that is a++ first of all a value is considered for expression than 1 is added to a.
Did'nt Understand?? this example will make the idea clear.
if a = 3;
b = ++a; (In this case there is pre-increment a will increase and then its value is returned to b so b will be 4 and a will also become 4)
if a = 3;
b = --a; (In this case there is pre-decrement a will first decrease and then its value is returned to b. so b will be equal to 2 and a also will be 2)
if a = 3;
b = a++ (In this case there is post-increment a value will be returned to b and then a will increase by 1 that is b will be 3 and a will become 4)
if a = 3;
b = a-- (In this case there is post-decrement a value will be returned to b and then a will decrease by 1 that is b will be 3 and a will become 2)
What is the difference between ++a and a++. This post will clear your confusion.
++a is a pre increment type in which first of all 1 is added in a and then its value is considered in expression. where ever in pre increment type that is a++ first of all a value is considered for expression than 1 is added to a.
Did'nt Understand?? this example will make the idea clear.
if a = 3;
b = ++a; (In this case there is pre-increment a will increase and then its value is returned to b so b will be 4 and a will also become 4)
if a = 3;
b = --a; (In this case there is pre-decrement a will first decrease and then its value is returned to b. so b will be equal to 2 and a also will be 2)
if a = 3;
b = a++ (In this case there is post-increment a value will be returned to b and then a will increase by 1 that is b will be 3 and a will become 4)
if a = 3;
b = a-- (In this case there is post-decrement a value will be returned to b and then a will decrease by 1 that is b will be 3 and a will become 2)
Sunday, 12 October 2014
Posted by Unknown
What is Data hierarchy
Data hierarchy
Data hierarchy is the step by step representation of complex
data in different level starting from basic bits to highest level files.
We will discuss in this post how this hierarchy is formed.
Suppose we have a excel
file in which there are the names of some students and their marks in a subject.
The file is the highest level of data-hierarchy . Next to it
is the record a person name together with its marks is referred to as records.
Inside record the name of the student will be a field. A field is combination
of characters. An individual character will then be represented by a byte. That
byte will be combination of a bit. The picture below will make the idea clear
what is object oriented programming?
Introduction to
object oriented programming.
Basically all the programming languages are of two types
either:
·
Structured Oriented or
·
Object Oriented.
Today most of the languages used are object oriented like
java as2.0, as3.0 and C++.
So the question arises when we say object oriented
programming what we mean by it or what is object oriented programming. This
topic will clear the basic the idea and introduce you to the basic concept of
it.
Object Oriented
Programming.
In object oriented programming we make different objects,
give them some instance names, and then call them by their instance name to
change some of their attributes to perform a function.
Didn’t Understand??
This example will make the idea clear.
Suppose we created a robot as shown below.
We will select this robot and make this an object. Now the
next step is to give it a name that is instance name. We can give any instance
name to it with some restriction which I may explain in any other post. So , suppose
any name for example we gave this robot instance name of robo . Now in our programming if we want the robot to do anything
we can simply call it by its instance name robo.
Our object will have some attributes like its position from
x axis, y axis, its rotation and many other. Suppose we want to move robot
forward when we press right key. We can do this by revealingly increasing its
distance from x-axis.
So our pseudo-code or general algorithmic form for that will be
While (right
key){
robo x-axis increase by 1
}
So in this example we are calling an object and changing its
attributes to do required task this type of programming is called object-oriented
programming.
Making a boundary of rectangle. square in C++
Creating a hollow square or rectangle in C++
// a rectangle boundry by asteriks
//including header files
#include<iostream>
#include<conio.h>
//including directives of iostream
using std::cout; //see out. for output same function as printf
using std::cin; //see in. for output same function as scanf
using std::endl; //end line same as \n
//main function starting
void main() {
//initializing vars
int length, width, row, column;
//length input
cout << "Enter length less then 39: ";
cin >> length;
while (length>38){
cout << "plz Enter length less then 39: ";
cin >> length;
}
//initializing column of the rectangle
column = length;
//Taking width input
cout << endl << "Enter the Width: ";
cin >> width;
row = width;
//new line
cout << endl;
//printing length of the rectangle in first line
for(column; column >(length/2+1); column--){
cout << " ";
}
cout << length;
//new line
cout << endl;
column = length;
//marking length of rectangle
for(column; column >0; column--){
cout << "--";
}
cout << endl;
column = length;
//begining of formation of rectangle
for(row ; row >0; row--) {
//top and bottom of rectangle
if (row == width || row == 1) {
for(column; column >0; column--){
cout << "* ";
}
cout << "|";
cout << endl;
column = length;
}
//middle portion of rectangle
else {
cout << "* ";
for(column; column >2; column--){
cout << " ";
}
cout << "*";
cout << " |";
if (row == (width/2+1)) {
cout << width;
}
cout << endl;
column = length;
}
}
_getch();
}//end main
Saturday, 4 October 2014
Posted by Unknown
Tag :
c++,
code,
coding,
Creating,
drawing,
making,
project c++(basic),
rectanagle,
square,
tutorial