Search This Blog

Wednesday 12 October 2016

Recursive descent parsing

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char in[50];
int j=0,f=0;
void PE1();
void PT();
void PT1();
void PF();
void match(char c);
void PE()
{
printf("E->TE1\n");
//printf("Enter PE\n");
PT();
PE1();
}
void PE1()
{
//printf("Enter PE1\n");
char c=in[j];
if(c=='+')
{
printf("E1->+TE1\n");
match(c);
PT();
PE1();
}
else
printf("E1->NULL\n");
}
void PT()
{
//printf("Enter PT\n");
printf("T->FT1\n");
PF();
PT1();
}
void PT1()
{
//printf("Enter PT1\n");
char c=in[j];
if(c=='*')
{
printf("T1->*FT1\n");
match(c);
PF();
PT1();
}
else
printf("T1->NULL\n");
}
void PF()
{
//printf("Enter PF\n");
char c=in[j];
if(c=='(')
{
printf("F->(E)\n");
match('(');
PE();
match(')');
}
else if((c>='a' && c<='z')|| (c>='A' && c<='Z'))
{
printf("F->id\n");
match(c);
}
else
{
printf("Syntax Error\n");
exit(0);
}
}
void match(char c)
{
if(j<=strlen(in))
{
if(in[j]==c)
{
printf("matched %c\n",c);
j++;
}
else
printf("Error\n");
}
}
int main()
{
scanf("%s",in);
PE();
if(in[j]=='\0')
printf("correct\n");
else
printf("Syntax error\nUnidentified symbol %c\n",in[j]);
return 0;

Predictive Parsing Of the grammar

/*This Program implements the Predictive Parsing Of the grammar
E->E+T/T
F->F*T/F
F->id(Identifier)*/
#include<string.h>
#include<stdio.h>
char a[10];
int top=-1,i;
void error(){
printf("Syntax Error");
}
void push(char k[]) //Pushes The Set Of Characters on to the Stack
{
  for(i=0;k[i]!='\0';i++)
  {
    if(top<9)
    a[++top]=k[i];
  }
}
char TOS()        //Returns TOP of the Stack
{
  return a[top];
}
void pop()       //Pops 1 element from the Stack
{
  if(top>=0)
    a[top--]='\0';
}
void display()  //Displays Elements Of Stack
{
  for(i=0;i<=top;i++)
    printf("%c",a[i]);
}
void display1(char p[],int m) //Displays The Present Input String
{
  int l;
  printf("\t");
  for(l=m;p[l]!='\0';l++)
    printf("%c",p[l]);
}
char* stack(){
return a;
}
int main()
{
 char ip[20],r[20],st,an;
 int ir,ic,j=0,k;
 char t[5][6][10]=  {"$","$","TH","$","TH","$",
      "+TH","$","e","e","$","e",
      "$","$","FU","$","FU","$",
      "e","*FU","e","e","$","e",
      "$","$","(E)","$","i","$"};
 printf("\nEnter any String(Append with $)");
 gets(ip);
 printf("Stack\tInput\tOutput\n");
 push("$E");
 display();
 printf("\t%s\n",ip);
 for(j=0;ip[j]!='\0';)
 {
  if(TOS()==an)
  {
   pop();
   display();
   display1(ip,j+1);
   printf("\tPOP\n");
   j++;
  }
  an=ip[j];
  st=TOS();
  if(st=='E')ir=0;
  else if(st=='H')ir=1;
  else if(st=='T')ir=2;
  else if(st=='U')ir=3;
  else if(st=='F')ir=4;
  else 
  {
   error();
   break;
  }
  if(an=='+')ic=0;
  else if(an=='*')ic=1;
  else if(an=='(')ic=2;
  else if(an==')')ic=3;
  else if((an>='a'&&an<='z')||(an>='A'&&an<='Z')){ic=4;an='i';}
  else if(an=='$')ic=5;
  strcpy(r,strrev(t[ir][ic]));
  strrev(t[ir][ic]);
  pop();
  push(r);
  if(TOS()=='e')
  {
   pop();
   display();
   display1(ip,j);
   printf("\t%c->%c\n",st,238);
  }
  else{
   display();
   display1(ip,j);
   printf("\t%c->%s\n",st,t[ir][ic]);
  }
  if(TOS()=='$'&& an=='$')
   break;
  if(TOS()=='$')
  {
   error();
   break;
  }
 }
 k=strcmp(stack(),"$");
 if(k==0 && j==strlen(ip)-1)
  printf("\nGiven String is accepted");
 else
  printf("\nGiven String is not accepted");
 return 0;
}

Friday 7 October 2016

making change using dynamic

#include<stdio.h>
int main()
{
    int n,C,d[10],i,j,V[15][15],b[15][15];
    printf("enter maximum change");
    scanf("%d",&C);
    printf("enter no. of elements");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&d[i]);
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=C;j++)
        {
            if(i==0 || j==0)
            {
                //printf("A\n");
                if(i==0)
                    V[i][j]=65535;
                else
                    V[i][j]=0;
            }
            else if(j<d[i-1])
            {
                //printf("B\n");
                V[i][j]=V[i-1][j];
                b[i][j]=j;
            }
            else
            {
                //printf("C\n");
                if(V[i-1][j]<((V[i][j-d[i-1]])+1))
                {
                    V[i][j]=V[i-1][j];
                    b[i][j]=j;
                }
                else
                {
                    V[i][j]=V[i][j-d[i-1]]+1;
                    b[i][j]=j-d[i-1];
                }
            }
            //printf("%d %d %d\n",i,j,V[i][j]);
        }
    }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=C;j++)
            {
                printf("%d ",V[i][j]);
            }
            printf("\n");
        }
        printf("\nminimum coins used is %d\n",V[n][C]);
        i=n;
        j=C;
        while(j!=0)
        {
            if(V[i][j]==V[i-1][j])
                i--;
            else
            {
                printf("%d\t",d[i-1]);
                j=b[i][j];
              
            }
        }
    return 0;
}

