Tuesday 31 July 2018

Program-16 Array Queue Insertion and Deletion

#include<iostream.h>

// #include<stdlib.h>

#include<process.h>

#include<conio.h>

const int size = 50;

int Queue[size],front=-1,rear=-1;

 int Insert(int Queue[], int ele)

  {

     if(rear == size-1) return -1;

     else

     if(rear==-1)

     {

      front=rear=0;

      Queue[rear]=ele;

     }

     else

     {

    rear++;

    Queue[rear]=ele;

     }

     return 0;

   }


   int Remove(int Queue[])

   {

     int ret;

     if(front==-1) return -1;

     else

     {

       ret=Queue[front];

       if(front == rear) front=rear=-1;

       else

       front++;

     }

     return ret;

   }

  void Display(int SQueue[],int front,int rear)

  {

     if(front==-1) return;

     for(int i=front;i<rear;i++)

    cout<<Queue[i]<<"<-";

     cout<<Queue[rear]<<endl;

  }

int main()

{

   int Item,res; char ch='Y';

   clrscr();


   while( ch=='y'||ch=='Y')

   {

     cout<<"\n  Enter Item for Insertion\n";

     cin>>Item;

     res=Insert(Queue,Item);

     if(res==-1)

       {

     cout<<"Overflow \n";exit(1);

    }

      cout<<"\n Now the Queue(front....to....rear)is: \n";

      Display(Queue,front,rear);

      cout<<"\n Want to insert more elements?(y|n)...";

      cin>>ch;

   }


     cout<<"Now deletion of elements begins,,,\n";

     cout<<"\nwant to delete elements ?(y/n)....\n";cin>>ch;

     while(ch=='y'||ch=='Y')

     { res=Remove(Queue);

       if( res==-1)

       {cout<<"Underflow!!!Aborting!!\n";   exit(1);

       getch();

       }

       else

       {  cout<<"\n Element deletion is: "<<res<<endl;

      cout<<"now the Queue(front...to...back) begins:";

      Display(Queue,front,rear);

       }

       cout<<"\nwant to delete more elements ?(y/n)....\n";cin>>ch;

     }

     return 0;

  }//main end

Program-15 Stack implementation using Array:Insertion and Deletion

#include<iostream.h>

#include<process.h>

#include<conio.h>

const int size=50;

 int Push(int Stack[], int & top, int ele)

 {

    if(top == size-1) return -1;

    else

      {

       top++;

       Stack[top]=ele;

      }

    return 0;

 }

 int pop(int stack[], int &top)

 {

    int ret;

    if(top == -1) return -1;

    else

     {

       ret = stack[top];

        top--;

    }

    return ret;

 }

 void display(int stack[], int top)

 {

    if(top==-1) return;

    cout<<stack[top]<<"<-"<<endl;

    for(int i=top-1;i>=0;i--)

      cout<<stack[i]<<endl;

 }

int main()

{

  int Stack[size],Item,top=-1,res;

  char ch='y';

  while(ch=='y'|| ch=='Y')

  {

    cout<<"\n Enter ITEm for insertion :";

    cin>>Item;

    res=Push(Stack,top,Item);

    if(res==-1)

    {

      cout<<"Overflow !!! \n";

      exit(1);

    }

    cout<<"The stack now is:\n";

    display(Stack,top);


    cout<<"\n want to insert more elements ?(y/n)";

    cin>>ch;

  }




  cout<<"Now deletion of elements begins ...?\n";

  cout<<"\n want to delete elements...?(y/n) \n";

  cin>>ch;

  while(ch=='y' || ch=='Y')

  {

    res=pop(Stack,top);

    if(res==-1)

    {

      cout<<"Underflow \n";

      exit(1);

    }

    else

    {

      cout<<"\n Element deleted is:"<<endl;

      cout<<"\n The stack now is :\n";

      display(Stack,top);

    }

    cout<<"\n want to delete more elements...?(y/n) \n";

   cin>>ch;

  }

   return 0;

   getch();

 }




Program-16 Array Queue Insertion and Deletion


#include<iostream.h>
// #include<stdlib.h>
#include<process.h>
#include<conio.h>

int Remove(int []);
int Insert(int [], int);
void Display(int [], int, int);

const int size = 50;
int Queue[size],front=-1,rear=-1;

