Programming with C and C++


C / C++ programming language

Programs
A set of instruction to perform a certain task is called as programs

Software
        One or more programs are called software

Smiley Face: Source Code

Compiler
 
 






01 10
Language
        àTo communicate with other we use language

Types of language
1.   Low level àmachine language, assembly
2.   Middle levelàC programs
3.   High levelàFORTRAN,COBOL,BASIC



Binary digit
Bit
0101010101111000000111

How to start C programs
My computer
C:
TurboC2
Bin
Tc

Start
Runàc:\turboc2\bin\tc
Ok

Full screen àalt + enter
Close à alt + x


Parts of C programs
·       Head
·       Body
·       End






Details in C programs
1.   Comments
2.   Header files
3.   Body (function)
4.   Programs statement




/* Prog for display massage */
#include<stdio.h>           (standard input output header)
#include<conio.h>         (console input output)
main()
{
clrscr();                           (clear screen)
printf(“Hello world “);   (print in format)
getch();                           (get character )
}


Compile àAlt+F9
Run -àCtrl + F9


//my first prog in c++  Single line comments
/*Multilinee
comments*/

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Hello I Am Ravisha";
getch();
}

//my first prog in c++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"****************************\n*  Name-Rishika gupta      *\n* 

college-G.E.C jagdalpur *\n";
cout<<"*  brach-civil             *\n*  DOB-05/02/98            *

\n****************************";
getch();
}



Variable, data types and constant

Variable
Variable is data name, which is used to store data values, and it takes different values at different times

Roll_no, name, mark
X
Y
Z

Rules for variable creation
1.   Must start with a character
2.   White spaces not allowed
3.   Special character not allowed
4.   Keywords not allowed
5.   Should have meaning full

Q. Find out the error in variable and explain why

1.   a=L*&b
2.   si=@p*r*t/100
3.   date of birth
4.   –year
5.   2011
6.   8area
7.   area4
8.   year_end

Data types
        Data type specify the value of any variable

1.   int (integer) ->whole number
a.    -32768 to 32767
2.   float / double àreal number
a.    400.50000
3.   char (character)àused for character
‘a’-‘z’,   ‘A’- ‘Z’


int var;
var=755;


float var;
var=45.60000;

char var;
var=’B’;


constant
value of variable is called constant which does not change during programs execution



Format string
Conversion character
What for use
%d
int
%f
float / double
%c
char
%s
string
                
//wap to display the use of variable
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
//=====variable declaration ========
int roll;
float marks;
char grade;

//=====Value assignment =======
roll=101;
marks=76.89;
grade='B';

//*******=Display======
cout<<"Roll no ="<<roll;//cascading of insertion operator
cout<<"\n marks gained="<<marks<<endl;
cout<<"grade achieved="<<grade;
getch();
}

Operators
Operator is a symbol which perform a specific task on operand to produce a result
10
x
Equals (=)
This operator transfers the right side operand into left side, and used to assign a value.

WAP to interchange two values

//prog for interchange the value of var
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int x,y,t;
x=20;
y=30;

cout<<"Before swap "<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;

//coding for interchange
t=x;
x=y;
y=t;

cout<<"After swap "<<endl;
cout<<"x= "<<x<<endl;
cout<<"y= "<<y<<endl;


getch();
}

Types of operator
1.   Arithmetic operator
a.    +, -.*, /, %
2.   Relational operator
a.    <, >, <=, >=, = =, !=
3.    logical operator
a.    AND &&, OR ||, NOT!
4.   Conditional operator
a.     ? :
5.   Increment / decrement operator
a.    ++, --
6.   Assignment operator
a.    +=, -=, *=, /=,%=

Arithmetic operator
        +, -, *, /, %
        This operator is used for mathematical operation

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

float bs,da,hra,ts;
cout<<"enter the value of bs";
cin>>bs;

da=bs*0.4;
hra=bs*0.2;

cout<<"Da="<<da<<endl;
cout<<"Hra="<<hra<<endl;
ts=da+hra+bs;

cout<<"value of total salary"<<ts;
getch();
}


//wap to calculate simple interest
#include<iostreae.h>
#include<conio.h>
viod main()
{
clrscr();
float p,r,t,si;
cout<<"enter the p";
cin>>p;
cout<<"enter the r";
cin>>r;
cout<<"enter the t';
cin<<t;
si=(p*r*t)/100;
cout<<"simple intrest is"<<si;
getch():
}
Questions
1.   WAP to accept basic salary of employee and calculate net salary on following condition
a.    DA is 40% of basic
b.   HRA is 20% of basic