matrix chain multiplication using dynamic

#include<stdio.h>
int p[10],m[10][10],s[10][10];
void matrix_chain_order(int n)
{
    int i,j,k,l,q;
//    n=p.length-1;
    for(i=1;i<n;i++)
    m[i][i]=0;
    for(l=2;l<=n;l++)
    {
        for(i=1;i<=n-l+1;i++)
        {
            j=i+l-1;
            m[i][j]=99999;
            //printf("%d\t%d\t%d\n",i,l,j);
            for(k=i;k<=j-1;k++)
            {
                q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                if(q<m[i][j])
                {
                    m[i][j]=q;
                    s[i][j]=k;
                    //printf("%d\t%d\t%d\t%d\n",i,j,k,m[i][j]);
                }
            }
        }
      
    }
    for(i=0;i<n;i++)
    {
        for(j=n;j>i;j--)
        {
            printf("%d %d\t",m[i][j],s[i][j]);
        }
        printf("\n");
    }
  
    printf("\n\n\n");
}

void PRINT_OPTIMAL_PARENS(i,j)
{
    if(i == j)
     printf("A%d",i);
    else
    {
        printf("(");
    PRINT_OPTIMAL_PARENS(i,s[i][j]);
    PRINT_OPTIMAL_PARENS(s[i][j]+1,j);
    printf(")");
    }

}
int main()
{
    int i,n;
    printf("enter no. of matrices");
    scanf("%d",&n);
    printf("enter sizes of matrix");
    for(i=0;i<=n;i++)
    scanf("%d",&p[i]);
    matrix_chain_order(n);
    PRINT_OPTIMAL_PARENS(1,6);
    return 0;
}

knapsack using dynamic

 //knapsack using dynamic
#include<stdio.h>
int main()
{
    int n,W,w[10],v[10],i,j,V[15][15];
    printf("enter capacity of knapsack");
    scanf("%d",&W);
    printf("enter no. of elements");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d %d",&w[i],&v[i]);
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=W;j++)
        {
            if(i==0 || j==0)
            V[i][j]=0;
            else if((j-w[i-1])<0)
            V[i][j]=V[i-1][j] ;
            else
            {
                if(V[i-1][j]<V[i-1][j-w[i-1]]+v[i-1])
                V[i][j]=V[i-1][j-w[i-1]]+v[i-1];
                else
                V[i][j]=V[i-1][j];          
            }
        }
    }
        for(i=0;i<=n;i++)
        {
            for(j=0;j<=W;j++)
            {
                printf("%d ",V[i][j]);
            }
            printf("\n");
        }
        printf("\nmax profit earned is %d\n",V[n][W]);
    return 0;
}

recursive_descent_parsing

#include<stdio.h>
#include<string.h>
char in[50];
int j=0,f=0;
void PE1();
void PT();
void PT1();
void PF();
void match(char c);
void PE()
{
//printf("Enter PE\n");
PT();
PE1();
printf("E->TE1\n");
}
void PE1()
{
//printf("Enter PE1\n");
char c=in[j];
if(c=='+')
{
match('+');
PT();
PE1();
}
else
printf("E1->NULL\n");
printf("E1->+TE1 | ^\n");
}
void PT()
{
//printf("Enter PT\n");
PF();
PT1();
printf("T->FT1\n");
}
void PT1()
{
//printf("Enter PT1\n");
int f=0;
char c=in[j];
if(c=='*')
{
match('*');
PF();
PT1();
}
else
printf("T1->NULL\n");
printf("T1->*FT1 | ^\n");
}
void PF()
{
//printf("Enter PF\n");
char c=in[j];
if(c=='(')
{
match('(');
PE();
match(')');
}
else if((c>='a' && c<='z')|| (c>='A' && c<='Z'))
match(c);
printf("F->(E)|i\n");
}
void match(char c)
{
if(j<=strlen(in))
{
if(in[j]==c)
{
printf("matched %c\n",c);
j++;
}
else
printf("Error\n");
}
}
int main()
{
scanf("%s",in);
PE();
if(in[j]=='\0')
printf("correct\n");
else
printf("Syntax error\nUnidentified symbol %c\n",in[j]);
return 0;
}


