20120124

How to pass a object and return a object in C++/Cpp

//How to pass a object and return a object in C++/Cpp
//Program to show how to pass a object and return a object
// How to use friend function in C++/ Cpp
#include<iostream>
using namespace std;
class complex
{
private:
        float x;
        float y;
public:
        void getdata(float real, float imag)
        {
        x=real;
        y=imag;
        }
        friend complex sum(complex, complex);
        void show(complex);
};
//defining friend function outside the class
 //does not require scop resolution operator
complex sum(complex C1, complex C2)// friend function defined outside the class
//passing objects as function arguments
{
complex C3;
C3.x= C1.x + C2.x;
C3.y= C1.y + C2.y;
return(C); //returning object C

How to acces private member function in C++/CPP

How to acces private member function in C++/CPP
Program to show accessibility of private member function.
//private member function(skeleton) can be accessed using
//member function only and not by object and dot operator
#include<iostream>
using namespace std;
class item
{
private:
        int number;
        float price;
        void getdata(int a, float b) // here member function getdata
                                               //is private so not accessible by object

20120123

Why destructor should be virtual in C++ ?

Why destructor should be virtual in C++ ?
Memory Leak ?

When delete is called virtual destructor makes sure to release memory of first derived class object then of by base class object. If destructor is not virtual then only the base class object's memory get released and not that of derived class object.
example:
#include<iostream>
using namespace std;
class base
{
  base(){};
 ~base(){};
};
class derived:public
{
  derived(){};
 ~derived(){};
};
int main()
{
base *ptr= new derived(); //exp1
delete ptr; //exp2
}
Explanation:
Line exp1 makes the derived and base constructoes to be called to allocate memory for base as well derived class objects.
But, if destructor is not virtual in base class then
Line exp2 makes to call the base destructor and derived destructor never gets called and hence memory allocated to derived class object is not release and leads to memory leak because memory used by derived class object can not be used util the end of the program.
Simples Way to learn Data Structure in C++ CPP or C
C++ Program for Circular Queue implementation using linked list
 
C++ program for Circular Queue implementation using Array

C++ program for reversing single Linked list nodes 

C++ Program for sorted single linked list with class

C++ Program for BST-Binary Search Tree   


Previous                             Home                               Next

20120118

How to use tricky while loop in CPP,C++,C

#include<stdio.h>
int main()
{
int i=0;
while(i<5)
        {
        printf("\n Iteration No=%d",i);
        }
return 0;
}
////////////////////
o/p = infinite times Iteration No=0 as we are not incrementing the i
///////////////////

How to use for loop in C++ CPP C

//==========================
//What will be the out put in C++, C or CPP
//1.Tricky for loop program
===========================
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=5;i++); //when i=6 condition will become false and
                              //control flow will go next stmt to print once
        {

20120113

Program in C++ to learn virtual function

//========================
//Program in cpp to learn virtual function, vrtuual1.cpp
//========================

#include<iostream>
using namespace std;
class base
{
public:
      virtual   void func()
        {
        cout<<"base function \n";
        }
};

How to use Virtual function in C++

//========================
//Program in cpp to learn virtual function, virtu.cpp
//========================

How to use Virtual function C++

//========================
//Program in cpp to learn virtual function, vertuual1.cpp
//========================

Program to create a table in C

//========================
//Program in c to create a table of square , table.c
//========================

How to use Structures in C

//========================
//Program in Cto learn structures in C, struct4.c
//========================
// array of structures, collection of collection of hetrogeneous data types

#include<stdio.h>
int main()
{
struct book
        {
        char name;
        int pages;
        float price;
        };
        struct book b[5]; // b is array of 5 books
        int i;
        for(i=0;i<5;i++)

How to use structures in C language

//========================
//Program in C to learn structures in C, struct3.c
//========================

Program in C to learn structures in C

//========================
//Program in C to learn structures in C, struct2.c
//========================
#include<stdio.h>
int main()
{
        struct book
        {
        char title;
        int page;
        float price;
        };
        struct book b1,b2,b3;
        printf("\n Enter the title, page and price for the first book \n");
        scanf("%c  %d  %f",&b1.title, &b1.page, &b1.price);
        printf("\n Details of first book are: %c %d %f \n",b1.title,b1.page,b1.price);

        printf("\n Enter the title, page and price for the Second book \n");
        scanf("%c  %d  %f",&b2.title, &b2.page, &b2.price);
        printf("\n Details of first book are: %c %d %f \n",b2.title,b2.page,b2.price);
        printf("\n Enter the title, page and price for the third book \n");
        scanf("%c  %d  %f",&b3.title, &b3.page, &b3.price);
        printf("\n Details of first book are: %c %d %f \n",b3.title,b3.page,b3.price);

return 0;
}
Previous                             Home                               Next

Program in C to learn structures in C

//========================
//Program in C to learn structures in C,struct1.c
//========================

Program in C++ to learn to reverse a strings in Cpp

//========================
//Program in C++ to learn to reverse a strings in Cpp, strrevcpp.cpp
//========================

Program in C++ to learn to reverse a strings in Cpp

//========================
//Program in Cpp to learn to reverse a strings in Cpp, strrevcpp3.cpp
//========================

Program in Cpp to learn to reverse a strings in Cpp

//========================
//Program in Cpp to learn to reverse a strings in Cpp, strrevcpp2.cpp
//========================
#include<iostream>
using namespace std;
#include<string.h>
int main()
{
char orgstr[]="manoj";
cout<<"orginal string is:\n"<<orgstr;
char revstr[50];
char *p1,*p2;
p1=orgstr+strlen(orgstr)-1;
p2=revstr;
while(p1>=orgstr)
*p2++=*p1--;
*p2='\0';
cout<<"\n reversed string is: \n"<<revstr;
return 0;
}


Previous                             Home                               Next

Program in C++ to learn to reverse a strings in Cpp

//========================
//Program in Cpp to learn to reverse a strings in Cpp, strrev.cpp
//========================
#include<iostream>
using namespace std;
int main()
{
char str[]="ProgramtoReverseString";
int length= strlen(str);
cout<<"***********Reversing a String******************";
cout<<"\n The given String is: \n \t";
for(int i=0;i<length;i++)
  {
   cout<<str[i];
  }
//Reversing the String
int length2=length/2;
length--;
for(int i=0;i<length2;i++)
   {
   str[i]=str[i]+str[length-i];
   str[length-i]=str[i]-str[length-i];
   str[i]=str[i]-str[length-i];
   }
 cout<<"\n The Reversed String is:\n  \t "<<str;
 cout<<"\n";
return 0;
}
Previous                             Home                               Next

Program in C to learn to reverse a strings in C

//========================
//Program in C to learn to reverse a strings in C,stringreverse.c
//========================
#include<stdio.h>
#include<string.h>
int main()
{
char name[]="SalmanKhan";
printf("%s \n",name);
strrev(name);
printf("%s \n",name);
return 0;
}

Previous                             Home                               Next

Program in Cto learn strings using pointers in C

//========================
//Program in Cto learn strings pointers in C, stringptr.c
//========================
#include<stdio.h>
int main()
{
char name[]="Karnighan";
int *ptr;
ptr=name;
        int i=0;
        while(*ptr != '\0')
        {
        printf("%c",*ptr);
        ptr++;
        }
return 0;
}


Previous                             Home                               Next

Program in C to learn strings in C language

//========================
//Program in C to learn strings in C, string2s.c
//========================
include<stdio.h>
int main()
{
char name[]="Ritchie";
printf("%s \n",name);
return 0;
}
Previous                             Home                               Next

Program in C to learn strings in

//========================
//Program in C to learn strings in C, string1.c
//========================
#include<stdio.h>
int main()
{
char name[]="DannishRitchie";
int i=0;
        while(name[i]!='\0')
        {
        printf("%c",name[i]);
        i++;// If not incrementing while loop then its a infite loop as it will never reach to \0
        }
return 0;
}

Previous                             Home                               Next

Program in C to learn static variable in C

//========================
//Program in C to learn static variable in C
//========================
#include<iostream>
using namespace std;
class base
{
public:
        static int x;
        void modifyx()
        {
        x++;
        }
        void displayx()
        {
        cout<<"\n x="<<x;
        }
};
 int base::x=0;
class derived:public base
{
};
int main()
{
base b;
derived d;
b.modifyx();
b.displayx();
d.modifyx();
d.displayx();
return 0;
}
Previous                             Home                               Next

Program in C to learn right shift

Program in C to learn right shift, shiftright.c


#include<stdio.h>
int main()
{
printf("\n Bit wise left shift demo \n");
unsigned int value=32;
unsigned int shift=3;
value=value>>shift;//32>>3 means 32/(2*2*2)//means 4/(3 times 2)
printf("Result is: %d \n" ,value);
return 0;
}

Previous                             Home                               Next

Program in C to learn shift opertor

//========================
//Program in C to learn shift opertor, shiftop.c
//========================
#include<stdio.h>
int main()
{
printf("\n Bit wise left shift demo \n");
unsigned int value=4;
unsigned int shift=3;
value=value<<shift;//4<<3 means 4*(2*2*2)//means 4*(3 times 2)
printf("Result is: %d \n" ,value);
return 0;
}

Previous                             Home                               Next

Program in C to reverse a string using pointer

//========================
Program in C to reverse a string using pointer, revstrusingptr.c

#include<stdio.h>
#include<string.h>
int main()
{
char orgstr[50];
char revstr[50];
char *p1,*p2;
printf("Enter the string to reverse:\n");
scanf("%s",orgstr);
printf("You entered: %s \n", orgstr);
p1=orgstr+ strlen(orgstr)-1;
p2=revstr;
while(p1>=orgstr)
*p2++ = *p1--;
*p2='\0';
printf("Reversed String is: %s \n",revstr);
return 0;
}



Previous                             Home                               Next

Program in C to reverse a string

Program in C to reverse a string, revstr99.cpp

#include<iostream>
using namespace std;
int main()
{
char orgstr[50];
char revstr[50];
cout<<"Enter a String: "<<endl;
cin>>orgstr;
char *p1,*p2;
p1=orgstr+ strlen(orgstr)-1;
p2=revstr;
while(p1>p2)
        {
        *p2++ = *p1--;
        }
*p2='\0';
cout<<"Reversed String is:"<<revstr;
cout<<endl;
return 0;
}


Previous                             Home                               Next

Program in C to reverse a string

Program in C to reverse a string,Reversing a String

#include<stdio.h>
void reverse(char s[]);
int main()
{
char text[50]="India";
printf("\n Text is :%s",text);
reverse(text);
printf("\n Reversed Text is :%s \n",text);
return 0;
}
void reverse(char s[])
{
int c,i,j;
for(i=1,j=strlen(s)-1; i<j; i++,j--)
        {
        c=s[i];
        s[i]=s[j];
        s[j]=c;
        }
}
Previous                             Home                               Next

Program in C to learn pointer in C


Program in C to learn pointer in C, ptr1.cpp

#include<iostream>
using namespace std;
int main()
{
int numbers[50],*ptr,n,i;
cout<<"\n Enter the counts of number: \n";
cin>>n;
 cout<<"Enter the numbers 1 by 1 \n";
 for(i=0;i<n;i++)
 cin>>numbers[i];
  //checking for the even numbers and adding
  ptr=numbers; //assigning the base address of the array numbers to ptr
  //ptr=numbers[0];
int sum=0;
for(i=0;i<n;i++)
 {
 if((*ptr%2)==0)
   {
   sum+=*ptr;
   ptr++;
   }
  }
        cout<<"Sum of even Numbers in the array numbers is:"<<sum;
        return 0;
}

Previous                             Home                               Next

Program in C to learn preprocessor directive,

Program in C to learn preprocessor directive, preprocessor.c

#include<stdio.h>
#define size 5
int main()
{
int i=1;
for(i=1;i<=5;i++)
printf("\n %d \n", i);
return 0;
}
Previous                             Home                               Next

How to use Pointer Arithmatic in C++

Program in c to learn pointer in C
#include<iostream>
using namespace std;
int main()
{
int num[]={10,20,30,40,50};
int *ptr1;
ptr1=num;//assigning the base address of array of integer
cout<<"Array elements are: \n";
int i;
for(i=0;i<5;i++)
{
cout<<num[i];
cout<<"\n";
}
return 0;
}
Previous                             Home                               Next

How to use pointer arithmatic in C++

Program in c to learn pointer in C, pointerarithmatic2.cpp

#include<iostream>
using namespace std;
int main()
{
int num[]={10,20,30,40,50};
int *ptr1;
ptr1=&num[0];//assigning the base address of array of integer
//ptr=num;
cout<<"Value of ptr is:"<<*ptr1<<"\n";
ptr1++;
cout<<"value of ptr1++ is \n"<<*ptr1<<"\n";
ptr1++;
cout<<"value of ptr1++ is \n"<<*ptr1<<"\n";

ptr1--;
cout<<"value of ptr1-- is \n"<<*ptr1<<"\n";
cout<<"Now adding an integer to the pointer\n";
ptr1=ptr1+2;
cout<<"value of ptr1 +2 is \n"<<*ptr1<<"\n";

cout<<"Now ubtraction 1 pointer from another \n";
//ptr1=ptr1+2;

return 0;
}
Previous                             Home                               Next

How to learn Pointers in C++

Program in c to learn pointer in C,pointer2.cpp

#include<iostream>
using namespace std;
int main()
{
int a=10;
int *ptr1, **ptr2; //note thatptr2 is ptr to ptr
ptr1=&a;
ptr2=&ptr1;
cout<<"Value of a is : \n"<<a<<"\n";
cout<<"Value of a is : \n"<<*ptr1<<"\n";

cout<<"Manipulating the value of contained by pointer \n";
*ptr1=(*ptr1)/2;
cout<<"Value of a is : \n"<<a<<"\n";
cout<<"Value of a is : \n"<<*ptr1<<"\n";
//cout<<"Adress of a is : \n"<<ptr1<<"\n";
//cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
return 0;
}

Previous                             Home                               Next

How to use pointer in C++


//Program in c to learn pointer in C, pointer1.cpp

#include<iostream>
using namespace std;
int main()
{
int *ptr1, **ptr2, a; //note thatptr2 is ptr to ptr
ptr1=&a;
ptr2=&ptr1;
cout<<"Adress of a is : \n"<<ptr1<<"\n";
cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
return 0;
}
Previous                             Home                               Next

How to initialization member in derived constructor

Program in c to learn "member initialization example in derived constructor", memberil.cpp

#include<iostream>
using namespace std;
class cycle
{
protected:
int x;
public:
cycle(int a)
{
x=a;
}
};
class bike
{
protected:
int y;
public:
bike(int b)
{
y=b;
}
};
class car:public cycle,public bike
{
protected:
float m,n;
public:
car(int a, int b, float c, float d):cycle(a), bike(b)
{
m=c;
n=d;
}
void display()
{
cout<<"*********member initialization example in derived constructor******"<<"\n";
cout<<"x,y,z,m and n are initialized to"<<x<<" "<<y<<" "<<m<<" "<<n<<"\n";
}
};

int main()
{
car g(1,2,3.7,4.7);
g.display();
return 0;
}


Previous                             Home                               Next

How to use malloc function in C

//Program in c to learn malloc function, mallocinc.c
How to use malloc in C. Simple Program for malloc in C.

->malloc function allocates required amount of memory in heap (instead of stack) and
returns the pointer to the address of allocated block of memory.
->We need to type cast the returned pointer by malloc as it returns a void pointer.
->memory allocated by malloc in heap is deallocated by free, so that memory in heap can be
  allocated by other malloc functions.
->Syntax of malloc in C
datatype *p;
p=(datatype *)malloc(sazeof(datatype));
example of malloc:
int *p;
p=(int *)malloc(100*sizeof(int));

Explanation:
Here, malloc function allocates 400( 100*size of integer ie 100*4=400 ) bytes in heap and
assigns the address of the allocated memory in integer pointer p.
How to use malloc in C.
Simple Program for malloc in C.
#include<stdio.h>
int main
{
int *p;
p=(int *)malloc(sizeof(int));
*p=10;
printtf("%d",*p);
free(p);
return 0;
}


//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c
//Program in c to learn malloc function, mallocinc.c

#include<stdio.h>
int main()
{
char name[5];
int i,len;
char *p;
char *pname[5];
for(i=0;i<=5;i++)
        {
        printf("Enter a name: ");
        scanf("%s", name);
        len=strlen(name);
        p=(char *)malloc(len-1);
        strcpy(p,name);
        pname[i]=p;
        }

        for(i=0;i<=5;i++)
        {
        printf("%s",pname[i]);
        }
return 0;
}

//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c
//How to use malloc function in C, mallocinc.c


Previous                             Home                               Next

How to use malloc in C

//Program in c to learn malloc function, mallocinc2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char name[10];
printf("Enter a Name:");
scanf("%s",name);
printf("\n You entered: %s \n", name);
char *p;
p=(char *)malloc(sizeof(strlen(name)-1));
strcpy(p,name);
printf("Copied string is: %s \n",p);
return 0;
}

Previous                             Home                               Next

How to use fork in C++

//Program in c to learn fork,linux1.cpp
#include<iostream>
using namespace std;
int main()
{
fork();
fork();
fork();
fork();
fork();
cout<<"Forked \n";
return 0;
}

Previous                             Home                               Next

How to use fork in C++


//Program in c to learn fork, linux1.c

#include<iostream>
using namespace std;
int main()
{
fork();
fork();
fork();
fork();
fork();
cout<<"Forked";
return 0;
}
Previous                             Home                               Next

How to use Linked list in C++

//Program in cpp to demonstrate linkedlist, linkedlist.cpp
#include<iostream>
using namespace std;
struct node
        {
        public:
        int data;
        node * next;
        };
class linkedlist
        {
        private:
        node* first;
        public:
        linkedlist()
                {
                first=NULL;
                }
        void getdata(int d);
        void display();
        };
void linkedlist::getdata(int dd)
        {
        node* newnode=new node;
        newnode->data=dd;
        newnode->next=first;
        first=newnode;
        }
void linkedlist::display()
        {
        node* current=first;
        while(current!=NULL)
                {
                cout<<current->data<<endl;
                current=current->next;
                }
        }
int main()
{
linkedlist li;
li.getdata(10);
li.getdata(15);
li.getdata(20);
li.getdata(18);
li.getdata(19);
li.display();
return 0;
}
You may also Like:
C++ Program for Circular Queue implementation using linked list  
C++ program for Circular Queue implementation using Array
C++ program for reversing single Linked list nodes 
C++ Program for sorted single linked list with class
C++ Program for BST-Binary Search Tree  

Previous                             Home                               Next

How to use Inheritance in C++

//Program in cpp to demonstrate inheritance, inherit.cpp

#include<iostream>
using namespace std;
class employee
{
protected:
int empid;
public:
void getempid(int a)
{
empid=a;
}
void showempid()
{
cout<<"********* Details of employee**********"<<"\n";
cout<<"Emp No is:"<<empid<<"\n";
}
};
class hike:public virtual employee
{
protected:
float hike1;
float hike2;
public:
void getHike(float h1, float h2)
{
hike1=h1;//line 43
hike2=h2;
}
void showHike(void)
{
cout<<"Hike in year 1 is :"<<hike1<<"\n";
cout<<"Hike in year 2 is:"<<hike2<<"\n";
}
};
class promotion:public virtual employee
{
protected:
float promotion_score;
public:
void getPromotion(float s)
{
promotion_score=s;
}
void showPromotionScore(void)
{
cout<<"Score in promotion is:"<<promotion_score<<"\n";
}
};

class result:public hike, public promotion
{
protected:
float total;
public:
//total=hike1+hike2+promotion_score;
void display();
};
void result::display()
{
total=hike1+hike2+promotion_score;
 showempid();
 showHike();
 showPromotionScore();
 cout<<"Total score is :"<<total<<"\n";
}
int main()
{
result Raheem;
Manoj.getempid(111);
Manoj.getHike(9.10, 9.5);
Manoj.getPromotion(8.2);
Manoj.display();
return 0;
}


How to use Inheritance in Cpp, C++, C plus plus with example

Previous                             Home                               Next

How to use Inheritance in C++ Program


//Program in cpp to demonstrate inheritance, inh2.cpp

#include<iostream>
using namespace std;
class base
{
private:
        void prifun()
        {
        cout<<"\n private Function";
        }

public:
        void pubfun()
        {
        cout<<"\n public  Function";
        }
protected:
        void profun()
        {
        cout<<"\n protected Function";
        }
};
class derived:protected base
{
public:
        void fun()
        {
       //prifun();
        pubfun();
        profun();
        }
};
int main()
{
base b;
derived d;
//b.prifun();
b.pubfun();
//b.profun();
//d.prifun();
//d.pubfun();
//d.profun();
d.fun();
return 0;
}
Previous                             Home                               Next

How to use Inheritance in C++

//========================
//Program in cpp to demonstrate inheritance, inh1.cpp
//========================
#include<iostream>
using namespace std;
class base
{
private:
        void prifun()
        {
        cout<<"\n private Function";
        }

public:
        void pubfun()
        {
        cout<<"\n public  Function";
        }
protected:
        void profun()
        {
        cout<<"\n protected Function";
        }
};
class derived:public base
{
public:
        void fun()
        {
       //prifun();
        pubfun();
        profun();
        }
};
int main()
{
base b;
derived d;
//b.prifun();
b.pubfun();
//b.profun();
//d.prifun();
d.pubfun();
//d.profun();
d.fun();
return 0;
}

How to use functions in C

//========================
//Program in C to display functions.c
//========================
extern int i;
int func1()
{
i++;
printf("\n i=%d",i);
return 0;
}
int func2()
{
i++;
printf("\n i=%d",i);
return 0;
}

How to use function pointer in C++

//========================
//Program in C to display "function pointer", funcptr.cpp
//========================
#include<iostream>
using namespace std;
typedef void (* FunctionPointer)(int x, int y);
void sumFunction(int i, int j)
{
cout<<i+j<<"\n";
}
void subtractionFun(int m, int n)
{
cout<<m-n<<"\n";
}
int main()
{
FunctionPointer ptr;
ptr=&sumFunction;
ptr(1,2);
ptr=&subtractionFun;
ptr(2,1);
return 0;
}
How to use function pointer in CPP/CPP
How to use function pointer in CPP/CPP
How to use function pointer in CPP/CPP
How to use function pointer in CPP/CPP

How to use friend function in C++

//Program in cpp to demonstrate friend1.cpp
#include<iostream>
using namespace std;
class base;
class anyClass
{
private:
friend class base;
anyClass()
{}
};
class base
{
public:
        virtual anyClass *createanyClass()
        {
        return new anyClass;
        }
};
class derived:public base
{
public:
        anyClass *createanyClass()
        {
        //return new anyClass;// base is frnd of another doesnot mean derive                                   //
class also will be friend  access private
        }
};
int main()
{
base b;
derived d;
b.createanyClass();
return 0;
}
How to use friend function in Cpp/C++
How to use friend function in Cpp/C++
How to use friend function in Cpp/C++
How to use friend function in Cpp/C++

How to use fork in C++

//========================
//Program in cpp for "fork",  fork2.c
//========================
#include<iostream>
using namespace std;
extern char **environ;
int main(int argc,char argv[])
{
cout<<"Hello World";
cout<<"First argument is \n "<<argv[1];
cout<<"Environment is \n "<<environ[0];
cout<<"Environment is \n "<<environ[1];
return 0;
}

How to use fork in C++

//========================
//Program in C++ to learn fork, fork1.cpp
//========================
#include<iostream>
using namespace std;
int main()
{
int pid;
pid=fork();
if(pid==0)
{
cout<<"Inside Child \n";
cout<<"Child pid="<<pid;
}
else
{
cout<<"Inside PARENT \n";
//cout<<"Parent's Pid="<<ppid;
}
return 0;
}

Program in C to learn "for loop", for3.c

//========================
//Program in C to learn  "for loop", for3.c
//========================
#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<10)
        {
        i++;
        continue;
        cout<<i<<"\t ";
        }
return 0;
}

Program in C++ to learn "for loop", for2.cpp

//========================
//Program in C++ to learn  "for loop", for2.cpp
//========================
#include<iostream>
using namespace std;
int main()
{
int i=0;
while(i<10)
        {
        i++;
        cout<<i<<"\t ";
        if(i==5)
        break;
        }
return 0;
}

Program in C to say hello, cdemo.c

//========================
//Program in C to say hello, cdemo.c
//========================
#include<stdio.h>
int main()
{
printf("\n Hello \n");
return 0;
}

//========================
//Program in c to display for loop, for1.cpp
//========================
#include<iostream>
using namespace std;
int main()
{
int i, max=10;
//for(i=1;i<=max;i++);
for(i=1;i<=max;i++)
        {
        cout<<i<<"\t ";
        }
return 0;
}

Program in C to set nth bit, bitset.c

//========================
//Program in C to set nth bit, bitset.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
        while(m > 0)
        {
                if(nn & m)
                {
                printf("1");
                }
                else
                {
                printf("0");
                }
        m>>=1;
        }
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to set:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d  \n",number,bits);
showbits(number);
number=number^(1<<(bits-1));
printf("\n Number now is: %d \n",number);
showbits(number);
printf("\n");
return 0;
}

