Friday, 12 August 2016

KU 6TH SEM PRACTICAL SOLUTIONS 2 ~ gniithelp

Set -1

1. Write a program for frame sorting technique used in buffers.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct frame
{
       int fslno;
       char finfo[20];
};
struct frame arr[10];
int n;
void sort()
{
       int i,j,ex;
       struct frame temp;
       for(i=0;i<n;i++)
       {
              ex=0;
              for(j=0;j<n-i-1;j++)
              if(arr[j].fslno>arr[j+1].fslno)
              {
                     temp=arr[j];
                     arr[j]=arr[j+1];
                     arr[j+1]=temp;
                     ex++;
              }
              if(ex==0) break;
       }
}
void main()
{
       int i;
       clrscr();
       printf("\n Enter the number of frames \n");
       scanf("%d",&n);
       for(i=0;i<n;i++)
       {
              arr[i].fslno=random(50);
              printf("\n Enter the frame contents for sequence number %d\n",arr[i].fslno);
              scanf("%s",arr[i].finfo);
       }
       sort();
       printf("\n The frames in sequence \n");
       for(i=0;i<n;i++)
       printf("\n %d\t%s \n",arr[i].fslno,arr[i].finfo);
       getch();
}

2. Write a program in C# using command line arguments to display a welcome message. The message has to be supplied as input in the command line.
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            args = new string[1];

            Console.Write("Enter Message Here: ");
            args[0] = Console.ReadLine();

            Console.WriteLine("\n" + args[0]);
            Console.ReadLine();
        }
    }
}

Set -2
1. Write a program for distance vector algorithm to find suitable path for transmission.
#include<stdio.h>
#define MAX 10
struct dist_vect
{
    int dist[MAX];
    int from[MAX];
};
int main()
{
    int adj[MAX][MAX],n,i,j,hop[10][10]={{0}},k,count;
    struct dist_vect arr[10];
    printf("Enter the number of nodes\n");
    scanf("%d",&n);
    printf("Enter adjecency matrix\n");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        scanf("%d",&adj[i][j]);
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            arr[i].dist[j]=adj[i][j];
            arr[i].from[j]=j;
        }
    }
    count=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<n;k++)
            {
                if(arr[i].dist[j]>adj[i][k]+arr[k].dist[j])
                {
                    arr[i].dist[j]=adj[i][k]+arr[k].dist[j];
                    arr[i].from[j]=k;
                    count++;
                    if(count==0)hop[i][j]=1;
                    else hop[i][j]=count+hop[k][j];
                }
            }
            count=0;
        }
    }
    for(i=0;i<n;i++)
    {
        printf("State value of router under %d",i+1);
        printf("\nNode\tvia node\tdistance\tnumber of hops\n");
        for(j=0;j<n;j++)
        {
            if(i==j)
            printf("\n%d\t%d\t%d\n",j+1,arr[i].from[j]+1,arr[i].dist[j]);
            else
            printf("\n%d\t%d\t\t%d\t\t%d\n",j+1,arr[i].from[j]+1,arr[i].dist[j],hop[i][j]+1);
        }
    }
}

2. Write a program to demonstrate the usage of threads in C#.
using System;
using System.Threading;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main()
        {
            Thread thread1 = new Thread(new ThreadStart(A));
            Thread thread2 = new Thread(new ThreadStart(B));
            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();
        }

        static void A()
        {
            Thread.Sleep(100);
            Console.WriteLine('A');
        }

        static void B()
        {
            Thread.Sleep(1000);
            Console.WriteLine('B');
        }
    }
}

Set -3
1. Write a program for simple RSA/DES algorithm to encrypt and decrypt the data.
#include<stdio.h>
#include<math.h>
double min(double x, double y)
{
    return(x<y?x:y);
}
double max(double x,double y)
{
    return(x>y?x:y);
}
double gcd(double x,double y)
{
    if(x==y)
    return(x);
    else
    return(gcd(min(x,y),max(x,y)-min(x,y)));
}
long double modexp(long double a,long double x,long double n)
{
    long double r=1;
    while(x>0)
    {
        if ((int)(fmodl(x,2))==1)
        {
            r=fmodl((r*a),n);
        }
        a=fmodl((a*a),n);
        x/=2;
    }
    return(r);
}
int main()
{
    long double p,q,phi,n,e,d,cp,cq,dp,dq,mp,mq,sp,sq,rp,rq,qInv,h;
    long double ms,es,ds;
    do
    {
        printf("\n Enter prime numbers p and q:");
        scanf(" %Lf %Lf",&p,&q);
    }
    while(p==q);
    n=p*q;
    phi=(p-1)*(q-1);
    do
    {
        printf("\n Enter prime value of e:");
        scanf(" %Lf",&e);
    }
    while((gcd(e,phi)!=1)&&e>phi);
    for(d=1;d<phi;++d)
    {
        if(fmod((e*d),phi)==1)
        break;
    }
    printf("\n D within main = %Lf",d);
    printf("\n Enter the message:");
    scanf(" %Lf",&ms);
    es=modexp(ms,e,n);
    ds=modexp(es,d,n);
    printf("\n Original Message : %Lf",ms);
    printf("\n Encrypted Message : %Lf",es);
    printf("\n Decrypted Message : %Lf\n",ds);
    return(0);
}

2. Using Try, Catch and Finally blocks write a program in C# to demonstrate error handling.
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, divide = 0;
            i = 10;
            j = 0;

            try
            {
                divide = i / j;
            }
            catch (DivideByZeroException)
            {
                divide = 0;
            }
            finally
            {
                Console.WriteLine(divide);
            }
            Console.ReadLine();
        }
    }
}

Set -4
1. Write a program for Spanning Tree Algorithm (PRIM) to find loop less path.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
#define INFINITY 999
#include<conio.h>
enum boolean {FALSE,TRUE};
void prim(int c[][MAX],int t[MAX],int n);
int mincost=0;
int main()
{
    int n,c[MAX][MAX],t[2*(MAX-1)];
    int i,j;
    clrscr();
    printf("\nTo find min path spanning tree");
    printf("\nEnter no of nodes:");
    scanf("%d",&n);
    printf("\nEnter the cost adjacency matrix");
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    scanf("%d",&c[i][j]);
    prim(c,t,n);
    for(i=0;i<2*(n-1);i+=2)
    printf("\n(%d %d)",t[i]+1,t[i+1]+1);
    printf("\nmincost=%d",mincost);
    getch();
    return 0;
}
//using prim's algorithm for finding shortest path
void prim(int c[][MAX],int t[MAX],int n)
{
    int i,j;
    enum boolean v[MAX];
    int k,s,min,v1,v2;
    for(i=0;i<n;i++)
    v[i]=FALSE;
    v[0]=TRUE;
    k=0;
    t[k]=1;
    s=0;
    k++;
    while(k<n)
    {
        min=INFINITY;
        for(i=0;i<n;i++)
        for(j=1;j<n;j++)
        if(v[i]==TRUE && v[j]==FALSE && c[i][j]<min)
        {
            min=c[i][j];
            v1=i;
            v2=j;
        }
        mincost=mincost+min;
        if(min==INFINITY)
        {
            printf("graph disconnected");
            exit(0);
        }
        v[v2]=TRUE;
        k++;
        t[s++]=v1;
        t[s++]=v2;
    }
}

2. Write a program in C# to demonstrate operator overloading.
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main()
        {
            Widget w = new Widget();
            w++;
            Console.WriteLine(w._value);
            w++;
            Console.WriteLine(w._value);

            Widget g = new Widget();
            g++;
            Console.WriteLine(g._value);

            Widget t = w + g;
            Console.WriteLine(t._value);
        }
    }
    class Widget
    {
        public int _value;

        public static Widget operator +(Widget a, Widget b)
        {
            Widget widget = new Widget();
            widget._value = a._value + b._value;
            return widget;
        }

        public static Widget operator ++(Widget w)
        {
            w._value++;
            return w;
        }
    }
}

Set -5
1. Write a program in C# to sort the students list based on their names. The students list is stored in a string array.
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] students = { "Vikash""Bhushan""Deepak""Tapas""Suresh""Avinash" };
            Array arr = students;
            Array.Sort(arr);
           
            foreach (string item in arr)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
    }
}

2. Write a C# Program to demonstrate use of Virtual and override Key words in C#.
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ChildClass cc = new ChildClass();
            cc.Display();
            Console.ReadLine();
        }
    }
    class ParentClass
    {
        public virtual void Display()
        {
            Console.WriteLine("Base Class");
        }
    }
    class ChildClass:ParentClass
    {
        public override void Display()
        {
            base.Display();
            Console.WriteLine("Derived Class");
        }
    }
}

Set -6
1. Write a program in C# , create a class called rectangle which consists of members side_1, side_2 and displayArea(). Write another class square which inherits class rectangle. Take side of square as input and display the area of square on console
using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            square sq = new square();
            sq.displayArea();
            Console.ReadLine();
        }
    }
    class rectangle
    {
        public int side_1 = 10;
        public int side_2 = 20;

        public virtual void displayArea()
        {
            Console.WriteLine("Area of Rectangle is: " + side_1 * side_2);
        }
    }
    class square:rectangle
    {
        public override void displayArea()
        {
            Console.WriteLine("Area of Square is: " + side_1 * side_1);
        }
    }
}


2. Write a program to multiply two matrices.
#include<stdio.h>
int main(){
  int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
  printf("\nEnter the row and column of first matrix");
  scanf("%d %d",&m,&n);
  printf("\nEnter the row and column of second matrix");
  scanf("%d %d",&o,&p);
  if(n!=o){
      printf("Matrix mutiplication is not possible");
      printf("\nColumn of first matrix must be same as row of second matrix");
  }
  else{
      printf("\nEnter the First matrix->");
      for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
      printf("\nEnter the Second matrix->");
      for(i=0;i<o;i++)
      for(j=0;j<p;j++)
           scanf("%d",&b[i][j]);
      printf("\nThe First matrix is\n");
      for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<n;j++){
           printf("%d\t",a[i][j]);
      }
      }
      printf("\nThe Second matrix is\n");
      for(i=0;i<o;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",b[i][j]);
      }      
      }
      for(i=0;i<m;i++)
      for(j=0;j<p;j++)
           c[i][j]=0;
      for(i=0;i<m;i++){ //row of first matrix
      for(j=0;j<p;j++){  //column of second matrix
           sum=0;
           for(k=0;k<n;k++)
               sum=sum+a[i][k]*b[k][j];
           c[i][j]=sum;
      }
      }
  }
  printf("\nThe multiplication of two matrix is\n");
  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<p;j++){
           printf("%d\t",c[i][j]);
      }
  }
  return 0;
}


Set -7
1. Write a C# program to demonstrate handling of server control programs.
In order to create new ASP.NET Server Control, use File – New – Project from Visual Studio 2008 menu to open the New Project dialog box. From the Web project type, choose ASP.NET Server Control.

Code Behind File For ServerControl1.cs :-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FirstServerControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ServerControl1 : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
}

2. Write a program to display a message “Welcome to ASP.NET” 8 times in increasing order of their font size using ASP.NET.
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication9.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
    <head>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name=vs_defaultClientScript content="JavaScript">
        <meta name=vs_targetSchema
        content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body MS_POSITIONING="GridLayout">
        <form id="Form2" method="post" runat="server">
            <%for(int i=0;i<8;i++){%>
                <font size="<%=i%>">Welcome to ASP.NET ASHISH</font><br/></font>
            <%}%>
        </form>
    </body>


</html>

No comments:

Post a Comment