Question

i need help converting this code to java please

#include<stdio.h>

typedef struct{

int pid,at,bt,ct,tat,wt,f;

}process;

int main()

{

int n,i,j,st=0,c,tot=0,pno=0,swi=0;

float atat=0,awt=0;

printf("enter no of processes : ");

scanf("%d",&n);

process a[n],temp;

for (i=0;i<n;i++){

a[i].pid=i+1;

a[i].f=0;

printf("enter at : ");

scanf("%d",&a[i].at);

printf("enter the bt : ");

scanf("%d",&a[i].bt);

printf("--------------------------- ");

}

while(1){

int min=999,c=n;

if (tot==n)

break;

for (i=0;i<n;i++){

if ((a[i].at<=st)&&(a[i].f==0)&&(a[i].at<min)){

min=a[i].at;

c=i;

}

}

if(pno!=a[c].pid)

swi++;

if (c==n)

st++;

else{

a[c].ct=st+a[c].bt;

st=st+a[c].bt;

a[c].tat=a[c].ct-a[c].at;

atat=atat+a[c].tat;

a[c].wt=a[c].tat-a[c].bt;

awt=awt+a[c].wt;

a[c].f=1;

tot++;

}

}

printf(" Process AT BT WT FT TAT ");

for(i=0;i<n;i++)

{

printf("p%d %d %d %d %d %d ",i,a[i].at,a[i].bt,a[i].wt,a[i].ct,a[i].tat);

}

printf(" average tat is %f ",(float)(atat/n));

printf("average wt is %f",(float)(awt/n));

printf(" NO OF SWITCHES :%d",swi);

}

input and output

:enter no of processes : 4 enter at : 1 enter the bt : 2 enter at : 1 enter the bt : 2 enter at :3 enter the bt : 4 enter at :

ROUND ROBIN(RR):

CODE:

#include<stdio.h>

typedef struct{

int pid,at,bt,bt1,ct,tat,wt,f,at1,res,done,wt1;

}process;

int main()

{

int n,i,q,st=0,c=0,j,count=0,number,pno=0;

float atat=0,awt=0,art=0,atat2=0,awt2=0,art2=0;

printf("enter no of processes:");

scanf("%d",&n);

printf("Enter time quantum");

scanf("%d",&q);

number=n;

process a[n],temp;

for (i=0;i<n;i++){

a[i].pid=i+1;

a[i].f=0;

a[i].done=0;

printf("enter arraival time:");

scanf("%d",&a[i].at);

printf("enter the bt:");

scanf("%d",&a[i].bt);

a[i].bt1=a[i].bt;

a[i].at1=a[i].at;

printf("--------------------------- ");

}

int m=999,max=-1;

for(i=0;i<n;i++)

{

if(a[i].bt<m)

m=a[i].bt;

}

while (1){

int r=n,min=999;

for (i=0;i<n;i++){

if ((a[i].at<min)&&(a[i].f==0)&&(a[i].at<=st))

{

min=a[i].at;

r=i;

}

}

if(a[r].done==0)

{

a[r].res=st-a[r].at;

//art=art+a[r].res;

}

if (c==n)

break;

else if(r==n){

st++;

if(c==n-1)

count++;

}

else if(a[r].bt<=q)

{

st=st+a[r].bt-count;

a[r].done=1;

a[r].bt=0;

a[r].f=1;

c++;

a[r].ct=st;

a[r].tat=a[r].ct-a[r].at1;

atat=atat+a[r].tat;

a[r].wt=a[r].tat-a[r].bt1;

awt=awt+a[r].wt;

}

else{

st=st+q-count;

a[r].at=st+1;

a[r].bt=a[r].bt-q;

a[r].done=1;

}

}

for(i=0;i<n;i++)

art+=a[i].res;

//printf("%f",art);

printf(" average tat is %f",(float)(atat/number));

printf(" average wt is %f ",(float)(awt/number));

printf(" average rt os %f ",(float)(art/number));

printf(" Process At Bt Wt Ft Tat Res");

for(i=0;i<n;i++)

{

printf(" P%d %d %d %d %d %d %d",a[i].pid,a[i].at1,a[i].bt1,a[i].wt,a[i].ct,a[i].tat,a[i].res);

}

}

input and output:

enter no of processes:4 Enter time quantum4 enter arraival time: 2 enter the bt:5 enter arraival time: 6 enter the bt:3 enter

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1.JAVA PROGRAM

import java.util.*;

class process{//class process
int pid,at,bt,ct,tat,wt,f;
}