- Jinay Shah

Sunday 7 August 2016

Counting Sort

#include <stdio.h>
#include <stdlib.h>
void count(int A[],int k,int n)
{
    int i,j;
    int B[10],C[15];
    for(i=0;i<=k;i++)
    {
        C[i]=0;
    }
    for(j=1;j<=n;j++)
    {
        C[A[j]]= C[A[j]] +1;
    }

    for(i=1;i<=k;i++)
        C[i]= C[i]+C[i-1];

    for(j=n;j>=1;j--)
    {
        B[C[A[j]]] = A[j];
        C[A[j]] = C[A[j]] -1;
    }

    printf("Sorted array is:\n");
    for(i=1;i<=n;i++)
    {
        printf("%d ",B[i]);
    }

}

int main()
{
    int n,A[15],k=0,i;
    printf("Enter n\n");
    scanf("%d",&n);
    printf("\nEnter ele:");
    for(i=1;i<=n;i++)
    {
        scanf("%d",&A[i]);
        if(A[i]>k)
            k=A[i];
    }
    count(A,k,n);

    return 0;
}

Friday 5 August 2016

Merge Sort

#include<stdio.h>
#include<math.h>
#include<sys/time.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

long int n=100000,a[100000];

void merge(long int begin,long int mid, long int end,long int a[])
{
long int i=begin,j=mid+1,temp[end],cnt=0;
while(cnt<(end-begin+1))
{
if(i==mid+1)
{
temp[cnt++]=a[j];
j++;
}
else if(j==end+1)
{
temp[cnt++]=a[i];
i++;
}
else
{
if(a[i]<a[j])
{
temp[cnt++]=a[i];
i++;
}
else
{
temp[cnt++]=a[j];
j++;
}
}
}
for(i=begin;i<=end;i++)
a[i]=temp[i-begin];
}

void divide(long int a,long int b,long int y[])
{
long int mid=(b-a)/2+a;
if((b-a)>1)
{
divide(a,mid,y);
divide(mid+1,b,y);
}
merge(a,mid,b,y);
}

int main()
{
struct timeval  tv1, tv2;
 
long int mid;
for(long int i=0;i<n;i++)
a[i]=rand();

gettimeofday(&tv1, NULL);

mid=(n-1)/2;
divide(0,mid,a);
divide(mid+1,n-1,a);
merge(0,mid,n-1,a);

gettimeofday(&tv2, NULL);
printf("Time taken in execution = %f micro-seconds\n",
    (double) (tv2.tv_usec - tv1.tv_usec));
    return 0;
}

Insertion Sort

#include<stdio.h>
#include<math.h>
#include<sys/time.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

int main()
{
long timedif;
struct timeval tpstart;
struct timeval tpend;
struct timeval now;
int rc;

now.tv_sec=000000000;
now.tv_usec=000000;

rc=settimeofday(&now, NULL);

if(rc==0)
{
printf("settimeofday() failed.\n");
}
else
{
printf("\nsettimeofday() Succesful\n");
printf("\nTime Set sec=%ld Msec=%ld\n",now.tv_sec,now.tv_usec);
}

gettimeofday(&tpstart, NULL);

long int n=100000;
long int a[n];

for(long int i=0;i<n;i++)
{
long int temp=random(),flag=1;
for(long int j=0;j<i;j++)
{
if(temp<a[j])
{
for(long int k=i;k>=j;k--)
a[k]=a[k-1];
a[j]=temp;
flag=0;
break;
}
}
if(flag==1)
a[i]=temp;
}

gettimeofday(&tpend, NULL);
timedif = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;

printf("\nTime difference is:%ld\n",timedif);
}


Selection sort

#include<stdio.h>
#include<math.h>
#include<sys/time.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

long int n=100000,a[100000];

int main()
{
struct timeval  tv1, tv2;
   
for(long int i=0;i<n;i++)
a[i]=rand();

gettimeofday(&tv1, NULL);

for(long int i=0;i<n;i++)
{
long long int min=a[i],ind=i;
for(long int j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
ind=j;
}
}
a[ind]=a[i];
a[i]=min;
//cout << a[i] << endl;
}

