Assignment No.1 1. Objective

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

ASSIGNMENT NO.1

1. OBJECTIVE :-
To obtain the shortest distance between every two pair of
vertices of the following weighted graph.

2. SOURCE CODE :-
#include<stdio.h>
#include<conio.h>
#define inf 9999
typedef struct
{
int val;
int status;
}graph;
int min(int,int);

void main()
{
clrscr();
int a[20][20],n,i,j,h,s,d,start,end,p,c=0;
char ch;
graph g[10][10];
int flag[10],path[10];
clrscr();
printf("Enter the number of vertices: ");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i][i]=0;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
if(i!=j)
{
printf("\nVertex %d to Vertex %d:\n",i+1,j+1);
printf("Is there any edge(y/n)? ");
fflush(stdin);ch=getche();
printf("\n");
if(ch=='y' || ch=='Y')
a[i][j]=1;
else
a[i][j]=inf;
a[j][i]=a[i][j];
}
}
printf("\nEnter the starting vertex : ");
scanf("%d",&s);start=s-1;
printf("Enter the destination vertex : ");
scanf("%d",&d);end=d-1;
for(i=0;i<n;i++)
{
g[0][i].val=inf;
g[0][i].status=0;
flag[i]=0;
}
g[0][start].val=0;g[0][start].status=1;flag[start]=1;
for(i=1;flag[end]!=1;i++)
{
for(j=0,h=inf;j<n;j++)
{
if(flag[j]==0)
2

{
g[i][j].val=min(g[i-1][j].val,g[i-
1][start].val+a[start][j]);
if(g[i][j].val<=h)
{
h=g[i][j].val;
p=j;
}
}
else
g[i][j].val=g[i-1][j].val;
}
g[i][p].status=1;
flag[p]=1;
start=p;
}
printf("\n\nShortest Path length = %d",g[i-1][end].val);
path[c++]=end;p=end;
for(j=i-2;j>=0;j--)
if(g[j][p].val!=g[j+1][p].val)
{
for(int k=0;k<n && g[j][k].status!=1;k++);
path[c++]=k;p=k;
}
printf("\nShortest Path: ");
for(i=c-1;i>=0;i--)
printf("V%d,",path[i]+1);
getch();
}

int min(int x,int y)


{
return((x<y)?x:y);
}

3. OUTPUT
Enter the number of vertices: 4

Vertex 1 to Vertex 2:
Is there any edge(y/n)? y
Vertex 1 to Vertex 3:
Is there any edge(y/n)? n
Vertex 1 to Vertex 4:
Is there any edge(y/n)? y
Vertex 2 to Vertex 3:
Is there any edge(y/n)? y
Vertex 2 to Vertex 4:
Is there any edge(y/n)? n
Vertex 3 to Vertex 4:
Is there any edge(y/n)? y
Enter the starting vertex : 1
Enter the destination vertex : 3

Shortest Path length = 2


Shortest Path: V1,V4,V3

4. DISCUSSIONS :-
This algorithm uses the Dijkstra’s method to find the shortest path.

*********************
3

ASSIGNMENT NO.2

1. OBJECTIVE :- Program in java to find IP of a computer.

2. SOURCE CODE :-
import java.net.InetAddress;
import java.lang.*;
import java.io.*;
import java.lang.Exception;
public class IpTrap{
public static void main(String args[]){
try{
InetAddress addr = InetAddress.getByName(args[0]);
System.out.println("IP Address:: "+addr);
System.out.println("Host Name ::" +addr.getHostName());
}
catch(Exception e){
System.err.println("Can’t detect local host:: "+e);
}
}
}

3. OUTPUT :-

3.1 V1
IpTrap.java:9: cannot resolve symbol
symbol : class InetAddress
location : class IpTrap
InetAddress localaddr = InetAddress.getLocalHost();
^
3.2 V2
IpTrap.java:9: cannot resolve symbol
symbol : variable InetAddress
location : class IpTrap
InetAddress localaddr = InetAddress.getLocalHost();

3.2 V3
F:\Assignments\Java>java IpTrap home-3bb4cb7b40
IP Address:: home-3bb4cb7b40/10.23.55.36
Host Name ::home-3bb4cb7b40

****************************
4

ASSIGNMENT NO. 3

1. OBJECTIVE :- Write a program in Java to send a text message to


another computer.

2. SOURCE CODE :-
//Server Program
import java.net.*;
import java.lang.*;
import java.io.*;
public class server {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(2000);
System.out.println("Server Starting.........");
Socket sock=s.accept();
BufferedReader datain=new BufferedReader(new
InputStreamReader(sock.getInputStream()));
System.out.println("Incoming message to Server :
"+datain.readLine());
sock.close();
s.close();
}
}

//Client Program
import java.io.*;
import java.net.*;
import java.lang.*;
public class client {
public static void main(String[] args) throws IOException{
Socket sock=new Socket(InetAddress.getByName("home-
3bb4cb7b40"),2000);
String message="Client touches the Server.........";
System.out.println("Sending the message from Client :
"+message);
BufferedWriter dataout=new BufferedWriter(new
OutputStreamWriter(sock.getOutputStream()));
dataout.write(message,0,message.length());
dataout.flush();
sock.close();
}
}

3. OUTPUT :-

Server Starting.........

Sending the message from Client : Client touches the Server.........

Incoming message to Server : Client touches the Server.........

****************************
5

ASSIGNMENT NO. 4

1. OBJECTIVE :- Write a program to send message to another computer


until ‘exit’ keyword is entered.

2. SOURCE CODE :-

//Server Program.
import java.net.*;
import java.lang.System;
import java.io.*;
public class msgserver {
public static void main(String[] args) throws IOException {
ServerSocket s=new ServerSocket(2000);
Socket sock;
BufferedReader datain;
String str;
System.out.println("Server Starting..............");
System.out.println("Incoming Message from Client :");
sock=s.accept();
datain=new BufferedReader(new
InputStreamReader(sock.getInputStream()));
str=datain.readLine();
System.out.println(str);
sock.close();
s.close();
}
}

//Client program.
import java.io.*;
import java.net.*;
import java.lang.*;
public class msgclient {
public static void main(String[] args) throws IOException{
Socket sock=new Socket(InetAddress.getByName("home-
3bb4cb7b40"),2000);
String message;
BufferedWriter dataout=new BufferedWriter(new
OutputStreamWriter(sock.getOutputStream()));
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Start entering the message to send : ");
do {
message=br.readLine();
dataout.write(message,0,message.length());
dataout.flush();
}while(!message.equalsIgnoreCase("exit"));
sock.close();
}
}
6

3. OUTPUT :-

Server Starting..............

Start entering the message to send :


Java Rules the Web!
It is the Best Programming Language.
exit

Incoming Message to Server :


Java Rules the Web! It is the Best Programming Language.

You might also like