import java.io.*;
import java.sql.*;
public class sql{
public static void main(String args[]) throws ClassNotFoundException,SQLException
{
Class.forName("oracle.jdbc.OracleDriver") ;
System.out.println("Oracle JDBC driver loaded ok.");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:cs359/cs359@192.168.1.198:1521:orcl");
System.out.println("Connected with @192.168.1.198:1521:orcl");
con.setAutoCommit(false);
Statement stmt = con.createStatement();
ResultSet rset =null;
rset = stmt.executeQuery("select * from emp");
while (rset.next())
System.out.println (rset.getString(1));
rset.close();
stmt.close();
System.out.println ("Ok.");
}
}
I KRISHNAPRASAD PURSUING MY B.TECH IN C.R.REDDY COLLEGE OF ENGINEERING TRADE COMPUTER SCIENCES AND ENGINEERING. I'M HAPPY TO PRESENT ALL TYPE OF PROGRAMS. I HOPE U UTILISE THIS BLOG AND ENJOY YOUR PROGRAMMING. THESE PROGRAMS ARE BEST COMPILED IN TURBO C++ COMPILERS AND SUN JAVA COMPILERS
Thursday, November 25, 2010
SQL-JAVA
PRODUCER CONSUMER
Producer Consumer Problem:
/*To avoid polling, Java includes an elegant interprocess communication mechanism via the wait( ), notify( ), and notifyAll( ) methods. These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized method. Although conceptually advanced from a computer science perspective, the rules for using these methods are actually quite simple:
1) wait( ) tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
2) notify( ) wakes up the first thread that called wait( ) on the same object.
3) notifyAll( ) wakes up all the threads that called wait( ) on the same object. The highest priority thread will run first.*/
class Q
{
int n;
boolean valueSet = false;
synchronized int get()
{
if(!valueSet)
try {
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
if(valueSet)
try {
wait();
} catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);
}
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
while(true) {
q.get();
}
}
}
class prodcon {
public static void main(String args[]) {
Q q = new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-C to stop.");
}
}
/*inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready. When this happens, execution inside get( ) resumes. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue. Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it.*/
SERVER-CLIENT
Server:
import java.io.*;
import java.net.*;
public class server
{
public static void main(String a[])throws Exception
{
String message;
message="hai";
System.out.println("waiting for the connection");
ServerSocket ss=new ServerSocket(3000);
Socket s=ss.accept();
try
{
DataInputStream in1=new DataInputStream(s.getInputStream());
DataOutputStream out1=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while(!message.equals("bye"))
{
message=in1.readUTF();
System.out.println("client>"+message);
System.out.print("server>");
message=br.readLine();
out1.writeUTF(message);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("connection terminated");
ss.close();
}
}
}
Client:
import java.io.*;
import java.net.*;
public class client
{
public static void main(String a[])throws Exception
{
String message,message1;
message="hai";message1="hai";
Socket s=new Socket("localhost",3000);
try
{
DataInputStream in1=new DataInputStream(s.getInputStream());
DataOutputStream out1=new DataOutputStream(s.getOutputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.print("client>");
message=br.readLine();
out1.writeUTF(message);
message1=in1.readUTF();
System.out.println("server>"+message1);
}while(!message.equals("bye"));
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("connection terminated");
s.close();
}
}
}
FREE SPACE MANAGEMENT
/* Freespace */
#include<stdio.h>
struct memory
{
int size;
int empty;
}m[20];
int j[20];
int nm,nj;
void asort();
void dsort();
void fit();
void display(int[]);
main()
{
int i,ch,njx;
printf("enter no of memory slots \n");
scanf("%d",&nm);
printf("enter memory slots");
for(i=0;i<nm;i++)
{
scanf("%d",&m[i].size);
m[i].empty=m[i].size;
}
printf("enter no of jobs\n");
scanf("%d",&nj);
printf("enter the jobs\n");
for(i=0;i<nj;i++)
{
printf("enter the job size of j:%d",i+1);
scanf("%d",&j[i]);
}
printf("various types of placement algorithms\n");
printf("1.first fit\n2.best fit\n3.worst fit\n4.readmore\n5.exit\n");
while(1)
{
place:
printf("enter your choice for placing:");
scanf("%d",&ch);
switch(ch)
{
case 1:fit(ch);
break;
case 2:fit(ch);
break;
case 3:fit(ch);
break;
case 4:printf("enter no of more jobs\n");
scanf("%d",&njx);
i=nj;
nj=njx;
printf("enter the job\n");
for( ;i<nj;i++)
scanf("%d",&j[i]);
goto place;
case 5:exit(0);
default:printf("invalid choice");
}
}
}
void fit(int ch)
{
int i,k;
int jm[20];
struct memory tm[20];
for(i=0;i<nm;i++)
tm[i]=m[i];
for(i=0;i<nj;i++)
{
jm[i]=0;
if(ch==2)
asort();
if(ch==3)
dsort();
for(k=0;k<nm;k++)
{
if(j[i]<m[k].empty)
{
jm[i]=m[k].size;
m[k].empty=j[i];
break;
}
}
}
for(i=0;i<nm;i++)
m[i]=tm[i];
display(jm);
}
void display(int jm[20])
{
int i,k,use,nf;
printf("job placement\n");
for(i=0;i<nj;i++)
{
printf("%d",j[i]);
if(jm[i]!=0)
printf("\t %d",jm[i]);
else
printf("\t can not be placed");
printf("\n");
}
printf("the remaining free slots are\n");
for(i=0;i<nm;i++)
{
for(k=0,use=0;k<nf;k++)
if(jm[k]==m[i].size)
use+=j[k];
printf("%5d",m[i].size-use);
}
printf("\n");
}
void asort()
{
int i,k;
struct memory t;
for(i=0;i<nm;i++)
for(k=0;k<(nm-i-1);k++)
if(m[k].empty>m[k+1].empty)
{
t=m[k];
m[k]=m[k+1];
m[k+1]=t;
}
}
void dsort()
{
int i,k;
struct memory t;
for(i=0;i<nm;i++)
for(k=0;k<(nm-i-1);k++)
if(m[k].empty<m[k+1].empty)
{
t=m[k];
m[k]=m[k+1];
m[k+1]=t;
}
}
SJF SCHEDULING ALG
/* SJF */
#include<stdio.h>
struct process
{
int id,w,b,ta;
}p[10];
void sort(int);
main()
{
int n,i,sw,sta;
float aw,ata;
printf("enter no of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("burst time for p %d: ",i+1);
scanf("%d",&p[i].b);
p[i].id=i+1;
}
sort(n);
p[0].w=0,p[0].ta=p[0].b;
sw=p[0].w;
sta=p[0].ta;
for(i=1;i<n;i++)
{
p[i].w=p[i-1].w+p[i-1].b;
p[i].ta=p[i-1].ta+p[i].b;
sw+=p[i].w;
sta+=p[i].ta;
}
aw=sw/(float)n;
ata=sta/(float)n;
printf("processes burst time wt tat \n");
for(i=0;i<n;i++)
{
printf("p %d",p[i].id);
printf("\t \t %d",p[i].b);
printf("\t \t %d",p[i].w);
printf("\t \t %d",p[i].ta);
printf("\n");
}
printf("average waiting time =%0.2f \n",aw);
printf("average TAT=%0.2f \n",ata);
}
void sort(int n)
{
int i,j;
struct process t;
for(i=0;i<n;i++)
for(j=0;j<(n-i-1);j++)
{
if(p[j].b>p[j+1].b)
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
ROUNDROBIN SCHEDULING ALG
/* Round Robin */
#include<stdio.h>
struct process
{
int id,b,w,ta;
}p[10];
main()
{
int rb[10],tq,n,i,d,j;
int sw,sta,st;
float aw,ata;
printf("enter time quantum: ");
scanf("%d",&tq);
printf("enter no.of processes: ");
scanf("%d",&n);
st=0;
for(i=0;i<n;i++)
{
printf("enter burst time for p%d: ",i+1);
scanf("%d",&p[i].b);
st+=p[i].b;
rb[i]=p[i].b;
p[i].id=i+1;
p[i].w=p[i].ta=0;
}
for(i=0;i<st;i++)
for(j=0;j<n;j++)
{
if(rb[j]<=tq&&rb[j]!=0)
{
p[j].w+=i;
i+=rb[j];
p[j].ta=i;
}
if(rb[j]>tq)
{
p[j].w+=i;
i+=tq;
rb[j]-=tq;
p[j].w-=i;
}
}
sw=sta=0;
for(i=0;i<n;i++)
{
sw+=p[i].w;
sta+=p[i].ta;
}
aw=sw/(float)n;
ata=sta/(float)n;
printf("process burst time waiting time turn around time\n");
for(i=0;i<n;i++)
{
printf("p%d",p[i].id);
printf("\t\t%d",p[i].b);
printf("\t\t%d",p[i].w);
printf("\t\t%d",p[i].ta);
printf("\n");
}
printf("Average waiting time=%0.2f\n",aw);
printf("Average turn around time=%0.2f\n",ata);
}
OPTIMAL PAGE REPLACEMENT ALG
/* Optimal */
#include<stdio.h>
int str[30],sl;
float placement(int);
main()
{
int nfs,fs[10];
int i,j,chk;
float pfr[10];
printf("enter the length of string: ");
scanf("%d",&sl);
printf("enter the string: ");
for(i=0;i<sl;i++)
scanf("%d",&str[i]);
printf("enter the no.of frames: ");
scanf("%d",&nfs);
printf("enter frame sizes: ");
for(i=0;i<nfs;i++)
scanf("%d",&fs[i]);
for(i=0;i<nfs;i++)
{
printf("\npage replacement for frame size: %d\n",fs[i]);
pfr[i]=placement(fs[i]);
}
for(i=0;i<nfs;i++)
for(j=0,chk=0;j<nfs;j++)
{
if(fs[i]>fs[j])
chk=1;
if(chk==1&&pfr[i]<pfr[j])
printf("\nBELADY'S ANOMALY occurred between frame sizes %d&%d\n",fs[i],fs[j]);
}
}
float placement(int fs)
{
int i,j,k,p;
int f[10],t[10];
int par[10][20];
int pf=0;
float pfr;
for(i=0;i<fs;i++)
f[i]=-1;
for(i=0;i<sl;i++)
{
p=-1;
for(j=0;j<fs;j++)
if(str[i]==f[j])
break;
if(j!=fs)
continue;
for(j=0;j<fs;j++)
if(f[j]==-1)
{
p=j;
break;
}
if(p==-1)
{
for(j=i+1;j<sl;j++)
{
for(k=0;k<fs;k++)
if(t[k]==0&&str[j]==f[k])
{
t[k]++;
break;
}
if(k!=fs);
{
for(k=0;k<fs;k++)
if(t[k]!=0)
t[k]++;
}
}
p=lesser(t,fs);
}
f[p]=str[i];
pf++;
for(j=0;j<fs;j++)
par[j][pf-1]=f[j];
}
for(i=0;i<fs;i++)
{
for(j=0;j<fs;j++)
printf("%5d",par[i][j]);
printf("\n");
}
pfr=((float)pf/sl)*100;
printf("no.of page faults=%d",pf);
printf("\npage fault rate=%0.3f\n",pfr);
return pfr;
}
int lesser(int t[10],int fs)
{
int i,j,c;
for(i=0;i<fs;i++)
{
c=0;
for(j=0;j<fs;j++)
if(t[i]<=t[j])
c++;
if(c==fs)
break;
}
return i;
}
LRU PAGE REPLACEMENT ALG
/* LRU */
#include<stdio.h>
int str[30],sl;
float placement(int);
main()
{
int nfs,fs[30];
int i,j,chk;
float pfr[10];
printf("enter the length of string:");
scanf("%d",&sl);
printf("enter the string\n");
for(i=0;i<sl;i++)
scanf("%d",&str[i]);
printf("enter number of frame sizes\n");
scanf("%d",&nfs);
printf("enter frame sizes\n");
for(i=0;i<nfs;i++)
scanf("%d",&fs[i]);
for(i=0;i<nfs;i++)
{
printf("\npage replacement for frame size= %d\n",fs[i]);
pfr[i]=placement(fs[i]);
}
for(i=0;i<nfs;i++)
for(j=0,chk=0;j<nfs;j++)
{
if(fs[i]<fs[j])
chk=1;
if(chk==1&&pfr[i]<pfr[j])
printf("\nanamoly occured between frame sizes %d&%d\n",fs[i],fs[j]);
}
}
float placement(int fs)
{
int i,j,k,p;
int f[10],t[10];
int par[10][20];
int pf=0;
float pfr;
for(i=0;i<fs;i++)
{
f[i]=-1;
t[i]=0;
}
for(i=0;i<sl;i++)
{
p=-1;
for(j=0;j<fs;j++)
if(str[i]==f[j])
break;
if(j!=fs)
continue;
for(j=0;j<fs;j++)
if(f[j]==-1)
{
p=j;
break;
}
if(p==-1)
{
for(j=0;j<i;j++)
{
for(k=0;k<fs;k++)
{
if(str[j]==f[k])
{
t[k]=0;
break;
}
}
for(k=0;k<fs&&k<=i;k++)
t[k]++;
}
p=greater(t,fs);
}
f[p]=str[i];
pf++;
for(j=0;j<fs;j++)
par[j][pf-1]=f[j];
}
for(i=0;i<fs;i++)
{
for(j=0;j<pf;j++)
printf("%5d",par[i][j]);
printf("\n");
}
pfr=((float)pf/sl)*100;
printf("no of page faults =%d",pf);
printf("\n page fault rate=%0.3f\n",pfr);
return pfr;
}
int greater(int t[10],int fs)
{
int i,j,c;
for(i=0;i<fs;i++)
{
c=0;
for(j=0;j<fs;j++)
if(t[i]>=t[j])
c++;
if(c==fs)
break;
}
return i;
}
FIFO PAGE REPLACEMENT ALG
/*FIFO*/
#include<stdio.h>
int str[30],sl;
float placement(int);
main()
{
int nfs,fs[10];
int i,j,chk;
float pfr[10];
printf("enter the length of the string: ");
scanf("%d",&sl);
printf("enter the string: ");
for(i=0;i<sl;i++)
scanf("%d",&str[i]);
printf("enter the no.of frmae sizes\n");
scanf("%d",&nfs);
printf("enter frame sizes: ");
for(i=0;i<nfs;i++)
scanf("%d",&fs[i]);
for(i=0;i<nfs;i++)
{
printf("\npage replacement for frame size: %d ",fs[i]);
pfr[i]=placement(fs[i]);
}
for(i=0;i<nfs;i++)
{
for(j=0,chk=0;j<nfs;j++)
{
if(fs[i]>fs[j])
chk=sl;
if(chk==1&&pfr[i]<pfr[j])
printf("\nanomoly occured between frame sizes %d %d\n",fs[i],fs[j]);
}
}
}
float placement(int fs)
{
int i,j,p;
int f[10],t[10];
int par[10][20];
int pf=0;
float pfr;
for(i=0;i<fs;i++)
{
f[i]=-1;
t[i]=0;
}
for(i=0;i<sl;i++)
{
p=-1;
for(j=0;j<fs;j++)
if(str[i]==f[j])
break;
if(j!=fs)
continue;
for(j=0;j<fs;j++)
if(f[j]==-1)
{
p=j;
break;
}
if(p==-1)
p=greater(t,fs);
f[p]=str[i];
t[p]=0;
pf++;
for(j=0;j<fs&&j<=i;j++)
t[j]++;
for(j=0;j<fs;j++)
par[j][pf-1]=f[j];
}
for(i=0;i<fs;i++)
{
for(j=0;j<pf;j++)
printf("%5d",par[i][j]);
printf("\n");
pfr=((float)pf/sl)*100;
printf("no.of page faults %d\n",pf);
printf("page faults rate :%0.3f\n",pfr);
return pfr;
}
}
int greater(int t[10],int fs)
{
int i,j,c;
for(i=0;i<fs;i++)
{
c=0;
for(j=0;j<fs;j++)
if(t[i]>=t[j])
c++;
if(c==fs)
break;
}
return i;
}
FCFS SCHEDULING ALG
/* FCFS */
#include<stdio.h>
main()
{
int a[10],b[10],p[10],wr[10],rr[10],f[10],w[10],r[10],ta[10];
int i,n,sw,sr,sta;
float aw,ar,ata;
printf("enter no of processes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter burst time for p %d: ",i+1);
scanf("%d",&b[i]);
printf("enter arrival time for p %d: ",i+1);
scanf("%d",&a[i]);
p[i]=i+1;
}
wr[0]=rr[0]=a[0];
f[0]=b[0]+a[0];
for(i=1;i<n;i++)
{
wr[i]=wr[i-1]+b[i-1];
rr[i]=rr[i-1]+b[i-1];
f[i]=f[i-1]+b[i];
}
for(i=0;i<n;i++)
{
w[i]=wr[i]-a[i];
r[i]=rr[i]-a[i];
ta[i]=f[i]-a[i];
}
sw=sr=sta=0;
for(i=0;i<n;i++)
{
sw+=w[i];
sr+=r[i];
sta+=ta[i];
}
aw=sw/(float)n;
ar=sr/(float)n;
ata=sta/(float)n;
printf("process burst time arrival time waiting time response time turn raound time \n");
for(i=0;i<n;i++)
{
printf("p %d",p[i]);
printf("\t \t %d",b[i]);
printf("\t \t %d",a[i]);
printf("\t \t %d",w[i]);
printf("\t \t %d",r[i]);
printf("\t \t %d",ta[i]);
printf("\n");
}
printf("average waiting time = %f \n",aw);
printf("average response time = %f \n",ar);
printf("average turn around time = %f \n",ata);
}
DEAD LOCK DETECTION
/* Dead Lock */
#include<stdio.h>
main()
{
int i,j,k,l,b[20],s,p,m[20][20],a[20][20],n[20][20];
int w[12],flag=0,c[30],count=0;
printf("enter number of processes\n");
scanf("%d",&p);
printf("enter number of resources\n");
scanf("%d",&s);
printf("enter the allocation matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&a[i][j]);
printf("enter maximum matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&m[i][j]);
printf("enter available matrix\n");
for(j=0;j<s;j++)
{
scanf("%d",&b[j]);
w[j]=b[j];
}
for(i=0;i<p;i++)
for(j=0;j<s;j++)
n[i][j]=m[i][j]-a[i][j];
printf("allocation matrix \t\t max\t\t need\n");
for(j=0;j<p;j++)
{
for(k=0;k<s;k++)
printf("%d\t",a[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",m[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",n[j][k]);
printf("\n");
}
printf("request is :\n");
for(i=0;i<p;i++)
c[i]=0;
for(l=0;l>p;l++)
{
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<s;j++)
if(n[i][j]>w[j])
flag=1;
if(flag!=1)
{
if(c[i]!=-1)
{
for(k=0;k<s;k++)
w[k]+=a[i][k];
printf("p[%d]\t",i);
count++;
c[i]=-1;
}
}
}
}
if(count!=p)
{
printf("deadlock is occured\n");
}
else
{
printf("safe sequence\n");
}
}
~
~
~
~ /* Dead Lock */
#include<stdio.h>
main()
{
int i,j,k,l,b[20],s,p,m[20][20],a[20][20],n[20][20];
int w[12],flag=0,c[30],count=0;
printf("enter number of processes\n");
scanf("%d",&p);
printf("enter number of resources\n");
scanf("%d",&s);
printf("enter the allocation matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&a[i][j]);
printf("enter maximum matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&m[i][j]);
printf("enter available matrix\n");
for(j=0;j<s;j++)
{
scanf("%d",&b[j]);
w[j]=b[j];
}
for(i=0;i<p;i++)
for(j=0;j<s;j++)
n[i][j]=m[i][j]-a[i][j];
printf("allocation matrix \t\t max\t\t need\n");
for(j=0;j<p;j++)
{
for(k=0;k<s;k++)
printf("%d\t",a[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",m[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",n[j][k]);
printf("\n");
}
printf("request is :\n");
for(i=0;i<p;i++)
c[i]=0;
for(l=0;l>p;l++)
{
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<s;j++)
if(n[i][j]>w[j])
flag=1;
if(flag!=1)
{
if(c[i]!=-1)
{
for(k=0;k<s;k++)
w[k]+=a[i][k];
printf("p[%d]\t",i);
count++;
c[i]=-1;
}
}
}
}
if(count!=p)
{
printf("deadlock is occured\n");
}
else
{
printf("safe sequence\n");
}
}
~
~
~
~ /* Dead Lock */
#include<stdio.h>
main()
{
int i,j,k,l,b[20],s,p,m[20][20],a[20][20],n[20][20];
int w[12],flag=0,c[30],count=0;
printf("enter number of processes\n");
scanf("%d",&p);
printf("enter number of resources\n");
scanf("%d",&s);
printf("enter the allocation matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&a[i][j]);
printf("enter maximum matrix\n");
for(i=0;i<p;i++)
for(j=0;j<s;j++)
scanf("%d",&m[i][j]);
printf("enter available matrix\n");
for(j=0;j<s;j++)
{
scanf("%d",&b[j]);
w[j]=b[j];
}
for(i=0;i<p;i++)
for(j=0;j<s;j++)
n[i][j]=m[i][j]-a[i][j];
printf("allocation matrix \t\t max\t\t need\n");
for(j=0;j<p;j++)
{
for(k=0;k<s;k++)
printf("%d\t",a[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",m[j][k]);
printf("\t");
for(k=0;k<s;k++)
printf("%d\t",n[j][k]);
printf("\n");
}
printf("request is :\n");
for(i=0;i<p;i++)
c[i]=0;
for(l=0;l>p;l++)
{
for(i=0;i<p;i++)
{
flag=0;
for(j=0;j<s;j++)
if(n[i][j]>w[j])
flag=1;
if(flag!=1)
{
if(c[i]!=-1)
{
for(k=0;k<s;k++)
w[k]+=a[i][k];
printf("p[%d]\t",i);
count++;
c[i]=-1;
}
}
}
}
if(count!=p)
{
printf("deadlock is occured\n");
}
else
{
printf("safe sequence\n");
}
}
TEMPLATES-BUBBLE SORT
/* Bubble Sort Using Templets */
#include<iostream>
using namespace std;
template<class T>
void bubble(T a[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template <class x>
void swap(x *a,x*b)
{
x temp;
temp=&a;
&a=&b;
&b=temp;
}
int main()
{
int x[5],i,j;
float y[5];
cout<<"enter integer array:\n";
for(i=0;i<5;i++)
cin>>x[i];
cout<<"enter float array:\n";
for(j=0;j<5;j++)
cin>>y[j];
bubble(x,5);
bubble(y,5);
cout<<"sorted x-array \n";
for(i=0;i<5;i++)
cout<<x[i]<<" "<<"\n";
cout<<"sorted y-array \n";
for(j=0;j<5;j++)
cout<<y[j]<<" "<<"\n";
return(0);
}
STRING OPERATIONS C++
/* String Operation By Overloading operator */
#include<string.h>
#include<iostream>
using namespace std;
class str
{
char *p;
int len;
public:
str()
{
len=0;
p=0;
}
str(const char *s);
str(const str &s);
friend str operator+(const str &s,const str &t);
friend int operator<(const str &s,const str &t);
friend int operator>(const str &s,const str &t);
friend int operator==(const str &s,const str &t);
friend void show(const str &s);
};
str::str(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
str::str(const str &s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}
int operator<(const str&s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m<n)
return(1);
else
return(0);
}
int operator>(const str &s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m>n)
return(1);
else
return(0);
}
int operator==(const str &s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m==n)
return(1);
else
return(0);
}
void show(const str &s)
{
cout<<s.p;
}
str operator+(const str &s,const str &t)
{
str temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
}
int main()
{
str t1,t2,t3;
int ch;
char s1[100],s2[100];
cout<<"enter strings s1,s2\n";
cin>>s1>>s2;
t1=s1;
t2=s2;
cout<<"first string =";
show(t1);
cout<<"\n";
cout<<"second string =";
show(t2);
cout<<"\n";
cout<<"1.comparision \n2.concatenation \n3.exit\n";
do
{
cout<<"enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
if(t1<t2)
{
show(t1);
cout<<" smaller than ";
show(t2);
cout<<endl;
}
else if(t1>t2)
{
show(t1);
cout<<" greater than ";
show(t2);
cout<<endl;
}
else
{
show(t1);
cout<<" equal to ";
show(t2);
cout<<endl;
}
break;
case 2:
t3=t1+t2;
cout<<"concatenation of two strings\n";
show(t3);
cout<<endl;
break;
case 3:
exit(0);
default:
cout<<"invalid choice\n";
}
}
while(1);
return(0);
}
C++ FILE COPY
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char source[100],target[100];
char ch;
cout<<"enter source filename\n";
cin>>source;
cout<<"enter target filename\n";
cin>>target;
ifstream infile(source);
ofstream outfile(target);
while(infile)
{
infile.get(ch);
outfile.put(ch);
}
infile.close();
outfile.close();
return 0;
}
Subscribe to:
Posts (Atom)