gettimeofday(&tv2,NULL);
printf("Time taken in execution = %f micro-seconds\n",
    (double) (tv2.tv_usec - tv1.tv_usec));
    return 0;
}


Bubble Sort

#include<stdio.h>
#include<math.h>
#include<sys/time.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

long int n=100000,a[100000];

int main()
{
     struct timeval tv1, tv2;

     for(long int i=0;i<n;i++)
     a[i]=rand();

     gettimeofday(&tv1, NULL);

     for(long int i=0;i<n;i++)
     {
           for(long int j=i;j<n;j++)
           {
                 if(a[i]>a[j])
                 {
                        long int temp;
                        temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                  }
           }
     }

     gettimeofday(&tv2, NULL);
     printf("Time taken in execution = %f micro-seconds\n",(double) (tv2.tv_usec - tv1.tv_usec));
     return 0;
}

Thursday 21 July 2016

Write an interactive program to print a diamond shape.

import java.util.Scanner;

class HelloWorld
{

    public static void main(String[] args)
 {
 
        int c=0,v=0;
int s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter Number :");
        s = in.nextInt();
     
        for(int i = 1 ;i<=s;i++)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
        for(int i = s-1 ;i>0;i--)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
    }
}


output:
Enter Number:
3
       *
   *      *
 *    *    *
   *      *
       *

Write an interactive program to print a string entrered in a pyramid form.

import java.util.Scanner;

class  Pyramid
{

    public static void main(String[] args)
{
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
     
        for(int i = 0 ;i<s.length();i++)
        {
            for(int j=s.length()/2 + 1;j>=i;j--)
System.out.print(" ");
for(int k=0;k<=i;k++)
{
char ch = s.charAt(k);
System.out.print(ch+" ");
}
System.out.println();
        }
    }
}

output:
Enter String:
SCET
        S
     S   C
  S    C    E
S   C   E   T

Create a class which ask the user to enter a sentence, and it should display count of each vowel type in the sentence. The program should continue till user enters a word “quit”. Display the total count of each vowel for all sentences.

import java.util.Scanner;

public class Vowel
 {

    public static void main(String[] args)
    {
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        s = s.toLowerCase();
        for(int i = 0 ;i<s.length();i++)
        {
            char ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                                        ch == 'o' || ch == 'u'){
                                v++;
            }
            else if(ch == ' '){
               
            }
            else if (Character.isDigit(s.charAt(i)))
            {
             
            }
         
            else
                c++;
        }
        System.out.println("Vowels are "+v+" and consonants are "+c);
    }
}


output:
Enter string:
Hello World
Vowels are 3 and consonants are 7.

Wednesday 6 July 2016

Write a program to find that given number or string is palindrome or not.

Unit 2

Prac 5


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        String s,s2="";
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        for(int i = s.length()-1;i>=0;i--)
        {
            s2 = s2 + s.charAt(i);
        }
        if(s.equals(s2))
            System.out.println("palindrome");
        else
            System.out.println("not palindrome");
    }
}


output:

Enter String :
gajjar
not palindrome

Enter String :
12321
palindrome

Write a program to count the number of words that start with capital letters.

Unit - 2

Practical 4


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        int ct=0;
        for(String str: s.split(" ")) {
        if(str.charAt(0)>=65 && str.charAt(0)<=90)
        {
            ct++;
        }
    }
    System.out.println("total  number of words start with capital letters are :"+ct);
    }
}


output:

Enter String :
Karan Gajjar kanu
total  number of words start with capital letters are :2

Write a program to accept a line and check how many consonants and vowels are there in line.

Unit - 2

Practical 3


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        s = s.toLowerCase();
     
        for(int i = 0 ;i<s.length();i++)
        {
            char ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                                        ch == 'o' || ch == 'u'){
                                v++;
            }
            else if(ch == ' '){
               
            }
            else if (Character.isDigit(s.charAt(i)))
            {
             
            }
         
            else
                c++;
        }
        System.out.println("Vowels are "+v+" and consonants are "+c);
    }
}


output:

Enter String :
karan gajjar 123
Vowels are 4 and consonants are 7

Write a program to find length of string and print second half of the string.

Unit - 2

Practical - 2


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        System.out.println("Length of ths String is :"+s.length());
        System.out.print("second half of the sring is:");
        System.out.println(s.substring(s.length() / 2));
   }
}


output:

Enter String :
gajjar
Length of ths String is :6
second half of the sring is:jar

Write a program to enter two numbers and perform mathematical operations on them

Unit - 1

Practical 2

package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        float m,n;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter two numbers :");
        m = in.nextFloat();
        n= in.nextFloat();
        while(true)
        {
            System.out.println("Enter your choice:\n1.sum\t2.sub\t3.mul\t4.div\t5.exit : ");
            byte choice= in.nextByte();
            switch(choice)
            {
                case 1:System.out.println("Addition is :"+(m+n));
                break;
             
                case 2:System.out.println("Subtraction is :"+(m-n));
                break;
             
                case 3:System.out.println("mul is :"+(m*n));
                break;
             
                case 4:System.out.println("div is "+(m/n));
                break;
             
                case 5:System.exit(0);
                break;
             
                default:System.out.println("Enter proper choice");
                break;
             
            }
        }
   }
}


output:

Enter two numbers :
2 2
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
1
Addition is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
2
Subtraction is :0.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
3
mul is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
4
div is 1.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
5

Write a program that calculate percentage marks of the student if marks of 6 subjects are given.

Unit 1

Practical - 1


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        float sum=0;
        Scanner in =new Scanner(System.in);
        float a[]= new float[6];
        System.out.println("Enter marks of total six subjects :");
        for(int i=0;i<6;i++)
        {
            a[i]=in.nextFloat();
            sum = sum+a[i];
        }
        System.out.println("Percentage is :"+sum/6);
    }
}


output:

Enter marks of total six subjects :

1 2 3 4 5 6
Percentage is :3.5

Write a program to convert rupees to dollar. 60 rupees=1 dollar

Practical - 1


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
   
        double i;
        Scanner in =new Scanner(System.in);
       
        System.out.println("Enter Rs. to convert into $ :");
        i = in.nextInt();
        System.out.println("INR = "+i+"\nUSD = "+i/67.50);
   }
}

output:

Enter Rs. to convert into $ :
10000
INR = 10000.0
USD = 148.14814814814815

Matrix Multiplication - java

package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
    int m, n, p, q, sum = 0, i, j, k;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of 1st matrix");
      m = in.nextInt();
      n = in.nextInt();
      int a[][] = new int[m][n];

      System.out.println("Enter the data of 1st matrix:");

      for ( i = 0 ; i < m ; i++ )
         for ( j = 0 ; j < n ; j++ )
            a[i][j] = in.nextInt();

      System.out.println("Enter the number of rows and columns of 2nd matrix");
      p = in.nextInt();
      q = in.nextInt();

      if ( n != p )
         System.out.println("Matrices can't be multiplied !.");
      else
      {
         int b[][] = new int[p][q];
         int c[][] = new int[m][q];

         System.out.println("Enter data of 2nd matrix");

         for ( i = 0 ; i < p ; i++ )
            for ( j = 0 ; j < q ; j++ )
               b[i][j] = in.nextInt();

         for ( i = 0 ; i < m ; i++ )
         {
            for ( j = 0 ; j < q ; j++ )
            {  
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + a[i][k]*b[k][j];
               }

               c[i][j] = sum;
               sum = 0;
            }
         }

         System.out.println("Multiplication is :");

         for ( i = 0 ; i < m ; i++ )
         {
            for ( j = 0 ; j < q ; j++ )
               System.out.print(c[i][j]+" ");

            System.out.print("\n");
         }
      }
   }
 
}


output:

Enter the number of rows and columns of 1st matrix
3
3
Enter the data of 1st matrix:
1
2
3
4
5
6
7
8
9
Enter the number of rows and columns of 2nd matrix
3
3
Enter data of 2nd matrix
9
8
7
6
5
4
3
2
1
Multiplication is :
30 24 18
84 69 54
138 114 90

Sum of two 2d array java-scanner


package pl;
import java.util.Scanner;
/**
 *
 * @author Kanu
 */
public class Pl {

    public static void main(String[] args) {
     int m,n;
           
            System.out.print("Enter rows and columns : ");
            Scanner in = new Scanner(System.in);
            m = in.nextInt();  
            n = in.nextInt();
            System.out.println("Enter data for array 1:");
            int [][]a=new int[m][n];          
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++){
                System.out.print("Enter Num :");
                a[i][j]=in.nextInt();
                }
            }
            System.out.println("Enter data for array 2:");
            int [][]b=new int[m][n];          
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++){
                System.out.print("Enter Num :");
                b[i][j]=in.nextInt();
                }
            }
            int [][]c=new int[m][n];          
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                    c[i][j]=a[i][j]+b[i][j];
                }
            }
            System.out.println("Array 1 is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(a[i][j]+"");
                }System.out.println();
               
            }
            System.out.println("Array 2 is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(b[i][j]+"");
                }System.out.println();
            }
            System.out.println("sum of two array is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(c[i][j]+"");
                }System.out.println();
               
            }
    }
}

output:

Enter rows and columns : 2
2
Enter data for array 1:
Enter Num :1
Enter Num :2
Enter Num :3
Enter Num :4
Enter data for array 1:
Enter Num :1
Enter Num :2
Enter Num :3
Enter Num :4
Array 1 is :
12
34
Array 2 is :
12
34
sum of two array is :
24
68