WAP to accept five subjects marks “Hindi, English math’s, science, social science” and calculate total marks, percent



WAP to accept five digit number and display in reverse order 12345à
54321

32146
64123



Relational operator 
This operator is used to denotes relation between two operand
<, >, <=, >=, = =, !=

Control statement
1.  Simple if
2.  if ……else
3.  nested if…..else
4.  if ……….else ladder





Simple if statement

                                                                                                       
//prog for simple if statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x;

cout<<"Enter any no ";
cin>>x;

if(x>5)
{
cout<<"X is greater no";
}
getch();
}

If else statement




#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr ();
int x;
cout<<"enter the value of x=";
cin>>x;
if(x>10)
{
cout<<"the number is greater than 10";
}
else
{
cout<<"the number is lesser than 10";
}
getch ();
}

//prog for if else statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x;

cout<<"Enter any no ";
cin>>x;

if(x>5)
{
cout<<x<<" is greater 5";
}
else
{
cout<<x <<"is is less than 5";
}
getch();
}


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x;
cout<<"enter the value of x";
cin >>x;
if (x>5)
{
cout<<"the number is positive no";
}
else
{
cout<<"the number is negative no";
}
getch();
}


Nested if else

//prog for nested if else statement
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;

cout<<"Enter 1st no ";
cin>>x;

cout<<"Enter 2nd no";
cin>>y;

if(x==y)
{
cout<<"Both are equal ";
}

else if(x>y)
{
cout<<x<<" is greater "<<y;
}
else
{
cout<<x <<"is is less than "<<y;
}
getch();
}

WAP to accept basic salary of employee calculate his net salary “if his basic is less than  5000 there is NO tax and if basic is greater than 5000 and less than 10000 there is 5% tax and >10,000  there is 10% tax

WAP to accept any number and check the number is even number or odd number









If………else   ladder





Logical operator
      AND &&, OR ||, NOT!
      The logical operators are used when we want to test more than one condition and make decisions.

Conditional operator
      ? :
(expr1? expr2: expr3)






Increment / decrement
Increment operator increases the value of any variable by one.
X++

AND
Decrement operator decrease the value of variable by one


·      Prefix notation
o   ++var / --var;
·      Postfix notation
o  Var++; / var--;




x=20

y=++x;

x=21   y=21




a=5
b=a++;
c=++b;
d=++c;

b=?  d=?


j=9

k=++j
l=j++
m=k++
n=m++

n=10,m=?
        

x=10;

y=x++;
z=++y;
x1=z++;
y1=x1--;
z1=x1++;

x1=?  z1=?

7.                    Assignment operator
+=, -=, *=, /=,%=

x=10

x+=5;        OR x=x+5

x=15

x*=4;



The switch statement
      The if statement occurs so many times the complexity of such programs increases dramatically. And programs difficult to read and follow. So the switch statement tests the value of a given variable against a list of case values and when a match is found, a block of statement with that case is executed.

switch(expression)
{
case value-1:
      Block-1
      break;
case value-2:
      block-2
      break;
…………..
……….
default :
      default-block
      break;
}







Loop (iteration)
If you want to continuously repeat the any programs or parts programs in certain number of times we use loop.

Types of loop
·      while loop
·      do-while loop
·      for loop





while loop(Entry control)
       in while loop first the conditions test than proceed the programs
      
do-while(exit control)
in this loop first execute the programs then condition test.

For loop
This loop is fixed loop and have three expressions

for(initialization expr;test expr;incre/decre )






                                                                                                Jumps in loops
                Loops perform a set of operations repeatedly until the control variable fails to satisfy the test condition. The number of times loop is repeated is decided in advance and the text condition is written to achieve this. Sometimes when executing a loop it becomes desirable to skip a part of the loop or leave the loop as soon as certain condition occurs.


Jumping out of a loop
          break;


Skipping a part of a loop
          continue;

Nested for loop
          Nesting of loops, that is, one statement within another for statement.