int main()
{
   int Item,res; char ch='Y';
   clrscr();

   while( ch=='y'||ch=='Y')
   {
     cout<<"\n  Enter Item for Insertion\n";
     cin>>Item;
     res=Insert(Queue,Item);
     if(res==-1)
       {
     cout<<"Overflow \n";exit(1);
    }
      cout<<"\n Now the Queue(front....to....rear)is: \n";
      Display(Queue,front,rear);
      cout<<"\n Want to insert more elements?(y|n)...";
      cin>>ch;
   }

     cout<<"Now deletion of elements begins,,,\n";
     ch='Y';
     while(ch=='y'||ch=='Y')
     { res=Remove(Queue);
       if( res==-1)
       {cout<<"Underflow!!!Aborting!!\n";   exit(1);
       getch();
       }
       else
       {  cout<<"\n Element deletion is: "<<res<<endl;
      cout<<"now the Queue(front...to...back) begins:";
      Display(Queue,front,rear);
       }
       cout<<"\nwant to delete more elements ?(y/n)....\n";cin>>ch;
     }
     return 0;
  }//main end

  int Insert(int Queue[], int ele)
  {
     if(rear == size-1) return -1;
     else
     if(rear==-1)
     {
      front=rear=0;
      Queue[rear]=ele;
     }
     else
     {
    rear++;
    Queue[rear]=ele;
     }
     return 0;
   }
   int Remove(int Queue[])
   {
     int ret;
     if(front==-1) return -1;
     else
     {
       ret=Queue[front];
       if(front == rear) front=rear=-1;
       else
       front++;
     }
     return ret;
   }
  void Display(int SQueue[],int front,int rear)
  {
     if(front==-1) return;
     for(int i=front;i<rear;i++)
    cout<<Queue[i]<<"<-";
     cout<<Queue[rear]<<endl;
  }

Prog-14.Write a Program to implement Insertion Sort using a 1-D array.

 #include<iostream.h>

 void InsSort(int[ ], int);

int main()
 {
   int AR[50],item, n, index;
   cout<<"How many elements do u want to create array with?(max..50)..";
   cin>>n;
   cout<<"\n Enter Array elements ..\n";
   for(int i=0;i<n;i++)
    cin>>a[i];

    InsSort(AR, n);
    cout<<"\n \n The sorted array is as shown below \n";
     for(i=0;i<n;i++)
    cout<<AR[i]<<" ";
     cout<<endl;
     return 0;
 }

 void InsSort(int AR[ ], int size)
 {
     int t,j;
     AR[0]=INT_MIN;
         for(int i=1;i<=size;i++)
          {
            t=AR[i];
            j=i-1;
               while(t<AR[j])
               {
                   AR[j+1]=t;
                   j--;
               }  // while end
              AR[j+1]=t;
             cout<<"Array after pass-"<<i<<"-is";
             for(int k =1;k<=size;k++)
             cout<<AR[k]<<" ";
             cout<<endl;
       } // for i loop end
 }
       

Prg-10.Write a Program to implement Bubble sort using a 1-D array.

 #include<iostream.h>

 void Bubblesort(int[ ], int);

int main()
 {
   int AR[50],item, n, index;
   cout<<"How many elements do u want to create array with?(max..50)..";
   cin>>n;
   cout<<"\n Enter Array elements ..\n";
   for(int i=0;i<n;i++)
    cin>>a[i];

   Bubllesort(AR, n);
   cout<<"\n \n The sorted array is as shown below \n";
   for(i=0;i<n;i++)
    cout<<AR[i]<<" ";
   cout<<endl;

 }
 void Bubblesort(int AR[], int size)
 {
   int t,ct=0;
   for(int i=0; i<size; i++)
   {
     for(int j=0; j<(size-1); j++)
     {
       if(AR[j]>AR[j+1])
        {
          t=AR[j];
          AR[j]=AR[j+1];
          AR[j+1]=t;
         }
      }
      cout<<"Array after iteration-"<<++ct<<"-is:";
      for(int k=0;k<size;k++)
       cout<<AR[k]<<" ";
      cout<<endl;
    }
 }

Program-12 Write a program to implement Selection Sort using 1-D Array

 #include<iostream.h>