Sunday 3 July 2016

Array of Sum - java with scanner and bufferedreader

Scanner class 

import java.util.Scanner;

public class Pl {

    public static void main(String[] args)  {
        int n,sum=0,num;    
        System.out.print("Enter length : ");
            Scanner in = new Scanner(System.in);
            n = in.nextInt();    
            int []a=new int[n];            
            for(int i=0;i<n;i++)
            {
               System.out.print("Enter Num :");
               num = in.nextInt();
               a[i]=num;
               sum = sum+a[i];
            }
            for(int i =0;i<n;i++)
            {
                System.out.print(a[i]+" ");
            }
          System.out.print("Sum is "+sum);
 
    }
 
}

output:
Enter length : 3
Enter num : 1
Enter num : 2
Enter num :3

1 2 3
sum is : 6

BufferedReader

package pl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pl {

    public static void main(String[] args) throws IOException  {
        int sum=0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter length");
        int n = Integer.parseInt(br.readLine());
        int []a=new int[n];              
        for(int i =0 ;i <n;i++)
        {
            System.out.print("Enter num:");
            int k = Integer.parseInt(br.readLine());
            a[i]=k;
            sum= sum + a[i];
        }
        for(int i =0;i<n;i++)
        {
            System.out.print(a[i]+" ");
        }
        System.out.print("Sum i :"+sum);
       

    }
   

}

5th sem syllabus and Materials

Syllabus  -  click here


E - Books 


algorithm - click here

java the complete reference - click here


Practical List


java  - click here

algorith - click here

Sunday 1 May 2016

Write a program to merge content of two files to third file.

Practical - 36

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
    ifstream i1, i2;
    ofstream o;
    char ch;
    i1.open("test1.txt");
    i2.open("test2.txt");
    if(i1==NULL || i2==NULL)
    {
        cout<<"error\n";
    }
    o.open("test3.txt");
    if(!o)
    {
        cout<<"error in file";
    }
    while(i1.eof()==0)
    {
        i1>>ch;
        o<<ch;
    }
    while(i2.eof()==0)
    {
        i2>>ch;
        o<<ch;
    }
    cout<<"The two files were merged into test3.txt";
    i1.close();
    i2.close();
    o.close();
return 0;
}

Write program to illustrate different seek() and tell() options

seekg(), tellg(), seekp(), tellp()

 

 

Write a simple Program for Exception Handling to raise Divide by zero exception.

Practcal - 38


#include <iostream>
using namespace std;

double division(int a, int b)
{
   if( b == 0 )
   {
      throw "Division by zero condition!";
   }
   return (a/b);
}

int main ()
{
   int x = 50;
   int y = 0;
   double z = 0;

   try {
     z = division(x, y);
     cout << z << endl;
   }catch (const char* msg) {
     cerr << msg << endl;
   }

   return 0;
}

Write a simple Program for Exception Handling with Multiple Catch blocks and also generic handler.

 Practical - 39


#include<iostream>
using namespace std;

void add(int x)
{
    try{
    if(x==0)
    {
        throw 'x';
    }
    else{
        if(x==1)
        throw x;
    else
        throw 5.0;
    }}
    catch(char c)
    {
        cout<<"\nChar exception";
    }
    catch(int i)
    {
        cout<<"\nint exception";
    }
    catch(double z)
    {
        cout<<"\ndouble exception";
    }
    /*
    remove all catch above and un comment this
    catch(...)
    {
        cout<<"\nFound exception generic:";
    }
    */
}

int main()
{
        add(0);
        add(1);
        add(5.5);
    return 0;
}

Write a C++ Program to solve quadratic equation. Raise exception when roots are not real.

Practical - 40

#include <iostream>
#include <cmath>
using namespace std;

int main() {

    float a, b, c, x1, x2, d;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;

    try{
    d = b*b - 4*a*c;

    if (d > 0) {
        x1 = (-b + sqrt(d)) / (2*a);
        x2 = (-b - sqrt(d)) / (2*a);
        cout << "Roots are real and different." << endl;
        cout << "x1 = " << x1 << endl;
        cout << "x2 = " << x2 << endl;
    }

    else if (d == 0) {
        cout << "Roots are real and same." << endl;
        x1 = (-b + sqrt(d)) / (2*a);
        cout << "x1 = x2 =" << x1 << endl;
    }

    else {
            throw "\nRoots are not real";
    }
    }catch(char const *c)
    {
        cout<<c;
    }

    return 0;
}

Output:
Enter co -eff : 3
-4
10

roots are not real

Write a C++ Program that raised object exception. Create student class with enrollment no, CPI, namedata members. Throw exception of typestudent objectfor CPI less than 4. Catch exception and print details of corresponding Student.

Practical - 41