`
Array
          The variables can stores only one value at any given time. Therefore they can be used only to handle limited amount of data. However we need to handle large volume of data in terms of reading Processing and printing. So we need a powerful data type

Array is a fixed size sequenced collection of elements of the same data type
         
10 20 30 50 65 46 76











String Handling Function
  C supports a large number of Strings handling function that can be used to carry out many of the string manipulation.
Function
Action
strcat()
strcmp()
strcpy()
strlen()
Concatenates two strings
Compares two strings
Copies one strings over another
Finds the length of strings


User-Defined Functions
        Function in, C programs can be classified into two categorized namely, library functions, and user-defined functions, main is example of user-defined function and printf and scanf is library function

main is a specially recognized function is C. every programs must have a main function to indicate where the programs has to begin its execution.

If the programs may becomes too large and complex and as results the task of debugging, testing and maintaining becomes difficult. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. These independently coded programs are called subprograms that are much easier to understand, debug and test. In C such subprograms are referred to as functions.

 
Definition of function
Ø Function name
Ø Function type
Ø List of parameters.
Ø Local variable declaration
Ø Function statements
Ø A return statement

All the six elements are grouped into two parts, namely,
·       Function header (first three elements)
·       Function body (second three elements)




Category of function
        Category 1:     function with no arguments and no return values.
        Category 2:     function with arguments and no return values.
        Category 3:     function with arguments and one return values.
        Category 4:     function with no arguments but return a value.
        Category 5:     function that return multiple values.

1: Function with no arguments and no return values.

Function 2 ()
{
…………………………………………………………………..
}
 

Function 1 ()
{
……………………………… function 2()
…………………………………..
}
 
                               
No input



No output
 


2: Function with arguments and no return values.

Function 2 ()
{
…………………………………………………………………..
}
 

Function 1 ()
{
………………………………… function 2()
…………………………………..
}
 
                               
Input



No output

3: Function with arguments and one return values.

Function 2 ()
{
…………………………………………………………………..
}
 

Function 1 ()
{
………………………………… function 2()
…………………………………..
}
 
                               
input



output
 



4: function with no arguments but return a value.




Function 2 ()
{
…………………………………………………………………..
}
 

Function 1 ()
{
………………………………… function 2()
…………………………………..
}
 
                               
No input



output
 




Recursion
        When a called function in turn calls another function a process of “chaining” occurs. Recursion is a special case
Of this process, where a function calls itself.

function 1 ()
{
………bces……………………… function 1()
…………………………………..
}
 
 







main()
{
Printf(“Hello world”);
main();
}


The scope visibility and lifetime of variable
        The scope of variable determines over what region of the program a variable is actually available for use (‘Active’). Longevity refers to the period during which a variable retains given value during execution of a programs (‘alive’). So longevity has direct effect on the utility of a given variable. The visibility refers to the accessibility of a variable from the memory.



Scope rules

Scope
The scope of a program in which a variable is available

Visibility
The program’s ability to access a variable from memory

Lifetime
The lifetime of a variable is the duration of time in which a variable exist in the memory during execution.

Variable storage class
1)  Automatic variable (local / internal)
2)  External variable (global)
3)  static variables
4)  Register variable

static
        àas the name suggest, the value of static variables persist until the end of the program.

Structure and unions
        arrays can be used to represent a group of data items that belongs to the same type, such as int, float, char. However, we cannot use an array if we want to represent a collection of data items of different types using a single name. C supports a constructed data type known as structure. A mechanism for packing data of different type.

Structure is a collection of data items of different data type

Declaration of structure

struct tag_name
{
Data_type       member1
Data_type       member2
…………………………
……………………….
};

struct book_bank
{
char        title[20];
char                 author[15];
int            pages;
float         price;
};



UNIONS
        Unions are a concept borrowed from structures and
therefore follow the same syntax as structure. However there is a major distinction between them in terms of storage. In structures, each member has its own storage location, whereas all the member of a unions use the same location, it can handle only one member at a time.

union       item
{
int           m;
float         x;
char                 c;
}code;




Pointer
Pointer is a variable which contains the address of another variable.


int *p;
p=&x;



File management in C
        The entire data is lost when either the program is terminated or the computer is turned off. It is necessary to have a more flexible approach where data can be stored on the disk and read whenever necessary, without destroying the data. This method employ the concept of files to store data, a file is a place on the disk where a group of related data is stored, like most other languages.

File operations
1.   naming a file
2.   opening a file
3.   reading data from file
4.   writing data to a file
5.   closing a file



Comments

Popular posts from this blog

Tally Erp9 with Advanced Gst

Computer fundamental and ms office