Saturday, October 20, 2018

c program for quicksort

Write C++ program to store first year percentage of students in array. Sort array of floating
point numbers in ascending order using quick sort and display top five scores.

float arr[5]={5.1,4.2,3.3,2.4,1.5};

void quicksort(int start,int end)
{
    if(start<end)
    {
        int i=start+1;
        float pivote=arr[start];
        int j=end;
        float temp;
        while(i<j)
        {
            while(arr[i]<pivote&&i<5)
            i++;
            while(arr[j]>pivote)
            j--;
           
            if(i<j)
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
       
        temp=arr[j];
        arr[j]=arr[start];
        arr[start]=temp;
        quicksort(start,j-1);
        quicksort(j+1,end);
    }
}

int main()
{
    int i=0;
    int start=0;
    int end=4;
   
    quicksort(start,end);
   
    printf("\nThe top 5 scores are:");
    for(i=0;i<5;i++)
    {
        printf("\n%f",arr[i]);
    }
   
    return 0;
}

c program for queue

/*Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. 
Write C program for simulating job queue.
Write functions to add job and delete job from queue.
*/

#include<stdio.h>
struct queue
{
        int rear,front;
        int a[10];
};

struct queue q;

void init()
{
        q.rear=0;
        q.front=-1;
}

int isfull()
{
        if(q.rear>=10)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

int isempty()
{
        if((q.rear-q.front)<=1)
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void insert(int x)
{
        q.a[q.rear]=x;
        q.rear++;
}

int del()
{
        q.front++;
        return q.a[q.front];
}

void list()
{
        int i;
        for(i=q.front+1;i<q.rear;i++)
        {
                printf("\n%d",q.a[i]);
        }
}

void main()
{
        int x;
        int ch;
       
        init();
       
        do{
                printf("\n1.Insert job ID\n2.delete job ID\n3.list\n0.exit");
                printf("\nEnter choice:");
                scanf("%d",&ch);
               
                switch(ch)
                {
                        case 1:
                                if(isfull()==1)
                                {
                                        printf("\nqueue is overflow");
                                }
                                else
                                {
                                        printf("\nEnter job ID:");
                                        scanf("%d",&x);
                                       
                                        insert(x);
                                }
                                break;
                               
                        case 2:
                                if(isempty()==1)
                                {
                                        printf("\nQueue is empty");
                                }
                                else
                                {
                                        printf("\nDeleted %d",del());
                                }
                                break;
                               
                        case 3:
                                printf("\nQueue");
                                list();
                                break;
                               
                        case 0:
                                printf("\nExit");
                                break;
                               
                        default:
                                printf("\nInvalid choice");
                }
        }while(ch!=0);
       
       
}

/*
output-

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:120

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:320

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:520

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:1

Enter job ID:650

1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:3

Queue
120
320
520
650
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:2

Deleted 120
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:3

Queue
320
520
650
1.Insert job ID
2.delete job ID
3.list
0.exit
Enter choice:0

*/

C program for string operations- copy, concatenate, check substring, equal, reverse and length

/*
Write C++ program for string operations- copy, concatenate, check substring, equal, reverse and length
*/
 
#include<stdio.h>              
#include<stdlib.h>             
int main()
{
  int n,i,j,flag,ch;
  char str1[50],str2[50],str3[50],str4[50];
 
 
  printf("Enter string 1: ");
  scanf("%s",str1);
  printf("Enter string 2: ");
  scanf("%s",str2);
 
 
  printf("\nstring 1 is: %s",str1);
  printf("\nstring 2 is: %s",str2);
 
  while(1)
  {
   printf("\nEnter choice\n");
   printf("1.copy\n2.concatenate\n3.check substring\n4.equal\n5.reverse\n6.length of string\n7.Exit\n");
   scanf("%d",&ch);
  
   switch(ch)
   {
     case 1:
                printf("this is copy case");   
                i=0;
                while(str1[i]!='\0')
                {
                 str3[i]=str1[i];
                 i++;
                 }
                 str3[i]='\0';
                 printf("\nafter copying string3 is : %s",str3);
                 break;
     case 2:
                 printf("\nthis is concanetation");
                 i=0;j=0;
                 while(str3[i]!='\0')
                 i++;
                 while(str2[j]!='\0')
                 {
                  str3[i]=str2[j];
                  i++;j++;
                 }
                str3[i]='\0';
                printf("\nafter concanating string1 & string2");
                printf("\nstring 3 is: %s",str3);
                break;  
     case 3:
                 printf("\nthis is substring");
                 i=0;j=0;flag=0;
                 while(str1[i]!='\0')
                 {
                  if(str1[i]==str2[j])
                 {
                  j++;
                  if(str2[j]=='\0')
                  flag=1;
                 }
                  i++;
                 }
                if(flag==1)
                printf("\nstring2 is found in string1!");
                else
                printf("\nstring2 is not found in string1!");
                break;
     case 4:
                printf("\nthis is equal case");
                i=0;flag=0;
                while(str1[i]!='\0')
                {
                 if(str1[i]!=str2[i])
                {
                 flag=1;
                 break;
                 }
                 i++;
                 }
                 if(flag==1)
                 printf("\nstring1 & string2 are not equal");
                 else
                 printf("\nstring1 & string2 are equal");
                 break;
               
     case 5:
                 printf("\nthis is reverse case");
                 i=0;j=0;
                 while(str1[i]!='\0')
                 i++;
                 i=i-1;
                 while(i>=0)
                 {
                   str4[j]=str1[i];
                   i--;
                   j++;
                 }
                str4[j]='\0';
                printf("\nAfter reversing string1 into string4");
                printf("\nstring4: %s",str4);
                break;
    
     case 6:
                printf("\nthis is length case");
                i=0;
                while(str1[i]!='\0')
                i++;
                printf("\nthe length of string1=%d",i);
                j=0;
                while(str2[j]!='\0')
                j++;
                printf("\nthe length of string2=%d",j);
                break;
     case 7:
                exit(0);
               
     default:
                printf("INVALID CHOICE");

               
     }
  }
  return 0;
 
}

Saddle point

/*
An m x n matrix is said to have a saddle point if some entry a[i][j] is the smallest value in
row i and the largest value in j. Write C/ C++ function that determines the location of a
saddle point if one exists.

*/

#include<stdio.h>
int main()
{
    int a[10][10],i,j,k,n,min,max,col,m;
   
    printf("enter order m,n of mxn matrix : ");
    scanf("%d %d",&m,&n);
    printf("enter elements row-wise\n");
   
    for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        scanf("%d",&a[i][j]);
                }
        }
       
        printf("\nMatrix is:\n");
        for(i=0;i<m;i++)
        {
                for(j=0;j<n;j++)
                {
                        printf("%d",a[i][j]);
                        printf("\t");
                }
                   printf("\n");
        }
       
       
    for(i=0;i<m;i++)
        {
                min=a[i][0];
                for(j=0;j<n;j++)
                {
                        if(a[i][j]<=min)
                        {
                                min=a[i][j];
                                col=j;
                        }
                }
                max=a[0][col];
                for(k=0;k<m;k++)
                {
                        if(a[k][col]>=max)
                        {
                                max=a[k][col];
                        }
                }
               
                if(max==min)
                {
            printf("\nsaddle point is %d\n",max);
                }          
        }
return 0;
}
/*
output-

enter order m,n of mxn matrix : 3 3
enter elements row-wise
1 2 3
4 5 6
7 8 9

Matrix is:
1    2    3   
4    5    6   
7    8    9   

saddle point is 7

*/

C program to store marks scored for first test of subject 'Data Structures and Algorithms' for N students. Compute I. The average score of class ii. Highest score and lowest score of class iii. Marks scored by most of the students iv. list of students who were absent for the test

/*
Write C program to store marks scored for first test of subject 'Data Structures and
Algorithms' for N students. Compute
I. The average score of class
ii. Highest score and lowest score of class
iii. Marks scored by most of the students
iv. list of students who were absent for the test

*/

#include<stdio.h>
struct student
{
  int marks;
  int attd;
};
int main()
{
        int n,i;
        int ch;

        printf("\nEnter total number of students:");
        scanf("%d",&n);
       
        struct student se[n];
       
        for(i=1;i<=n;i++)
        {  
            printf("\nroll no. %d \n",i);
            printf("attendence: 1.present 2.absent");
            printf("\nEnter your choice:");
            scanf("%d",&ch);
           
            if(ch==1)
            {
                se[i].attd=1;
                printf("\nEnter DSA marks: ");
                scanf("%d",&se[i].marks);
            }
            else
            {
                se[i].attd=0;
                se[i].marks=0;
            }
           
         }
        
         for(i=1;i<=n;i++)
         {
             printf("\nRoll number is: %d\tattendence is: %d\tmarks are:%d",i,se[i].attd,se[i].marks);
         }
        
         //The average score of class
         int sum=0;
         float avg=0;
         int cnt=0;
        
         for(i=1;i<=n;i++)
         {
             if(se[i].attd==1)
             {
                sum=sum+se[i].marks;
                cnt++;
             }
         }
         avg=sum/cnt;
         printf("\n\nThe average score of class is : %f",avg);
        
         //Highest score and lowest score of class
         int max=0;
         int min=99;
         for(i=1;i<=n;i++)
         {
              if(se[i].attd==1)
              {
                 if(min>se[i].marks)
                 {
                    min=se[i].marks;
                 }
                 if(max<se[i].marks)
                 {
                    max=se[i].marks;
                 }
              }
         }
        
         printf("\n\nHighest score is: %d and lowest score is: %d",max,min);
           
       //Marks scored by most of the students
         int j;
         int m=0;
         int val;
         cnt=0;
         for(i=1;i<=n;i++)
         {
                  cnt=0;
                  for(j=1;j<=n;j++)
                  {
                      if(se[i].marks==se[j].marks)
                      cnt++;
                  }
                 
                  if(m<cnt)
                  {
                       m=cnt;
                       val=se[i].marks;
                  }
          }
         
          printf("\n\nMarks scored by most of the students is: %d and no. of student is: %d",val,m);
         
          //list of students who were absent for the test
          cnt=0;
          printf("\n\nlist of students who were absent for the test:");
          for(i=1;i<=n;i++)
          {
              if(se[i].attd==0)
              {
              printf("\nRoll number %d is absent",i);
              cnt++;
              }
          }
          printf("\ntotal number of absent students is: %d\n",cnt);
         
         
return 0;
}
/*

output-

Enter total number of students:5

roll no. 1
attendence: 1.present 2.absent
Enter your choice:1

Enter DSA marks: 60

roll no. 2
attendence: 1.present 2.absent
Enter your choice:2

roll no. 3
attendence: 1.present 2.absent
Enter your choice:1

Enter DSA marks: 40

roll no. 4
attendence: 1.present 2.absent
Enter your choice:2

roll no. 5
attendence: 1.present 2.absent
Enter your choice:1

Enter DSA marks: 80

Roll number is: 1    attendence is: 1    marks are:60
Roll number is: 2    attendence is: 0    marks are:0
Roll number is: 3    attendence is: 1    marks are:40
Roll number is: 4    attendence is: 0    marks are:0
Roll number is: 5    attendence is: 1    marks are:80

The average score of class is : 60.000000

Highest score is: 80 and lowest score is: 40

Marks scored by most of the students is: 0 and no. of student is: 2

list of students who were absent for the test:
Roll number 2 is absent
Roll number 4 is absent
total number of absent students is: 2

*/

Friday, October 19, 2018

C program using stack to check whether given expression is well parenthesized or not.

/*
In any language program mostly syntax error occurs due to unbalancing delimiter such as
(),{},[]. Write C program using stack to check whether given expression is well
parenthesized or not.
*/
#include<stdio.h>
# define size 10
int stack[size];
int top=-1;
char ans;
char pop()
{
    char var;
    if(top==-1)
    {
        printf("\nUnderflow");
        return 0;
    }
    else
    {
        var=stack[top];
        top--;
        return var;
    }
}
    void push(char var)
    {
        if(top+1==size)
        printf("\nOverflow");
        else
        {
            top++;
            stack[top]=var;
        }
    }
    char getval(char input)
    {
        switch(input)
        {
            case '[':
                    return']';
               case '(':
                    return')';
            case '{':
                    return'}';
        }
    }
    int main()
    {
        int i=0;
        char input[20];
        printf("\nEnter the operation: ");
        scanf("%s",input);
        for(i=0;input[i]!='\0';i++)
        {
            if(input[i]=='['||input[i]=='('||input[i]=='{')
            {
                push(input[i]);
            }
            if(input[i]==']'||input[i]==')'||input[i]=='}')
            {
                ans=pop();
           
                if(getval(ans)!=input[i])
                {
                    printf("\nFail\n");
                    return 0;
                }
            }
        }
        if(top==-1)
        {
            printf("\nPass\n");
        }
        else
        {
            printf("\nFail\n");
        }
            return 0;
}
/*
output-
Enter the operation: (A+B)-(A*B}   

Fail
*/


C program to merge to sorted linked list

/*
Let x = (x 1 ,x 2 , ... , x n ) and y = (y 1 , y 2 ,.... , y m ) be two doubly linked lists. Assume that in
each linked list, the nodes are in non-decreasing order of their data-field values. Write
C/C++ program to merge the two lists to obtain a new linked list z in which the nodes are
also in this order. Following the merge, x and y should represent empty lists because each
node initially in x or y is now in z. No additional nodes may be used
*/

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node * next;
    struct node * prv;
};