#include<iostream>

using namespace std;

class student{

    int eno,cpi;
    char name[10];

public:
    void getdata()
    {
        cout<<"\nEnter enrollment no:";
        cin>>eno;

        cout<<"\nEnter name:";
        cin>>name;

        c:
        try{
        cout<<"\nEnter Cpi:";
        cin>>cpi;
        if(cpi<4)
        {
            throw cpi;

        }
        }catch(int i)
        {
            cout<<"\nCpi "<<i<<" not valid";
            goto c;
        }
    }
    void display()
    {
        cout<<"\n"<<eno<<"\t"<<name<<"\t"<<cpi<<endl;
    }
};

int main()
{
    student s[10];
    int n;
    cout<<"\nEnter no of student:";
    cin>>n;
    for(int i=0;i<n;i++)
    {
        s[i].getdata();
    }
    cout<<"\nEno\tName\tCpi";
    for(int i=0;i<n;i++)
    {
        s[i].display();
    }
return 0;
}

type conversion friend function


Practical - 23





#include<iostream>
using namespace std;
class T12
{
      int h,m;
      char c;
      public:
      T12()
      {
            h=0;
            m=0;
            c='\0';
      }
      void getdata()
      {
            cout<<"\n Enter the time according to 12 hour";
            cout<<"\n enter no of hour : ";
            cin>>h;
            cout<<"\n enter no of minitues : ";
            cin>>m;
            cout<<"\n enter a for a.m or p for p.m : ";
            cin>>c;
     
      }
      friend class T24;
     
};
class T24
{
      int ho,mi;
      public:
      T24()
      {
            ho=0;
            mi=0;
      }
      T24(T12 t1)
      {
            if(t1.c == 'p')
            {
                  ho = t1.h + 12;
                  mi = t1.m;
            }
            else
            {
                  ho = t1.h;
                  mi = t1.m;
            }
      }
     
      friend void disp(T24);
};
void disp(T24 t2)
{
      cout<<"\n hour : "<<t2.ho;
      cout<<"\n minitues : "<<t2.mi;
}
int main()
{
     
      T12 t1;
      T24 t2;
      t1.getdata();
      t2=t1;
      disp(t2);
      return 0;
}
 

Wrie a Program to find simple payroll system using single inheritance

Practical - 24


#include<iostream>
using namespace std;
class employee
{
   public:
     int eno;
     char name[20],des[20];
     void get()
     {
              cout<<"Enter the employee number:";
              cin>>eno;
              cout<<"Enter the employee name:";
              cin>>name;
              cout<<"Enter the designation:";
              cin>>des;
     }
};

class salary:public employee
{
     float bp,hra,sb,leave,np;
   public:
     void get1()
     {
              cout<<"Enter the basic pay:";
              cin>>bp;
              cout<<"Enter the Human Resource Allowance:";
              cin>>hra;
              cout<<"Enter the Special Bonus :";
              cin>>sb;
              cout<<"Enter the Leave Deduction rs:";
              cin>>leave;
     }
     void calculate()
     {
              np=bp+hra+sb-leave;
     }
     void display()
     {
              cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<sb<<"\t"<<leave<<"\t"<<np<<"\n";
     }
};

int main()
{
    int i,n;
    salary s[10];

    cout<<"Enter the number of employee:";
    cin>>n;
    for(i=0;i<n;i++)
    {
              s[i].get();
              s[i].get1();
              s[i].calculate();
    }
    cout<<"\ne_num \t e_name\t des \t bp \t hra \t sb \t leave \t total \n";
    for(i=0;i<n;i++)
    {
              s[i].display();
    }
    return 0;
}

write a c++ program to find student info using multiple inheritance

 Practical - 25


#include<iostream>
using namespace std;

class student
{
    protected:
       int no,m1,m2;
    public:
            void get()
              {
                            cout<<"Enter the Roll no :";
                            cin>>no;
                            cout<<"Enter the two marks :";
                            cin>>m1>>m2;
              }
};
class Practical
{
    protected:
       int pm;
    public:
                void getpm()
              {
                 cout<<"\nEnter the Practical mark :";
                 cin>>pm;

              }
};
class result:public student,public Practical
{
    int tot,avg;
    public:
    void display()
              {
                 tot=(m1+m2+pm);
                 avg=tot/3;
                 cout<<"\nRoll No    : "<<no<<"\nTotal      : "<<tot;
               cout<<"\nAverage    : "<<avg;
              }
};
int main()
{
   result r;
   r.get();
   r.getpm();
   r.display();
   return 0;

}

Thursday 28 April 2016

Derive the two classes son and daughter and demonstrate polymorphism in action

 Practical - 26


#include <iostream>
using namespace std;
class father
{
protected:
     int age;
public:
     father(int a)
     {
        age=a;
     }
    virtual void whoisme()
     {
     cout<<"\nI am father.My age is:"<<age;
     }
};

class son:public father
{
private:
     int ages;
public:
     son(int y,int x):father(x)
     {
     ages=y;
     }
     void whoisme()
     {
     cout<<endl<<"I am son.My age is: "<<ages;
     }
};
class daughter:public father
{
private:
     int aged;
public:
     daughter(int c,int l):father(l)
     {
     aged=c;
     }
     void whoisme()
     {
     cout<<endl<<"I am daughter.My age is: "<<aged;
     }
};
int main()
{
father f(55);
son s(2,24);
daughter d(1,25);
f.whoisme();
s.whoisme();
d.whoisme();
father *p;
p=&s;
p->whoisme();
p=&d;
p->whoisme();
return 0;
}

Create a class called Person, that has member data name. Include member functions getdata() to get data from the user, and putdata() function to display its data. Write a main( ) program that creates an array of pointers, persPtr[100] to Person. In a loop, ask the user for data, and use new to create an object of type Person to hold the data. Put the pointer to the object in the array. When the user has finished entering the data for all the persons, use a for loop and a single statement such as persPtr[I]->putdata( ) to display the data from each object in the array

Practical 27


 #include <iostream>
using namespace std;

class Person
{
    string name;
public:
    void getdata()
    {
        cout<<"\nEnter Name:";
        cin>>name;
    }
    void putdata()
    {
        cout<<"\nYour name :"<<name;
    }
};

int main()
{
    int n;
    Person *persptr[10];
    cout<<"\nHow Many Names:";
    cin>>n;

    for(int i=0;i<n;i++)
    {
        persptr[i] = new Person;
        persptr[i]->getdata();
    }
    for(int i=0;i<n;i++)
    {
        persptr[i]->putdata();
    }

    return 0;
}

Thursday 21 April 2016

WAP in c++ to convert lowercase to uppercase from a file.

Practical 28



#include <iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    ifstream f;
f.open("test.txt");
string line;
while (!f.eof())
{
  f >> line;
  for (int j=0; j< line.length(); ++j)
  {
    line[j] = toupper(line[j]);
  }
  cout<<line<<endl;
     }

    return 0;
}

A hospital wants to create a file regarding its indoor patients. The information to store include _ Name of the patient _ Date of admission _ Disease _ Date of discharge Create a base class to store the above information. The member function should include functions to enter information and display a list of all the patients in the database. And apply the search function also.

Practical 29


#include <iostream>
#include<fstream>
#include<stdlib.h>

using namespace std;

class Hospital
{
    string name,disease;
    string a[10];
    int c;
    long int dateadd,discharge;
    public:
        Hospital()
        {

            c=0;
         string line;
            fstream f1;
            f1.open("hosp.txt",ios::in|ios::out|ios::app);
            while(getline(f1,line))
            {
                c++;
            }
        }
    void adddata()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);

        cout<<"\nEnter Name of Patient:";
        cin>>name;

        cout<<"\nEnter Date of admission:";
        cin>>dateadd;

        cout<<"\nEnter Disease:";
        cin>>disease;

        cout<<"\nEnter date of discharge:";
        cin>>discharge;

        if(c==0)
        {
            f1<<endl;
        }

        f1<<name<<" "<<dateadd<<" "<<disease<<" "<<discharge<<endl;
        f1.close();
    }

    void patients()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);
        f1.clear();
        f1.seekg(0);
        string l,l1;
        while(getline(f1,l1,' '))
        {
            f1>>l;
            cout<<l1;
        }
        f1.close();
    }

    void searchp()
    {
        fstream f1;
        f1.open("hosp.txt",ios::in|ios::out|ios::app);
        f1.clear();
        f1.seekg(0);
        string p,p1;
        int i=0;
        while(getline(f1,p1,'\n'))
        {
            f1>>p;
            a[i]=p;
            f1>>p1;
            i++;
        }
        string s,line;
        cout<<"\nEnter the name you want to search";
        cin>>s;
        for(i=0;i<c-1;i++)
        {
            if(a[i]==s)
            {
                cout<<"\n\nFound "<<s<<endl;
                break;
            }
            if(i==c-2)
            {
                cout<<"\nNot Found";
            }
        }
        f1.close();
    }
};

int main()
{
    Hospital h;
    int ch;
    while(1)
    {
        cout<<"\n1.Enter Information\n2.Display Patients\n3.Search Patient\n5.exit";
        cin>>ch;

        switch(ch)
        {
        case 1:
            h.adddata();
            break;
        case 2:
            h.patients();
            break;
        case 3:
            h.searchp();
            break;
        case 5:
            exit(0);
            break;
        default:
            cout<<"\nEnter proper choice\n";
        }
    }
    return 0;
}