Program in C to manipulate bits,bitmanip.c

//========================
//Program in C to manipulate bits,bitmanip.c
//========================
#include<stdio.h>
int main()
{
int n,b;
printf("\n Enter a number and bit to manipulate: \n");
scanf("%d%d",&n,&b);
printf("You entered %d and %d",n,b);
//Code to Set bth bit in number n
n=n|(1<<b);
//to toggle //n=n^(1<<b)
//to display bth bit// n&(1<<b)
//to clear bth bit // n& ~(1<<b)
printf("\n Entered Number after setting %d th bit is %d \n :",b,n);
return 0;
}

Program in C to check bit at nth position, bitcheckatn.c

//========================
//Program in C to check bit at nth position, bitcheckatn.c
//========================
#include<stdio.h>
int showbits(int nn)
{
unsigned int m;
m=1<<(sizeof(nn)*8-1);
        while(m > 0)
        {
                if(nn & m)
                {
                printf("1");
                }
                else
                {
                printf("0");
                }
        m>>=1;
        }
}
int main()
{
int number,bits;
printf("Enter a nomber and bits to see qt bit th position:\n");
scanf("%d%d",&number,&bits);
printf("\n You Entered: %d and %d  \n",number,bits);
showbits(number);
number=number&(1<<(bits));
printf("\n Now number is :%d and \n bit at %d  position is: \n",number,bits);
showbits(number);
printf("\n");
return 0;
}

Program in C to learn bitwise and operator

//========================
//Program in C to learn bitwise and operator
//========================
#include<stdio.h>
int main()
{
printf("\n Bit wise and demo \n");
unsigned int a=60;
unsigned int b=13;
unsigned int c=0;
c=a & b;
printf("Result to display is: %d \n" ,c);
return 0;
}

Program in C++ to learn array of pointer, arrofptr.cpp

//========================
//Program in C++ to learn  array of pointer, arrofptr.cpp
//========================
#include<iostream>
using namespace std;
#include<string.h>
#include<ctype.h>
int main()
{
cout<<"\n declaring array of pointer \n";
char * arrptr[10]={"internet", "net", "Music", "TV"};
// above arrptr is array of 10 pointers to character elements
char arrchar[15];
cout<<"Enter ur favorite time pass: \n";
cin>>arrchar;
int i;
for(i=0;i<4;i++)
{
        if(!strcmp(*arrptr[i], arrchar))
        {
        cout<<"Your fav time pass is available here"<<"\n";
        break;
        }
}
//cout<<"Adress of a is : \n"<<ptr1<<"\n";
//cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
return 0;
}

Program in C++ to access array of pointer to object, arrofptr2obj.cpp

//========================
//Program in C++ to access array of pointer to object, arrofptr2obj.cpp
//========================
//Array of pointer to object of class person
//pros: Dont need to know the number of objects to be created
//cons: Need to give a fixed size of array// solution is linked list
#include<iostream>
using namespace std;
class person
{
protected:
        char name[50];
public:
        void setname()
        {
        cout<<"\n Enter Name of the :";
        cin >> name;
        }
        void showname()
        {
        cout<<"\n Name of the person is: ";
        cout<<name;
        }
};
int main()
{
person* ptrpers[100];//array of pointer to object of class person
int n=0;
char option;
do{
        ptrpers[n]= new person;//creating 1 object run time
        ptrpers[n]->setname();
        n++;
        cout<<"Enter 1 more person's name (y/n)"<<"\n";
        cin>>option;
}while(option=='y');
//displaying the names
for(int j=0;j<n;j++)
        {
        cout<<"\n Person Number:"<<j+1;
        ptrpers[j]->showname();
        }
cout<<endl;
return 0;
}

Program in C to display array elements by pointer, arreltbyptr.c

//========================
//Program in C to display array elements by pointer, arreltbyptr.c
//========================
#include<stdio.h>
int main()
{
int i;
int arr[10]={10,11,12,14,17};
int *j;
j=&arr[0];
for(i=0;i<5;i++)
{
printf("\n array elt at position %d  is: %d",i,*j);
//printf("\n array elt at position %d  is: %d",i,arr[i]);
j++;
}
return 0;
}

Program in Cto display array elements using pointer, arrayinfunc.c

//========================
//Program in Cto display array elements using pointer, arrayinfunc.c
//========================
#include<stdio.h>
void display(int *, int);
int main()
{
int i;
int arr[5]={11,22,33,44,55};
display(&arr[0],5);
return 0;
}
void display(int *j, int n)
{
int i;
for(i=0;i<5;i++)
{
printf("\n Elements are:%d",*j);
j++;
}
}

Program in Cto display array elements

//========================
//Program in Cto display array elements
//========================
#include<stdio.h>
void display(int *);
int main()
{
        int i;
        int  marks[]={11,10,20,95,98,50,98};
        for(i=0;i<=6;i++)
                display(&marks[i]);
        //return 0;
}
void display(int *m)
{
        printf("\n %d",*m);
}

Program in C to display marks in an array

//========================
//Program in C to display marks in array
//========================
#include<stdio.h>
void display(int);
int main()
{
        int i;
        int  marks[]={11,10,20,95,98,50,98};
        for(i=0;i<=6;i++)
                display(marks[i]);
        //return 0;
}
void display(int m)
{
        printf("\n %d",m);
}

Program in C to calculate average of marks in an array

//Program in C to calculate average of marks in an array
#include<stdio.h>
int main()
{
        int i,sum=0,avg;
        int  marks[5];
        printf("\n Enter marks for 5 ppl:\n");
        for(i=0;i<5;i++)
        {
        scanf("%d",&marks[i]);
        }
        for(i=0;i<5;i++)
        {
        sum=sum+marks[i];
        }
        avg=sum/5;
        printf("\n svg is =%d",avg);
}


Simples Way to learn Data Structure in C++ CPP or C

Program in C to demonstrate 2-D array

//========================
//Program in C to demonstrate 2-D array
//========================
#include<stdio.h>
int main()
{
int arr[4][2]={ {00,01},{10,11},{20,21},{30,31} };
int i=0;
for(i=0;i<4;i++)
printf("\n %3d%3d",arr[i][0], arr[i][1]);
return 0;
}

Program in c to display third element of an array

//====================
//Program in c to display Third elt of an array
//====================
#include<stdio.h>
int main()
{
int arr[5];
arr[3]=100;
printf("\n Third element of array arr is %d \n ",arr[3]);
return 0;
}

20120105

Starting Up My First Technical Blog

Starting Up My First Technical Blog:

This blog will be updated on day to day basis.
Come and put your comments and your programs.
This blog will help for quick reference to C
and C++ Concepts online.