clrscr();
void selsort(int[],int);
int main()
{int ar[60],item,n,index;
cout<<"how many elements you want to create with array?";
cin>>n;
cout<<"\nenter array elements...";
for(int i=0;i<n;i++)
cin>>ar[i];
selsort(ar,n);
cout<<"\n\nthe stored array is nas shown below...\n";
for(i=0;i<n;i++)
cout<<ar[i]<<"  ";
cout<<endl;
return 0;
}
void selsort(int ar[],int size)
{
    int small,pos,tmp;
    for(int i=0;i<size;i++)
   {

       small=ar[i];
       pos=i;
      for(int j=i+1;j<size;j++)
     { 

           if(ar[j]<small)
          {

             small=ar[j];
             pos=j;

         }   //if end
    }//j loop end
  tmp=ar[i];
  ar[i]=ar[pos];
  ar[pos]=tmp;
  cout<<"\narray after pass-"<<i+1<<"-is:";
  for(j=0;j<size;j++)cout<<ar[j]<<" ";
 }// i loop end

}// function end

Prgoram-13 Write a C++ Program to implement Binary Search

#include<iostream.h>

int bsearch(int[],int,int);
int main()
{int ar[50],item,n,index;
cout<<"enter desired array size...";
cin>>n;
cout<<"\n enter array elements";
for(int i=0;i<n;i++)
cin>>ar[i];
cout<<"\nenter element to be searcherd for...";
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
cout<<"\nsorry!!given elment could not found.\n";
else
cout<<"\nelement found at index:"<<index<<",position:"<<index+1<<endl;
return 0;
}
int bsearch(int ar[],int size,int item)
{

      int beg,last,mid;
      beg=0;
      last=size-1;
 while(beg<=last)
  { 

       mid=(beg+last)/2;
       if(item==ar[mid])

             return mid;
         else 

          if (item>ar[mid])

               beg=mid+1;
           else

                  last=mid-1;
   }

  return-1;
}

Prog-11 Linear Search using 1-D Array

#include<iostream.h>

#include<conio.h>

int Lsearch(int AR[],int size,int item)
 { for(int i=0;i<size;i++)
  { if(AR[i]==item)
    return i;
  }
 return -1;
 }

void main()
{
int AR[50],ITEM,N,index;
cout<<"\nEnter Desired Array Size: ";
cin>>N;
cout<<"\nEnter Array Elements: ";
for(int i=0;i<N;i++)
{
cin>>AR[i];
}
cout<<"\nEnter Element to be searched for : ";
cin>>ITEM;
index=Lsearch(AR,N,ITEM);
if(index==-1)
cout<<"\nSOrry!! Given Element could not be found.";
else
cout<<"\nElement found at index : "<<index<<", Position :"<<index+1;
getch();
}

Monday 23 July 2018

11-C Prog-9 Write Program to find absolute value for integer, real and Complex Numbers



# Python code to illustrate 
# abs() built-in function

# floating point number
float = -54.26
print('Absolute value of integer is:', abs(float))

# An integer
int = -94
print('Absolute value of float is:', abs(int))

# A complex number
complex = (3 - 4j)
print('Absolute value or Magnitude of complex is:', abs(complex))
Example:
Absolute value of integer is: 54.26
Absolute value of float is: 94
Absolute value or Magnitude of complex is: 5.0

11C Prg-8 Sort 3 numbers without using conditional or looping statments


Algorithm:

#Program
x = int(input("Input first number: ")) y = int(input("Input second number: ")) z = int(input("Input third number: ")) small = min(x, y, z) large = max(x, y, z) midd = (x + y + z) - small - large print("Numbers in sorted order: ",small,midd,large)

Example:

Input first number: 5
Input second number: 7
Input third number: 6
Numbers in sorted order:  5 6 7

Wednesday 18 July 2018

11-C Data Types: Numbers, Strings, List and Tuples, Dictionary

Numbers:
a) integers: int
b) booleans  bool(0) - False bool(1) - True
c) floating point numbers: float
d) complex numbers: a= 0+3.1j    b=1.5+2j

Strings:
String is a sequence of characters can be individually accessed using its index

subject="Computers"
subject
'Computers'
subject[-9]
'C'

List 
a=[1,2,3,4,5]
print(a)

Tuples:
p=(1,2,3,4,5)

Value of type list are mutable i.e. changeable- one can change/delete/add a list's elements.  But the values of type tuple are immutable i.e. non-changeable:


Dictionary:
It is an unordered set of comma-separated key:value

example: v= {'a':1,'e':2,'i':3,'o':4,'u':5}

v['a']   ans: 1      v['u'] ans: 5

Output Problem 4 || Pointers || CBSE Class 12 Computer Science

https://www.youtube.com/watch?v=BaQAkbjvsMA

Tuesday 17 July 2018

11-C Prog-6. Write a program to read the value of a variable of a quadratic equation and find the discriminant and give the root of the quadratic equation.

Algorithm
Input:
1).a    2.) b   3.) c
Ouput:
1.Roots are distinct or Roots are equal or Imaginary Root
Method:
1.Start
2. Read a, b and c
3. Write a, b and c
4. d= b*b-4*a*c
5. Write d
6. #logic
  if(d==0)
  begin
     r1=r2= -b /(2*a)
     Write "Root1=Root2= ",r1
  end
  else
  if( d>0)
  begin
     r1 = (-b + sqrt(d)  ) /( 2*a)
     r2 = (-b - sqrt(d)  ) /( 2*a)
     write" Root1 = ", r1
     write" Root2 = ", r2
  end
  else
  begin
      write "Roots are imaginary"
  end
7. stop
Example1:

Enter the a co-efficient  1
Enter the b co-efficient  2
Enter the c co-efficient  1
a= 1.0 b= 2.0 c= 1.0
d= 0.0
Root1=Root2= -1.0
 
Example2: 
Enter the a co-efficient 2
Enter the b co-efficient 5
Enter the c co-efficient 2
a= 2.0 b= 5.0 c= 2.0
d= 9.0
Root1= -0.5
Root2 -2.0 
Example3:
Enter the a co-efficient  3
Enter the b co-efficient  1
Enter the c co-efficient  2
a= 3.0 b= 1.0 c= 2.0
d= -23.0
Imaginary number



#Program

import math

a=float(input("Enter the a co-efficient   "))
b=float(input("Enter the b co-efficient  "))
c=float(input("Enter the c co-efficient  "))

print("a=",a,"b=",b,"c=",c)
d=b*b-4*a*c
print("d=",d)
#logic
if d==0:
    r1=r2=-b/(2*a)
    print("Root1=Root2=",r1)
else:
    if d>0:
        r1=(-b+math.sqrt(d))/(2*a)
        r2=(-b-math.sqrt(d))/(2*a)
        print("Root1=",r1)
        print("Root2",r2)
    else:
        print("Imaginary number")      


11-C Program-7 Write a program to input number of week days (1-7) and translate to its equivalent name of the week. (1 to Monday, 2 to Tuesday……7 to Sunday)*/

#7.Write a program to input number of week days (1-7) and translate to its #equivalent name of the week. (1 to Monday, 2 to Tuesday……7 to Sunday)

Algorithim:
Input:
1.n_d (name of the day)
Output:
1. Week of Day (Mon to Sunday as per input)
Method:
1.Start
2. Read n_d
3. Write n_d
4. #logic
  if(n_d == 1)
  begin
     Write "Monday"
 end
 else 
if(n_d == 2)
  begin
     Write "Tuesday"
 end
 else
 if(n_d == 3)
  begin
     Write "Monday"
 end
 else 
  .
  .
  .
if(n_d == 7)
  begin
     Write "Sunday"
 end
 else 
 begin
     Write" Wrong choice:(choose 1 to 7
 end
5.stop

Example:
Enter number of day (1-7)7 Number of Day= 7 The Day is Sunday


#program
 
n_d=int(input("Enter number of day (1-7)"))
print("Number of Day=",n_d)
#logic
if n_d==1:
    print("The Day is Monday")
else:
    if n_d==2:
        print("The Day is Tuesday")
    else:
        if n_d==3:
            print("The Day is Wednesday")
        else:
            if n_d==4:
                print("The Day is Thursday")
            else:
                if n_d==5:
                    print("The Day is Friday")
                else:
                    if n_d==6:
                        print("The Day is Saturday")
                    else:
                        if n_d==7:
                            print("The Day is Sunday")
                        else:
                            print("Wrong Choice enter(1 to 7)")