Pages

Tuesday, December 16, 2014

How to use Stack in C/C++ programs


This is an example program showing the syntax for stack declaration and manipulation on that stack using push and pop operations defined in the <stack> library. Other auxiliary functions of stack library like size() and top() are also used in the program. 

  • Push operation is used to insert an element in a stack
  • Pop operation is used to remove an element from a stack 

Property of Stack:

The element pushed in to the stack will always reside at the top of the stack.

The element popped up from stack will always be the top element from the stack.

Depiction of Stack push and pop Functions.









Try to run the below code in your C compiler and understand the  syntax and usage of the Stack Library 


#include<iostream>
#include<stack>

using namespace std;

int main()

{

stack <string> cards;    
\\ Declaration of Stack which 
\\can contain elements of "String" Type and
\\name of the stack assigned is "cards"

cards.push("kings of hearts");  \\ push an element
cards.push("kings of club");
cards.push("kings of spade");
cards.push("kings of diamond");

\\ size() function gives the
 \\ number of elements in the stack  
cout<<"there are"<<cards.size()<<"cards in the deck";

cards.push("ace of Spade");
cards.push("ace of hearts");

\\ top() function of stack library returns 
 \\ the element residing at the top position of the stack

string topcard=cards.top();  


cout<<"\ntop card is\t"<<topcard;

\\pop() function of stack library will 
\\simply delete the top element from the top
\\and the next element below it will be updated
\\ as new top of the stack

cards.pop();  

cout<<"\ntop card is\t"<<cards.top();
}

Sunday, January 12, 2014

Call by value Call by reference C++ Programming

Wednesday, September 14, 2011

Struct in C


A struct in C programming language is a structured (record) type that aggregates a fixed set of labelled objects, possibly of different types, into a single object.
A struct declaration consists of a list of fields, each of which can have any type. The total storage required for a struct object is the sum of the storage requirements of all the fields, plus any internal padding.

/* Objective: To prepere a menu which offer following facilities:
        -Register a student
        -delete record
        -search record and print its average
        -display list of student as per percentage obtained     */



        // Source Code  //


#include<stdio.h>            //including header files//
#include<string.h>
#include<conio.h>
struct info      
      //declaration of structure//
{
char name[20];
char branch[20];
int year;
int marks[5];
float percent;
char eno[20];
}student[50];

//Function Declaration//
void enter(int);
void del(int);
void search(int);
void display(int);
int main()
{
int i,n;
char choice;

printf("enter the no of students u want to register\t");
scanf("%d",&n);         // taking no of records to enter//
enter(n);               // calling enter function//
do
{
printf("enter\n 1:Delete \n 2:search \n 3:Display");
scanf("%d",&i);         //taking user's choice//
switch(i)
{
case 1:
del(n);                  // calling del function//
break;
case 2:
search(n);              // calling search function//
break;
case 3:
display(n);            // calling display function//
break;

default:
printf("invalid choice");
break;
}
printf("do u wanna continue press y or n");
scanf("%c",&choice);
}
while(choice=='y' || choice =='Y');
getch();
}
void enter(int n)      // function definition//
{
int i,j;

for(i=0;i<n;i++)
{
printf("Enter name,branch,year,enrolement no. of student");
scanf("%s %s %d %s",student[i].name,student[i]
.branch,&student[i].year,student[i].eno);
         //taking student info//
printf("enter marks of 5 subject");
for(j=0;j<5;j++)
scanf("%d",&student[i].marks[j]);  // taking student's marks//
}
}
void del(int n)    // function definition//
{
char sname[20];
int i,j,flag;
printf("enter the name of student whose records u wanna delete:");
scanf("%s",sname);
for(i=0;i<n;i++)
if(strcmp(student[i].name,sname)==0)      //checking//
flag=i;
for(j=flag;j<n-1;j++)
{
strcpy(student[j].name,student[j+1].name);  //swapping//
student[j].year=student[j+1].year;
strcpy(student[j].branch,student[j+1].branch);
for(i=0;i<5;i++)
student[j].marks[i]=student[j+1].marks[i];
strcpy(student[j].eno,student[j+1].eno);
}
printf("The records after deleting info are:");
display(n-1);               //function call for display function//
}
void search(int n)          //function definition//
{
int i, avg,j, flag;
char eno[20];
printf("enter eno of student whose marks are to be calculated");
scanf("%s",eno);
for(i=0;i<n;i++)

                if(strcmp(student[i].eno,eno)==0)
                flag=i;

                for(j=0;j<n;j++)
                                {
                                avg+=student[flag].marks[j];
                                }
printf("\nThe avg is %d",avg);
}