public class Main//class Main
{
   public static void main(String[] args) {//method main
   int n,i,j,st=0,c,tot=0,pno=0,swi=0;
float atat=0,awt=0;
Scanner s=new Scanner(System.in);
  
   System.out.print("enter no of processes : ");
n=s.nextInt();
process a[]=new process[n],temp;

for (i=0;i<n;i++){
a[i]=new process();
a[i].pid=i+1;
a[i].f=0;
System.out.print("enter at : ");
a[i].at=s.nextInt();
System.out.print("enter the bt : ");
a[i].bt=s.nextInt();
System.out.println("--------------------------- ");
}

while(true){
int min=999;
c=n;

if (tot==n)
break;

for (i=0;i<n;i++){
if ((a[i].at<=st)&&(a[i].f==0)&&(a[i].at<min)){
min=a[i].at;
c=i;
}
}

if(c==n||pno!=a[c].pid)
swi++;
if (c==n)
st++;
else{
a[c].ct=st+a[c].bt;
st=st+a[c].bt;
a[c].tat=a[c].ct-a[c].at;
atat=atat+a[c].tat;
a[c].wt=a[c].tat-a[c].bt;
awt=awt+a[c].wt;
a[c].f=1;
tot++;
}

}

   System.out.println("Process\tAT\tBT\tWT\tFT\tTAT ");

for(i=0;i<n;i++){
System.out.println("p"+i+"\t"+a[i].at+"\t"+a[i].bt+"\t"+a[i].wt+"\t"+a[i].ct+"\t"+a[i].tat);
}

   System.out.println("average tat is "+(float)(atat/n));
System.out.println("average wt is "+(float)(awt/n));
System.out.println(" NO OF SWITCHES : "+swi);
   }
}

OUTPUT

enter no of processes : 4 enter at 1 enter the bt : 2 enter at : 1 enter the bt : 2 enter at : 3 enter the bt : 4 enter at :1 import java.util.*; 3. class process{//class process int pid, at, bt, bt1, ct, tat,wt,f, ati,res, done, wt1; 5 } 6 7 publicC=n; if (tot= n) break; for (i=0;i<n;i++){ if ((a[i]at<=st)&&(a[i].f==0)&&(a[i].at<min)>< min=a[i].at; C=i; } ہا 37 38 39 40

2.JAVA PROGRAM

import java.util.*;

class process{//class process
int pid,at,bt,bt1,ct,tat,wt,f,at1,res,done,wt1;
}

public class Main//class Main
{
   public static void main(String[] args) {//method main
   int n,i,q,st=0,c=0,j,count=0,number,pno=0;
float atat=0,awt=0,art=0,atat2=0,awt2=0,art2=0;
Scanner s=new Scanner(System.in);
  
   System.out.print("enter no of processes : ");
n=s.nextInt();
System.out.print("Enter time quantum");
q=s.nextInt();

number=n;
process a[]=new process[n],temp;

for (i=0;i<n;i++){
a[i]=new process();
a[i].pid=i+1;
a[i].f=0;
a[i].done=0;
System.out.print("enter arraival time:");
a[i].at=s.nextInt();
System.out.print("enter the bt:");
a[i].bt=s.nextInt();
a[i].bt1=a[i].bt;
a[i].at1=a[i].at;
System.out.println("--------------------------- ");
}

int m=999,max=-1;

for(i=0;i<n;i++){
if(a[i].bt<m)
m=a[i].bt;
}

while (true){
int r=n,min=999;
for (i=0;i<n;i++){
if ((a[i].at<min)&&(a[i].f==0)&&(a[i].at<=st)){
min=a[i].at;
r=i;
}
}

if(r<n&&a[r].done==0){
a[r].res=st-a[r].at;
}

if (c==n)
break;

else if(r==n){
st++;
if(c==n-1)
count++;
}

else if(a[r].bt<=q){
st=st+a[r].bt-count;
a[r].done=1;
a[r].bt=0;
a[r].f=1;
c++;
a[r].ct=st;
a[r].tat=a[r].ct-a[r].at1;
atat=atat+a[r].tat;
a[r].wt=a[r].tat-a[r].bt1;
awt=awt+a[r].wt;
}

else{
st=st+q-count;
a[r].at=st+1;
a[r].bt=a[r].bt-q;
a[r].done=1;
}

}

for(i=0;i<n;i++)
art+=a[i].res;

System.out.println(" average tat is "+(float)(atat/number));
System.out.println(" average wt is "+(float)(awt/number));
System.out.println(" average rt os "+(float)(art/number));
System.out.println(" Process\tAt\tBt\tWt\tFt\tTat\tRes");

for(i=0;i<n;i++){
System.out.println(" P"+a[i].pid+"\t"+a[i].at1+"\t"+a[i].bt1+"\t"+a[i].wt+"\t"+a[i].ct+"\t"+a[i].tat+"\t"+a[i].res);
}

}
}

OUTPUT

amput Estes time was extes deauval me: 2 Jenter arraival time: 6 enter the bt:3 enter arraival time: 2 lenter the bt:5 enterimport java.util.*; class process{//class process int pid, at,bt, bt1, ct,tat,wt, f,at1,res, done, wt1; } public class Main//43 44 while (true) { int r=n, min=999; for (i=0;i<n;i++){ if ((a[i].at<min)&&(a[i].f==0)&&(a[i].at<=st)){ min=a[i].at; r=i; }d.wt-a[r].tat-a[r].bt1; awt-awta[r].wt; } else{ st=st+4-count; a[r].at=st+1; a[r].bt=a[r].bt-4; a[r].done=1; } } 75 76 77 78

Add a comment
Know the answer?
Add Answer to:
i need help converting this code to java please #include<stdio.h> typedef struct{ int pid,at,bt,ct,tat,wt,f; }process; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float aver...

    Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...

  • i need flowchart for the following c code #include <stdio.h> int main() {     int n,...

    i need flowchart for the following c code #include <stdio.h> int main() {     int n, i, sum = 0;     printf("Enter an integer: ");     scanf("%d",&n);     i = 1;     while ( i <=n )     {         sum += i;         ++i;     }     printf("Sum = %d",sum);     return 0; }

  • Can you write the algorithm for this code? #include<stdio.h> int main() { int process[20], priority[20], arrival_time[20],...

    Can you write the algorithm for this code? #include<stdio.h> int main() { int process[20], priority[20], arrival_time[20], burst_time[20], turnaround_time[20], waiting_time[20]; int i, j, limit, sum = 0, position, temp; float average_wait_time, average_turnaround_time; printf("Enter Total Number of Processes:\t"); scanf("%d", &limit); printf("\nEnter Burst Time and Priority For %d Processes\n", limit); for(i = 0; i < limit; i++) { printf("\nProcess[%d]\n", i + 1); printf("Process Burst Time:\t"); scanf("%d", &burst_time[i]); printf("Process Priority:\t"); scanf("%d", &priority[i]); process[i] = i + 1; } for(i = 0; i < limit;...

  • C program-- the output is not right please help me to correct it. #include <stdio.h> int...

    C program-- the output is not right please help me to correct it. #include <stdio.h> int main() { int arr[100]; int i,j,n,p,value,temp; printf("Enter the number of elements in the array: \n "); scanf("%d",&n); printf("Enter %d elements in the array: \n",n); for(i=0;i<n;i++) { printf("\nelement %d: ",i); scanf("\n%d",&arr[i]); } printf("\nEnter the value to be inserted: \n "); scanf("\n%d",&value); printf("The exist array is: \n"); for(i=0;i<n;i++) { printf("%d",arr[i]); } p=i; for(i=0;i<n;i++) if(value<arr[i] ) { p = i; break; } arr[p]=value; printf("\n"); for (i =...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Whats wrong with my code? Please help! #include <stdio.h> int main() { //variables int m1, m2,...

    Whats wrong with my code? Please help! #include <stdio.h> int main() { //variables int m1, m2, m3; int a1, a2, a3; int f1, f2, f3; //input acceleration printf("Enter value for a1: "); printf("Enter value for a2: "); printf("Enter value for a3: "); scanf("%d %d %d",&a1,&a2,&a3); //input mass printf("Enter value for m1: "); printf("Enter value for m2: "); printf("Enter value for m3: "); scanf("%d %d %d",&m1,&m2,&m3); //functions f1=m1*a1; m1=f1/a1; a1=f1/m1; f2=m2*a2; m2=f2/a2; a2=f2/m2; f3=m3*a3; m3=f3/a3; a3=f3/m3; //command: printf("f1=%d\n, m1=%d\n, a1=%d\n"); printf("f2=%d\n,...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int...

    I'm having trouble rounding the numbers i'm getting in my output here's my code: #include<stdio.h> int main() { char gender; float a1, a2, a3, a4, a5; float waistmeasurement, wristmeasurement, hipmeasurement, forarmmeasurement; float B, bodyweight, Bodyfat, Bodyfatpercentage;    printf("This program determines the body fat of a person.Enter your gender (f|F|m|M): "); scanf("%c", &gender); printf("\n");    if(gender=='F' || gender=='f'){ printf("Enter body weight (in pounds): \n"); scanf("%f", &bodyweight); printf("Enter wrist measurement at fullest point (in inches): \n"); scanf("%f", &wristmeasurement); printf("Enter waist measurement at...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i);...

    #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i); for(j=0;j<3;j++){ scanf("%f",&array[i][j]); } } printf("Given table data:\n"); for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf(" %0.1f",array[i][j]); } printf("\n"); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ rowsum=rowsum+array[i][j]; } printf("sum of the %d row is %0.1f\n",i,rowsum); rowsum=0; } for(j=0;j<3;j++){ colsum+=array[j][i]; printf("Sum of %d coloumn is %0.1f\n",j,colsum); colsum=0; } return(0); } can someone help me to get the correct row sum and column sum in c program

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT