Saturday, November 11, 2017

Kruskal's Algoritm DAA

Tags
Question: Write a program to find the minimum cost of connecting all the engineering colleges in your state using Kruskal's algorithm.
Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Kruskal {

    static  class  edge{
        int distance,s,d;

        public edge(int distance, int s, int d) {
            this.distance = distance;
            this.s = s;
            this.d = d;
        }

        @Override        public String toString() {
            return "edge{" +
                    "distance=" + distance +
                    ", s=" + s +
                    ", d=" + d +
                    '}';
        }
    }

    public static void main(String[] nt){

        int graph[][]= {{0, 2, 0, 6, 0},
                {2, 0, 3, 8, 5},
                {0, 3, 0, 0, 7},
                {6, 8, 0, 0, 9},
                {0, 5, 7, 9, 0},
        };

        boolean[] visited=new boolean[graph.length];
        ArrayList<edge>edgeArrayList=new ArrayList<>();

        for(int i=0;i<graph.length;i++){
            visited[i]=false;
            for(int x=0;x<graph[i].length;x++){
                if(graph[i][x]!=0){
                    edgeArrayList.add(new edge(graph[i][x],i,x));
                }
            }
        }

        Collections.sort(edgeArrayList, new Comparator<edge>() {
            @Override            public int compare(edge edge, edge t1) {
                return edge.distance-t1.distance;
            }
        });
        int count=0;
        edge edgenow;
        int min=0;
        while (count!=graph.length-1){
            edgenow=edgeArrayList.get(0);
            edgeArrayList.remove(0);
            if(visited[edgenow.d]==false || visited[edgenow.s]==false){
                min+=edgenow.distance;
               System.out.println((edgenow.s+1)+" -> "+(edgenow.d+1)+" : "+edgenow.distance);
                visited[edgenow.d]=true;
                visited[edgenow.s]=true;
                count++;
            }
        }
        System.out.println("Cost is : "+min);
    }
}

Output:

Dijkstra’s algorithm.

Tags
Question: Write a program to find shortest path from your home to college using Dijkstra’s algorithm.

Code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Dijkstras {

    static class vertex{
        int distance;
        boolean visited;
        int no;
        int previous;

        @Override        public String toString() {
            return "vertex{" +
                    "distance=" + distance +
                    ", visited=" + visited +
                    ", no=" + (no+1) +
                    ", previous=" + (previous+1) +
                    '}';
        }

        public vertex(int distance, boolean visited, int no, int previous) {
            this.distance = distance;
            this.visited = visited;
            this.no = no;
            this.previous = previous;
        }


    }

    public static void main(String[] nt){
        ArrayList<vertex> arrayList=new ArrayList<>();
        ArrayList<vertex> result=new ArrayList<>();
       /* int graph[][] = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},                {4, 0, 8, 0, 0, 0, 0, 11, 0},                {0, 8, 0, 7, 0, 4, 0, 0, 2},                {0, 0, 7, 0, 9, 14, 0, 0, 0},                {0, 0, 0, 9, 0, 10, 0, 0, 0},                {0, 0, 4, 14, 10, 0, 2, 0, 0},                {0, 0, 0, 0, 0, 2, 0, 1, 6},                {8, 11, 0, 0, 0, 0, 1, 0, 7},                {0, 0, 2, 0, 0, 0, 6, 7, 0}        };       */       int[][] graph={{0,4,8,0,0},{0,0,5,8,10},{0,4,0,0,3},{0,0,0,0,6},{0,0,0,7,0}};
       for (int i=0;i<graph.length;i++){
            arrayList.add(new vertex(Integer.MAX_VALUE,false,i,Integer.MAX_VALUE));
        }

        arrayList.get(0).distance=0;
        arrayList.get(0).previous=0;

        Comparator<vertex> com=new Comparator<vertex>() {
            @Override            public int compare(vertex vertex, vertex t1) {
                return vertex.distance-t1.distance;
            }
        };

        vertex poped;
        while (arrayList.size()!=0){
            Collections.sort(arrayList,com);
            poped=arrayList.get(0);
            arrayList.remove(0);


            if(poped.visited==false){
                for (int i=0;i<graph[poped.no].length;i++){
                    if(graph[poped.no][i]!=0&& find(arrayList,i)!=Integer.MAX_VALUE){
                        int x=find(arrayList,i);
                            if(graph[poped.no][i]+poped.distance<arrayList.get(x).distance){
                                arrayList.get(x).distance=graph[poped.no][i]+poped.distance;
                                arrayList.get(x).previous=poped.no;
                            }
                    }
                }
                poped.visited=true;
                result.add(poped);
            }
        }

        Comparator<vertex>comp2=new Comparator<vertex>() {
            @Override            public int compare(vertex vertex, vertex t1) {
                return vertex.no-t1.no;
            }
        };
        Collections.sort(result,comp2);
        for(int i=0;i<result.size();i++){

            System.out.println(result.get(i).toString());
        }

    }

    static int find(ArrayList<vertex> arrayList,int n){
        int i=Integer.MAX_VALUE;

        for (int x=0;x<arrayList.size();x++){
            if(arrayList.get(x).no==n){
                return x;
            }
        }
        return i;
    }
}

Output:


Sunday, November 5, 2017

Sutherland PolyGon CLipping

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
    if(x2-x1)
        m=(y2-y1)/(x2-x1);
    else
        m=100000;
    if(x1 >= xmin && x2 >= xmin)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(x1 < xmin && x2 >= xmin)
    {
        arr[k]=xmin;
        arr[k+1]=y1+m*(xmin-x1);
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(x1 >= xmin  && x2 < xmin)
    {
        arr[k]=xmin;
        arr[k+1]=y1+m*(xmin-x1);
        k+=2;
    }
}

void clipt(float x1,float y1,float x2,float y2)
{
    if(y2-y1)
        m=(x2-x1)/(y2-y1);
    else
        m=100000;
    if(y1 <= ymax && y2 <= ymax)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(y1 > ymax && y2 <= ymax)
    {
        arr[k]=x1+m*(ymax-y1);
        arr[k+1]=ymax;
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(y1 <= ymax  && y2 > ymax)
    {
        arr[k]=x1+m*(ymax-y1);
        arr[k+1]=ymax;
        k+=2;
    }
}

void clipr(float x1,float y1,float x2,float y2)
{
    if(x2-x1)
        m=(y2-y1)/(x2-x1);
    else
        m=100000;
    if(x1 <= xmax && x2 <= xmax)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(x1 > xmax && x2 <= xmax)
    {
        arr[k]=xmax;
        arr[k+1]=y1+m*(xmax-x1);
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(x1 <= xmax  && x2 > xmax)
    {
        arr[k]=xmax;
        arr[k+1]=y1+m*(xmax-x1);
        k+=2;
    }
}

void clipb(float x1,float y1,float x2,float y2)
{
    if(y2-y1)
        m=(x2-x1)/(y2-y1);
    else
        m=100000;
    if(y1 >= ymin && y2 >= ymin)
    {
        arr[k]=x2;
        arr[k+1]=y2;
        k+=2;
    }
    if(y1 < ymin && y2 >= ymin)
    {
        arr[k]=x1+m*(ymin-y1);
        arr[k+1]=ymin;
        arr[k+2]=x2;
        arr[k+3]=y2;
        k+=4;
    }
    if(y1 >= ymin  && y2 < ymin)
    {
        arr[k]=x1+m*(ymin-y1);
        arr[k+1]=ymin;
        k+=2;
    }
}

void main()
{
    int gdriver=DETECT,gmode,n,poly[20];
    float xi,yi,xf,yf,polyy[20];
    clrscr();
    cout<<"Coordinates of rectangular clip window :\nxmin,ymin             :";
    cin>>xmin>>ymin;
    cout<<"xmax,ymax             :";
    cin>>xmax>>ymax;
    cout<<"\n\nPolygon to be clipped :\nNumber of sides       :";
    cin>>n;
    cout<<"Enter the coordinates :";
    for(int i=0;i < 2*n;i++)
    cin>>polyy[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    for(i=0;i < 2*n+2;i++)
    poly[i]=round(polyy[i]);
    initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
    setcolor(RED);
    rectangle(xmin,ymax,xmax,ymin);
    cout<<"\t\tUNCLIPPED POLYGON";
    setcolor(WHITE);
    fillpoly(n,poly);
  getch();
    cleardevice();
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    n=k/2;
    for(i=0;i < k;i++)
    polyy[i]=arr[i];
    polyy[i]=polyy[0];
    polyy[i+1]=polyy[1];
    k=0;
    for(i=0;i < 2*n;i+=2)
    clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
    for(i=0;i < k;i++)
    poly[i]=round(arr[i]);
    if(k)
    fillpoly(k/2,poly);
    setcolor(RED);
    rectangle(xmin,ymax,xmax,ymin);
    cout<<"\tCLIPPED POLYGON";
    getch();
    closegraph();
}

Output:


This Program is taken from pracspedia.com we at gndecprogramming do not believe in copying but this is done because of less time.....

Mid Point Eclipse Drawing Algoritm

Code:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}


Output:



This Program is Taken from Studentcpu.com, We at gndecprogramming donot believe in copying but this time we did it because of less time .....

Travelling Salesman Problem

Tags
Question:

Code:
Write a program to find minimum route for a newspaper distributer of your locality using Greedy algorithm. 

public class Dmain {

    public static void main(String[] nt){

        int n=5;
        int[][] c={{0,3,Integer.MAX_VALUE,7,Integer.MAX_VALUE,3},{3,0,2,Integer.MAX_VALUE,6,Integer.MAX_VALUE},{Integer.MAX_VALUE,2,0,2,5,Integer.MAX_VALUE},
                {7,Integer.MAX_VALUE,2,0,1,Integer.MAX_VALUE},
                {Integer.MAX_VALUE,6,5,1,0,4},
                {3,Integer.MAX_VALUE,Integer.MAX_VALUE,Integer.MAX_VALUE,4,0}
                    };

        /*int[][] d={                {1,2,3,4},                {5,6,7,8},                {3,4,5,6},                {9,8,4,3}        };*/
        System.out.println("Shorest Path is : "+find(c));
    }

    static StringBuffer find(int[][] c){
        StringBuffer buffer=new StringBuffer();
        boolean [] present=new boolean[c.length];
        for(int i=0;i<c.length;i++){
            present[i]=false;
        }
        int count=1,node=0,cost=0,previous=0;
        present[node]=true;
        while(count!=c.length+1){
            int min=Integer.MAX_VALUE,minindex=0;
            for(int i=0;i<c[node].length;i++){
                if(present[i]==false && min>c[node][i]){
                        min=c[node][i];
                        minindex=i;
                }
            }
            present[minindex]=true;
            if(min!=Integer.MAX_VALUE){ cost+=min;}
            buffer.append((node+1)+"->");
            previous=node;
            node=minindex;
            count++;
        }
        buffer.append("1"+"\nMinimum Cost Is : "+(cost+c[previous][0]));
        return buffer;
    }

}

Output:


Saturday, November 4, 2017

WT 11,12 JavaScript

Tags
Question: 
Create an HTML file to display the various arithmetic operations on variables using JavaScript.

Code:

<html>
<head>
<title>Arithmetic Operators</title>

<style type="text/css">
p{
color: white;
margin:1in;
margin-bottom: 0in;
margin-top: 0in;
text-align: center;
background-color: purple;
font-size: 25px;
font-family: Arial;
}
</style>

<script type="text/javascript">
function add(){
document.form1.result.value=parseInt(document.form1.a.value)+parseInt(document.form1.b.value);
}

function sub(){
document.form1.result.value=parseInt(document.form1.a.value)-parseInt(document.form1.b.value);
}

function mul(){
document.form1.result.value=parseInt(document.form1.a.value)*parseInt(document.form1.b.value);
}

function Div(){
document.form1.result.value=parseInt(document.form1.a.value)%parseInt(document.form1.b.value);
}
</script>
</head>
<body>
<center>
<p>This is a Simple Example  to display the various arithmetic operations on variables using JavaScript</p>
<br>
<br>
<form name="form1" style="font-size:20px;">
Enter First Number  : <input type="number" name="a" placeholder="Enter A : "/><br><br>
Enter Second Number : <input type="number" name="b" placeholder="Enter B : "/><br><br>
<button type="button" onClick="add()">+</button>&nbsp;&nbsp;<button type="button" onclick="sub()">-</button>&nbsp;&nbsp;<button type="button" onclick="mul()">*</button>&nbsp;&nbsp;<button type="button" onclick="Div()">%</button><br><br>
Result              : <input type="number" name="result" placeholder="Result :">
</form>
</center>
</body>
</html> 

Output:



Question:

Create an HTML file to implement the concept of document object model using JavaScript.

Code:
 <html>
<head>
<script type="text/javascript">
function change(){

var a=document.getElementById("Jarvis");
a.innerHTML="This Is Text After Clicking On Below Button";

a.style.color = "blue";
a.style.fontFamily = "Arial";
a.style.fontSize = "larger";
}

</script>


<style type="text/css">
h1{
color: white;
margin:1in;
margin-bottom: 0in;
margin-top: 0in;
text-align: center;
background-color: purple;
font-size: 25px;
font-family: Arial;
}
</style>

</head>

<body>
<h1>This is a simple Example  to implement the concept of document object model using
JavaScript. </h1>

<p id='Jarvis'>Clicking Below Button Will Change The Element Having Id='Jarvis'</p>
<center><button onclick="change()">Click Here</button></center>

</body>

</html>

Output:


WT 13,14,15 PHP Practicals

Tags
Question:
. Create a PHP file to print any text using variable

Code:
<html>

<head>
<?php
$variable="Hello World";
echo "<center><h1>".$variable."</h1></center>";
?>

<style type="text/css">
h1{
color: white;
background-color: purple;
font-size: 50px;
font-family: Arial;
}
</style>
</head>

<body>
<p style="font-size:30px;">This is a Simple Example To Show Text Using Variable in PHP</p>

</body>

</html>

Output:


Question:
 Demonstrate the use of statements, operators and functions in PHP.

Code:
<html>

<head>
<?php

if(isset($_POST['c'])){
$a=$_POST['a'];
$b=$_POST['b'];
$d=$_POST['d'];

operate($a,$b,$d);
}

function operate($a,$b,$d)
{
if(!(empty($a)||empty($b))){
if($d=="Add"){
echo "<html><h1>Result is : ".($a+$b)."</h1></html>";
}
else if($d=="Subtract"){
echo "<html><h1>Result is : ".($a-$b)."</h1></html>";
}
else if($d=="Multiply"){
echo "<html><h1>Result is : ".($a*$b)."</h1></html>";
}
else if($d=="Modulus"){
echo "<html><h1>Result is : ".($a%$b)."</h1></html>";
}
}else{
echo "All Fields are Mandatory";
}
}

?>

<style type="text/css">
h1{
color: white;
background-color: purple;
font-size: 50px;
font-family: Arial;
}
</style>
</head>

<body>
<p style="font-size:30px; text-align:center;">This is a Simple Example To Demonstrate the use of statements, operators and functions in PHP</p>

<center>
<form action="wt13.php" method="post">
<input type="number" name="a" placeholder="Enter A : ">
<input type="number" name="b" placeholder="Enter B : ">
<select name="d">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Modulus</option>
</select>
<input type="submit" value="Submit" name="c">
</form>
</center>

</body>

</html>

Output:

Question:
Demonstrate the use of Loops and arrays in PHP

Code:

<html>

<head>
<?php
if(isset($_POST['c']))
{

$array=array();
$a=$_POST['a'];

for($i=1;$i<=10;$i++){
$array[$i]=$a*$i;
// echo $a." * ".$i." = ".($a*$i);
}


for($i=1;$i<=10;$i++){
echo "<h1>",$a." * ".$i." = ".$array[$i]."</h1><br>";
}
}



?>

<style type="text/css">
h1{
color: white;
margin:1in;
margin-bottom: 0in;
margin-top: 0in;
text-align: center;
background-color: purple;
font-size: 30px;
font-family: Arial;
}
</style>
</head>

<body>
<p style="font-size:30px; text-align:center;">This is a Simple Example To Demonstrate the use of Loops and arrays in PHP</p>

<center>
<form action="wt13.php" method="post">
<input type="number" name="a" placeholder="Enter Any Number : " required>
<input type="submit" value="Submit" name="c">
</form>
</center>

</body>

</html>

Output:

Transformation Computer Graphics

Question:
Write a program to draw any 2-D object and perform the transformations on it according to the input parameters from the user, namely: Translation, Rotation or Scaling

Code:

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main(){

    int g=DETECT,gmode;
    initgraph(&g,&gmode,"C://TURBOC3//BGI");
    line(200,300,300,200);
    int x;
    cout<<"Enter Your Choice\nPress 1 For Translation\n2 For Rotation\n3 For Scaling\n";
    cin>>x;

    if(x==1){
cleardevice();
int tx,ty;
line(200,300,300,200);
cout<<"Enter Translation Along x and y\n";
cout<<"Tx =";
cin>>tx;
cout<<"Ty =";
cin>>ty;
line(200+tx,300+ty,300+tx,200+ty);
    }else if(x==2){
cleardevice();
line(200,300,300,200);
cout<<"Enter Rotation Angle:\n";
cout<<"a=";
float a;
cin>>a;
a=a*(3.142/180);
cout<<200*cos(a)-300*sin(a)<<","<<200*sin(a)+300*cos(a)<<","<<300*cos(a)-200*sin(a)<<","<<300*sin(a)+200*cos(a);
//line((int)200*cos(a)-300*sin(a),(int)200*sin(a)+300*cos(a),(int)300*cos(a)-200*sin(a),(int)300*sin(a)+200*cos(a));
line(200*cos(a)-300*sin(a),200*sin(a)+300*cos(a),300*cos(a)-200*sin(a),300*sin(a)+200*cos(a));
    }else{
cleardevice();
float sx,sy;
line(200,300,300,200);
cout<<"Enter Scaling along x and y\n";
cout<<"Sx =";
cin>>sx;
cout<<"Sy =";
cin>>sy;
line(200*sx,300*sy,300*sx,200*sy);
    }

    getch();
}

Output:

Wednesday, November 1, 2017

Wt Practical 8,9,10

Tags
Code:

<html>
<head>
<link rel="stylesheet" type="text/css" href="Wt8.css">
<style type="text/css">
h2{
font-size:40px;
font-family: Arial;
text-align: center;
}
</style>
</head>

<body>
<h1 style="font-size:50px; text-align:left; font-family:TimesNewRoman; ">Inline Style</h1>
<h2>Internal Style</h2>
<h3>External Style</h3>
</body>
</html>


Wt8.css:

h3{
font-size: 40px;
font-family: Courier;
text-align: right;
}


Output:
Code:
<html>
<head>
<style type="text/css">
a{
font-family:Helvetica;
font-size: 30px;
}
a:visited{
color: red;
}

a:link{
color: yellow;
}

a:hover{
color: green;
font-size: 40px;
}
p{
text-align: center;
font-size: 50px;
font-family: Arial;
color: blue;
}
</style>
</head>
<body>

<p>Click On Below Links And You Will Be Redirected</p>
<ul type="square">
<li><a href="https://www.google.co.in/?q=hello U">iPhoneX</a></li>
<li><a href="https://www.google.co.in/">iPhone8</a></li>
<li><a href="https://www.google.co.in/?q=bye U">iPhone7</a></li>
</ul>


</body>
</html>

Output:

Code:

<html>
<head>
<style type="text/css">
table th{
background-color: purple;
color: white;
}

table tr{
font-family: Arial;
font-size: 20px;
}
table tr:hover{
background-color: green;
}

ul{
list-style: squares;
}
ul li{
font-size: 20px;
font-family: Arial;
}
ul li:hover{
color: red;
}
</style>
</head>
<body>

<center><h3>Key Specs iPhone7</h3>
<table border="2" cellpadding="5" cellspacing="5">
<tr><td>Display</td><td>Processor</td><td>Front Camera</td></tr>
<tr><th>4.70-inch</th><th>quad core</th><th>7-megapixel</th></tr>
<tr></tr>
<tr><td>Resolution</td><td>OS</td><td>Storage</td></tr>
<tr><th>750x1334 pixels</th><th>iOS 10</th><th>32GB</th></tr>
<tr></tr>
<tr><td>Rear Camera</td><td>Price</td></tr>
<tr><th>12-megapixel</th><th>Rs. 45,999</th></tr>
</table>
</center>
<br>
<ul>
<li>iPhone7</li>
<li>iPhone7+</li>
<li>iPhone8</li>
<li>iPhone8 plus</li>
<li>iPhoneX</li>
</ul>



</body>
</html>

Output: