Question

In: Computer Science

Could someone point out the logical error in this code? The code seems to start at...

Could someone point out the logical error in this code?

The code seems to start at the last input. But I want the code to output something like this:

Enter name: A
Enter time:5
Enter name:B
Enter time: 4
Enter name: C
Enter time:3
Enter name:D
Enter time: 9
Enter name: E
Enter time: 11
A process is started and ends in 5 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
B process is started and ends in 4 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
C process is started and ends in 3 sec.
1 sec passed...
2 sec passed...
3 sec passed...
D process is started and ends in 9 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
6 sec passed...
7 sec passed...
8 sec passed...
9 sec passed...
E process is started and ends in 10 sec.
1 sec passed...
2 sec passed...
3 sec passed...
4 sec passed...
5 sec passed...
6 sec passed...
7 sec passed...
8 sec passed...
9 sec passed...
10 sec passed...

10 sec reduced in E process with time left 1 sec.

E process is started and ends in 1 sec.
1 sec passed...
All Processes are executed...

Code:

#include 
#include 
#include 
#include 
#include 
#define S 4

typedef struct
{
   char pname [S];
   int ptime;
}PROCESS;

typedef struct
{
   PROCESS queue [S];
   int front;
   int rear;
}QUE;

void createQueue(QUE *);
void enCQueue(QUE *, PROCESS);
PROCESS deCQueue(QUE *);
bool isCEmpty(int,int);
bool isCFull(int,int);
void tshareProc (PROCESS);

int main(void)
{
   PROCESS p;
   QUE q;
  
   createQueue (&q);
   enCQueue (&q, p);
   tshareProc(p);
  
   return 0;
}

void createQueue(QUE *q)
{
   memset(q->queue,'\0',S);
   q->front = 0;
   q->rear = 0;
}

void enCQueue(QUE *q, PROCESS p)
{
   q->queue[q->rear] = p;
   q->rear = (q->rear + 1) % S;
}

PROCESS deCQueue(QUE *q)
{
   PROCESS p;
   q->queue[q->front]= p;
   q->front = (q->front + 1) % S;
   return;
}

bool isCEmpty(int front,int rear)
{
   if(front == rear)
       return true;
   else
       return false;
}
void tshareProc (PROCESS p)
{
   QUE q;
   int i, j, x;
   bool empty, full;
   createQueue (&q);

   for (i=0; i <= S; i++)
   {
  
       printf ("Enter username:");
       scanf("%s",&p.pname);
       printf("Enter time in seconds:");
       scanf("%d",&p.ptime);
       while(p.ptime < 1) {
       printf("Time must be greater than zero. \nInput again:");
       scanf("%d",&p.ptime);
       }
       enCQueue (&q,p);
   }
  
   empty = isCEmpty (q.front, q.rear);
   while (!empty)
   {
       if (p.ptime <= 10){
           printf ("\n %s's process is starting and will end in %d seconds \n", p.pname, p.ptime);
       for (j=0; j < p.ptime; j++){
           printf(" %d seconds passed\n",j+1);
           Sleep(1000);                             //change to sleep(1000) if giving error
           } //end of for loop
       deCQueue (&q);
       }
       else
       {
           printf("\n %s's process is starting and ends in 10 seconds\n",p.pname);
           for(x = 0; x < 10; i++) {
               printf("%d seconds passed\n",x+1);
               Sleep(1000);                         //change to sleep(1000) if giving error
           } // end of for loop
       p.ptime = p.ptime - 10;
       printf("10 seconds is subtracted in %s's process with %d seconds left\n",p.pname,p.ptime);
       deCQueue(&q);
       }
       enCQueue(&q, p);
   } //end of while loop
   printf ("All processes were performed");
   return;
}

Solutions

Expert Solution

Following is the fixed code, containing comments for fixes appropriately :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>

/*
 * We should be able to hold maximum 5 processes.
 */
#define S 5

typedef struct
{
   char pname [S];
   int ptime;
}PROCESS;

typedef struct
{
   PROCESS queue [S];
   int front;
   int rear;
}QUE;

void createQueue(QUE *);
void enCQueue(QUE *, PROCESS);
PROCESS deCQueue(QUE *);
bool isCEmpty(int,int);
bool isCFull(int,int);
void tshareProc (PROCESS);

int main(void)
{
   PROCESS p;
   QUE q;
  
   createQueue (&q);
   enCQueue (&q, p);
   tshareProc(p);
  
   return 0;
}

void createQueue(QUE *q)
{
   memset(q->queue,'\0',S);
   q->front = 0;
   q->rear = 0;
}

void enCQueue(QUE *q, PROCESS p)
{
   /*
    * Add check if queue is full.
    */
   if(q->rear == S)
   {
       printf("\n\n!! Queue is full. Cannot add more !!\n\n"); 
       return;
   }

   q->queue[q->rear] = p;
   q->rear = q->rear + 1;
}

PROCESS deCQueue(QUE *q)
{
   PROCESS p;

   /*
    * We need to properly assign the values "into" p.
    */
   p = q->queue[q->front];

   q->front = q->front + 1;

   return p;
}