void display(int n)           // function definition//
{
int i,j,sum[50],pos,a[50],k;
float small;
struct info s1;
for(i=0;i<n;i++)
{
                 sum[i]=0;
                for(j=0;j<5;j++)
   
                sum[i]+=student[i].marks[j];
                student[i].percent=(float)sum[i]/5;
   

}
printf("The information of students are:");


for(i=0;i<n;i++)
{
    // sorting info in increasing order of percentage//
    for(j=0;j<n-i-1;j++)
    {
        if(student[j].percent>student[j+1].percent)
        {
        strcpy(s1.name,student[i].name);
        strcpy(s1.branch,student[i].branch);
        strcpy(s1.eno,student[i].eno);
        s1.year=student[i].year;
        for(k=0;k<5;k++)
        s1.marks[k]=student[i].marks[k];
        s1.percent=student[i].percent;
        strcpy(student[i].name,student[i+1].name);
        strcpy(student[i].branch,student[i+1].branch);
        strcpy(student[i].eno,student[i+1].eno);
        student[i].year=student[i+1].year;
        for(k=0;k<5;k++)
        student[i].marks[k]=student[i+1].marks[k];
        student[i].percent=student[i+1].percent;
        strcpy(student[i+1].name,s1.name) ;
        strcpy(student[i+1].branch,s1.branch);
        strcpy(student[i+1].eno,s1.eno)  ;
        student[i+1].year=s1.year;
        for(k=0;k<5;k++)
        student[i+1].marks[k]=s1.marks[k];
        student[i+1].percent=s1.percent;
        }
    }

    }
for(i=0;i<n;i++)
{
    printf("\nName: %s \n branch: %s \n enrolement no: %s",student[i].name,student[i].branch,student[i].eno)  ;
    printf("\nThe marks of 5 subjects are:");
    for(j=0;j<5;j++)
    printf("\n%d",student[i].marks[j]);
    printf("\npercentage %f",student[i].percent);
}
}

Array Data Type & Functions

 An array is a systematic arrangement of objects, usually in rows and columns.

Generally, a collection of data items that can be selected by indices computed at run-time, including:   
  • Array data structure, an arrangement of items at equally spaced addresses in computer memory
  • Array data type, used in a programming language to specify a variable that can be indexed
  • Associative array, an abstract data structure model that generalizes arrays to arbitrary indices


Objective: Given an array of N integers, write a program to perform following operations on the array based user's choice:
          -Insertion of elements at perticular location
          -Deletion of specific element
          -searching of perticular element
          -merging of list of elements
          -array traversing               
  

          //  Source Code   //

#include<stdio.h>     //including header files//
#include<conio.h>     //including header files//
void insertion(int[],int);         //function declaration//
void deletion(int[],int);          //function declaration//
void duplidel(int[],int);          //function declaration//
void search(int[],int);            //function declaration//
void merge(int[],int[],int);       //function declaration//


int main()
{
int a[20],b[20],n,e,i;

printf("enter the no of elements you wanna enter:");
scanf("%d",&n);                    //enter no of elements of array//
printf("enter array elements in assending order:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);                 //entering elements of array//
printf("select\n 1: insertion \n 2: deletion \n 3: deletion of duplicate \n 4:search \n 5:merge");
scanf("%d",&e);
switch (e)              //testing user's choice //
{
case 1:
insertion( a,n);        //calling insertion function//
break;
case 2:
deletion( a, n);       //calling deletion function//
break;
case 3:
duplidel( a, n);      //calling duplicate delete function//
break;

case 4:

search ( a, n);     //calling search function//
break;
case 5:
{
printf("enter the second array:\n");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
merge( a,b,n);
}
break;
default:
printf("invalid selection");
break;
}
getch();
}

void insertion (int a[20],int n)  //function definition//
{ int e,pos,t,i;                  //variable declaration//
printf("enter the element to be inserted ");
scanf("%d",&e);
printf("enter the position at which the above element is to be inserted");
scanf("%d",&pos);
for(i=pos-1;i<n+1;i++)
{
t=a[i];
a[i]=e;
e=t;
}

  printf("the array after insertion is:");
  for(i=0;i<n+1;i++)
  printf("%d\t",a[i]);
  getch();
  }
void deletion( int a[20], int n)  //function definition//
{  int i,e,t,pos;
printf("enter the element u wanna delete:");
scanf("%d",&e);
for(i=0;i<n;i++)
{
if( a[i]==e)
{
pos=i;
break;
}
}
for(i=pos;i<n;i++)
a[i]=a[i+1];
for(i=0;i<n-1;i++)
printf("%d\t",a[i]);
getch();
}
void search(int a[],int n)        //function definition//
 {
 int i, flag=0,no;
 printf("enter the no u wanna search:");
 scanf("%d",&no);
 for(i=0;i<n;i++)
 {
 if (a[i]==no);
        {
            printf("%d found at %d position",no,i+1);
            flag=1;
            break;
        }
}
if (flag==0)
printf("%d not found",no);
getch();
}
void merge(int a[],int b[],int n)     //function definition//
{  int c[20],i,j;
for(i=0;i<n;i++)
c[i]=a[i];
for(j=0;j<n;j++)
c[j+i]=b[j];
printf("The array after merging is:");
for(i=0;i<2*n;i++)

printf("%d\t",c[i]);
}
void duplidel(int a[], int n)
{
int i, j, k,temp,m=0,pos;
for(i=0;i<n-m;i++)
{
    temp = a[i];
    for(j=i+1;j<n-m;j++)
    {
        if(a[j]==temp)
        {
            m++;
            for(k=j;k<n-m;k++)
            a[k]=a[k+1];
        }
    }

}
printf("The array after deletion of duplicate element is:");
for(i=0;i<n-m;i++)
printf("%d\t",a[i]);
}

Sunday, September 4, 2011

Divide And Conquer Approach

  Divide-and-conquer is a top-down technique for designing algorithms that consists of dividing the problem into smaller sub-problems, we achieve the desired result by keep dividing the original problem in to many sub problems and solve those small (sub-problems)
recursively as its easy to calculate some small problem then to complete big one and finally combine all those sub problems to get the result of main problem.

divide-and-conquer paradigm consists of following major phases:

  • Breaking the problem into several sub-problems that are similar to the original problem but smaller in size,
  • Solve the sub-problem recursively (successively and independently), and then
  • Combine these solutions to sub-problems to create a solution to the original problem.




    Quick sort is the best example of the divide and conquer течникуе