int main()
{
    int l1,l2,i;
   
    printf("\nEnter total no. of node in list 1\n");
    scanf("%d",&l1);
    printf("\nEnter total no. of node in list 2\n");
    scanf("%d",&l2);

    struct node *list1=NULL;
    i=0;
    while(i<l1)
{
    if(list1==NULL)
  {
    list1=(struct node *) malloc(sizeof(struct node));
    printf("\nEnter the no. in list 1\n");
    scanf("%d",&list1->data);

    list1->next=NULL;
    list1->prv=NULL;
  }
     else
  {
    struct node *temp;
    temp=list1;
    while(temp->next!=NULL)
    temp=temp->next;
    struct node *newnode=(struct node *) malloc(sizeof(struct node));
    printf("\nEnter the no. in list 1\n");
    scanf("%d",&newnode->data);

         temp->next=newnode;
         newnode->next=NULL;
         newnode->prv=temp;
  }
 i++;
}

    struct node *list2=NULL;
    i=0;
    while(i<l2)
{
     if(list2==NULL)
  {
      list2=(struct node *) malloc(sizeof(struct node));
      printf("\nEnter the no. in list 2\n");
      scanf("%d",&list2->data);
      list2->next=NULL;
      list2->prv=NULL;
  }
      else
  {
      struct node *temp;
      temp=list2;
      while(temp->next!=NULL)
      temp=temp->next;
      struct node *newnode=(struct node *) malloc(sizeof(struct node));
      printf("\nEnter the no .in list 2\n");
      scanf("%d",&newnode->data);
      temp->next=newnode;
      newnode->next=NULL;
      newnode->prv=temp;
  }
 i++;
}
    printf("\nList 1\n");
    struct node *temp;
    temp=list1;
    while(temp!=NULL)
    {
    printf("\t%d",temp->data);
      temp=temp->next;
    }
    printf("\nList 2\n");
    temp=list2;
    while(temp!=NULL){
      printf("\t%d",temp->data);
      temp=temp->next;
    }

struct node *z=NULL;

while(list1!=NULL && list2!=NULL)
{
      if(list1->data < list2->data)
    {
      if(z==NULL)
      {
        z=list1;
        list1=list1->next;
        z->next=NULL;
        z->prv=NULL;
      }
      else
      {
        struct node *tt;
          tt=z;
          while(tt->next!=NULL)
          tt=tt->next;
          tt->next=list1;
          list1->prv=tt;
          list1=list1->next;
          tt->next->next=NULL;
      }
    }

else
{
    if(z==NULL)
      {
        z=list2;
        list2=list2->next;
        z->next=NULL;
        z->prv=NULL;
      }
      else
      {
       struct node *tt;
         tt=z;
         while(tt->next!=NULL)
         tt=tt->next;
         tt->next=list2;
         list2->prv=tt;
         list2=list2->next;
         tt->next->next=NULL;
      }
   }
}

while(list1!=NULL)
{
     if(z==NULL)
      {
        z=list1;
        list1=list1->next;
        z->prv=NULL;
      }
      else
      {
       struct node *tt;
        tt=z;
        while(tt->next!=NULL)
        tt=tt->next;
        tt->next=list1;
        list1->prv=tt;
        list1=list1->next;
        tt->next->next=NULL;
    }
}
while(list2!=NULL)
{
     if(z==NULL)
      {
        z=list2;
        list2=list2->next;
        z->prv=NULL;
      }
      else
      {
       struct node *tt;
         tt=z;
         while(tt->next!=NULL)
         tt=tt->next;
         tt->next=list2;
         list2->prv=tt;
         list2=list2->next;
         tt->next->next=NULL;
      }
}

    printf("\nmerged list\n");
    temp=z;
    while(temp!=NULL)
    {
      printf("\t%d",temp->data);
     temp=temp->next;
    }
return 0;
}
/*
output:


Enter total no. of node in list 1
4

Enter total no. of node in list 2
5

Enter the no. in list 1
1

Enter the no. in list 1
2

Enter the no. in list 1
3

Enter the no. in list 1
4

Enter the no. in list 2
5

Enter the no .in list 2
6

Enter the no .in list 2
7

Enter the no .in list 2
8

Enter the no .in list 2
9

List 1
    1    2    3    4
List 2
    5    6    7    8    9
merged list
    1    2    3    4    5    6    7    8    9
*/