bool isCEmpty(int front,int rear)
{
   if(front == S)
       return true;
   else
       return false;
}
void tshareProc (PROCESS p)
{
   QUE q;
   int i, j, x;
   bool empty, full;
   createQueue (&q);

   /*
    * Maximum "S" processes should be used.
    */
   for (i = 0; i < S; i++)
   {
  
       printf ("Enter username:");
       scanf("%s",p.pname);
       printf("Enter time in seconds:");
       scanf("%d",&p.ptime);
       while(p.ptime < 1) {
       printf("Time must be greater than zero. \nInput again:");
       scanf("%d",&p.ptime);
       }
       enCQueue (&q,p);
   }
  
   empty = isCEmpty (q.front, q.rear);
   while (!empty)
   {
       p = deCQueue (&q);

       if (p.ptime <= 10){
           printf ("\n%s's process is starting and will end in %d seconds \n", p.pname, p.ptime);

           for (j=0; j < p.ptime; j++) {

               printf("%d seconds passed\n",j+1);
               sleep(1);                             //change to sleep(1000) if giving error

           } //end of for loop
       }
       else
       {
           while(p.ptime > 0) 
           {
               printf("\n%s's process is starting and ends in %d seconds\n",p.pname, p.ptime < 10 ? p.ptime : 10);

               for(j = 0; j < (p.ptime < 10 ? p.ptime : 10); j++) {

                   printf("%d seconds passed\n",j+1);
                   sleep(1);                         //change to sleep(1000) if giving error
               } // end of for loop


               p.ptime = p.ptime - 10;
               if(p.ptime > 0)
               {
                   printf("10 seconds is subtracted in %s's process with %d seconds left\n",p.pname,p.ptime);
               }
           }
       }


       /*
        * Need to recheck the empty condition.
        */
       empty = isCEmpty (q.front, q.rear);

       //enCQueue(&q, p);
   } //end of while loop


   printf ("All processes were performed");
   return;
}

Related Solutions

There is error in this java code. Pls debug. // Prompt user for value to start...
There is error in this java code. Pls debug. // Prompt user for value to start // Value must be between 1 and 20 inclusive // At command line, count down to blastoff // With a brief pause between each displayed value import java.util.*; public class DebugSix3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String userNumString; int userNum, val; final int MIN = 1; final int MAX = 20; System.out.println("Enter a number between " +...
Could the sorting algorithm start out as if then else situation?
Could the sorting algorithm start out as if then else situation?
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method...
can someone tell me why I'm getting the error code on Eclipse IDE: Error: Main method is not static in class StaticInitializationBlock, please define the main method as:    public static void main(String[] args) This is what I'm working on class A { static int i; static { System.out.println(1); i = 100; } } public class StaticInitializationBlock { static { System.out.println(2); } public static void main(String[] args) { System.out.println(3); System.out.println(A.i); } }
Consider the variables of job satisfaction and perceived job security. It seems logical that these go...
Consider the variables of job satisfaction and perceived job security. It seems logical that these go hand in hand; it would make sense for them to be connected in some way. Suppose that a study was conducted on these variables, and the correlation coefficient was found to be .61 (r = .61). Develop a main response in which you address the following: Explain what a Pearson's r value of .61 would imply about the relationship for these variables (strength and...
I am creating SAS code, but am receiving an errors " ERROR: Value is out of...
I am creating SAS code, but am receiving an errors " ERROR: Value is out of range or inappropriate. ERROR: No body file. HTML5(WEB) output will not be created." This is the code: option ls=65 ps=65; data one; input IQ; cards; 145 139 122 ; title 'Normal Quantile - Quantile Plot for IQ'; ods graphics on; proc univariate data=one; qqplot IQ / normal (mu=est sigma=est); run;
Elevated ocean temperatures are one factor contributing to coral bleaching. Therefore, it seems logical that as...
Elevated ocean temperatures are one factor contributing to coral bleaching. Therefore, it seems logical that as the Earth warms, then the latitudes in which the temperature range is suitable for coral reef formation should move away from the equator and toward cooler waters at higher latitudes. Provide and briefly explain three or more distinct (natural or anthropogenic) environmental factors that might prevent outcome this from occurring? In other words, what would constrain the movement of coral reefs to the poles...
I would appreciate if someone could work this out and also explain how to find the...
I would appreciate if someone could work this out and also explain how to find the upper and lower limits. Rothamsted Experimental Station (England) has studied wheat production since 1852. Each year, many small plots of equal size but different soil/fertilizer conditions are planted with wheat. At the end of the growing season, the yield (in pounds) of the wheat on the plot is measured. For a random sample of years, one plot gave the following annual wheat production (in...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
#1. It seems logical that restaurant chains with more restaurants (units) would have greater sales. This...
#1. It seems logical that restaurant chains with more restaurants (units) would have greater sales. This is mitigated by several possibilities. Use the data below to answer whether there is a positive relationship between the number of restaurants and sales. Provide a precise quantitative explanation. Chain Sales ($ Billions) No. of Restaurants (1000s) McDonald's 17.1 12.4 Burger King 7.9 7.5 Taco Bell 4.8 6.8 Pizza Hut 4.7 8.7 Wendy's 4.6 4.6 KFC 4.0 5.1 Subway 2.9 11.2 Dairy Queen 2.7...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how to find it correctly for me?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT