IDLE situation using Round Robin - c

I am having trouble with calculating the correct Average wait time and Average Turnaround time when there are no processes ready to be executed. [ IDLE ]
Example of IDLE situation:
0 3
0 5
9 8
10 6
The first column represents Arrival Time
The second column represents Burst Time
for the current n process
The average wait time should be: 3.5
The average turnaround time should be: 9
But the results I get are:
The average wait time: 5
The average turnaround time: 10
Any suggestions of what I should do to fix this problem, based on my code? I know where the IDLE situation outlies and it's noted in my code. Any help would be greatly appreciated..
Where IDLE is commented, I was planning on subtracting the (save) variable from the wait_time. Since the arrival and burst time from ( before the IDLE situation occured) already completed.
#include <stdio.h>
int main()
{
int i, total = 0, x, limit, counter = 0, t_quantum;
int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10];
float average_wait_time, average_turnaround_time;
printf("\nEnter Total Number of Processes: ");
scanf("%d", &limit);
x = limit;
for (i = 0; i < limit; i++)
{
printf("\nProvide the details for Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
}
printf("\nEnter Time Quantum:\t");
scanf("%d", &t_quantum);
int save = 0;
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for (total = 0, i = 0; x != 0;)
{
if (temp[i] <= t_quantum && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if (temp[i] > 0)
{
temp[i] = temp[i] - t_quantum;
total = total + t_quantum;
}
if (temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]);
// printf("Completion TIme: %d\n", total);
wait_time = wait_time + total - arrival_time[i] - burst_time[i];
save = total - arrival_time[i] - burst_time[i];
turnaround_time = turnaround_time + total - arrival_time[i];
// printf("CT: %d\n", turnaround_time);
counter = 0;
}
if (i == limit - 1)
{
i = 0;
}
else if (arrival_time[i + 1] <= total)
{
i++;
}
else
{
// IDLE when temp[i] == 0
// limit +=1;
i++;
}
}
average_wait_time = wait_time *1.0 / limit;
average_turnaround_time = turnaround_time *1.0 / limit;
printf("\n\nAverage Waiting Time:\t%f", average_wait_time);
printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time);
return 0;
}

Related

I want to make a parking fee program by using visual studio

int min1, min2, won;
printf("parking minutes(분)? ");
scanf("%d", &min1);
min2 = (min1 - 30) % 10;
if (min1 <= 39)
won = 2000;
else {
if (min2 = 0)
won = 2000 + 1000 * (min1 - 30) % 10;
else
won = 2000 + 1000 * (min1 - min2 - 20) % 10;
}
printf("parking fee: %d", won);
The conditions of this program
until 30min, 2000won
after 30min, 1000won per 10min
max 25000won per a day
parking minutes cannot be over than 24 hours
I thought that '%' means remainder so I write like that but when I input 52, the results say 5200! I want to make result to be 5000. And I want to know what to do for condition 3 and 4. What can I do? Should I use 'for' and 'sum'?
Let's program the steps in the same order as the assignment:
int min1, min2, won;
printf("parking minutes(분)? ");
if (scanf("%d", &min1) != 1) {
printf("invalid input\n");
return 1; // invalid input.
}
won = 2000; // 1. until 30min, 2000won, minimum price
if (min1 > 30) {
// 2. after 30min, 1000won per 10min
min2 = min1 - 30; // minutes after 30
// add 1000won for every slice or 10min or portion thereof
won += ((min2 + 9) % 10) * 1000;
// 3. max 25000won per a day
if (won > 25000)
won = 25000;
}
// 4. parking minutes cannot be over than 24 hours
if (min1 > 24 * 60) {
// reject request
printf("parking time exceeds 24 hours\n");
} else {
printf("parking fee: %d\n", won);
}
The problem is with the condition of your inner if in else block.
if(min2 = 0)
One equal sign is assignment operator, you have to use == for equality check.
if(min2 == 0)

how can i make this code more efficient? need to calaulate number of combinations

#include<stdio.h>
void main() {
int nis1 = 0, nis2 = 0, nis5 = 0, nis10 = 0, nis20 = 0, nis50 = 0, nis100 = 0, nis200 = 0;
long int num, count = 0, sum = 0;
while (1)
{
printf_s("what number you like to check? (or press '0' to exit)\n");
scanf_s("%d", &num);
if (num == 0)
break;
for (nis200 = 0; nis200 <= num / 200; nis200++) {
for (nis100 = 0; nis100 <= num / 100; nis100++) {
for (nis50 = 0; nis50 <= num / 50; nis50++) {
for (nis20 = 0; nis20 <= num / 20; nis20++) {
for (nis10 = 0; nis10 <= num / 10; nis10++) {
for (nis5 = 0; nis5 <= num / 5; nis5++) {
for (nis2 = 0; nis2 <= num / 2; nis2++) {
for (nis1 = 0; nis1 <= num; nis1++) {
sum = nis200 * 200 + nis100 * 100 + nis50 * 50 + nis20 * 20 + nis10 * 10 + nis5 * 5 + nis2 * 2 + nis1 * 1;
if (sum == num) {
count++;
break;
}
if (sum > num)
{
break;
}
}
}
}
}
}
}
}
}
printf_s("the number of combinations is: %d\n", count);
count = 0;
}
}
//i have to build a code that with a given number by the user, how many posibillities are there to sum the number with the number:1,2,5,10,20,50,100,200.
Check in each for loop if your sum is already above the num and break the loop if it is the case. Not the ultimate optimization but it makes your code much faster.
You do a lot of counting from 0 to whatever. If you use a number like 500, then your first iteration will be 0 x 200, 0 x 100 ... all the way down to 0 x 2, and then you count 1s from 0 to 500. When you're down to your last option, you should be able to calculate how many 1s you still need.
For any given bill, you always count how many of it you need without regard for the bills you've already selected. If you use a number like 500, and you already have 2 x 200, then you will never need 100 5s; you should be able to calculate the maximum number of 5s you will need. (Keep a running total of each selection-so-far).

Time game (hours, minutes, seconds)

first thanks watching. i tried to take an input (hour, minute, second, duration (seconds) )
and convert all to a total hour of arrival.
i got a problem when i enter this numbers : "23 59 59 10801".
what i am expecting to get is "arrival time - > 3 0 0 "
but actually i get nothing.
(every other positive number works just fine...)
thank for help, sorry if my code is a total mess.
:)
int h = 0, m = 0, s = 0, time = 0, ih = 0, im = 0, is = 0;
printf("please enter 4 digits\n");
scanf("%d %d %d %d", &h, &m, &s, &time);
if ((((h<=23)&&(h>0)) && ((m<=59) && (m>0)) && ((s<=59) && (s>0))) && (time > 0))
{
// hour loop
while (time >= 3600) {
++ih;
time = (time - 3600);
// minute loop
while ((time < 3600) && (time != 0)) {
++im;
time = (time - 60);
// second loop
while ((time <= 60) && (time != 0)) {
++is;
time = (time - 1);
}
}
}
h = (h + ih);
m = (m + im);
s = (s + is);
if (h >= 24) {
h = ((h * 0) + ih-1);
}
if (m = 60) {
m = (m * 0);
++h;
}
if (s = 60) {
s = (s * 0);
++m;
}
printf("\nwe are happy to annonce that:\n\nyour arival time will be at : %d %d %d\n\n\nhave a nice day!\n\n\n\n", h, m, s);
}
else printf("\nwrong value my friend!\n\n");
return 0;
}
if (m = 60) sets m to 60 and is true. You want to write if (m == 60).
The same for if (s = 60)

Displaying a list of errors,when consecutive data is too far from the average,

I want to do 2 things:
display all voltages that differ from the average by more than 10% of the average and display all pairs of consecutive hours where the change from the voltage at one hour to the next is greater than 15% of the average.I have come into trouble with the second part.
#include <stdio.h>
#include <math.h>
int i, problem = 0, index;
float voltage[6];
float average, average10, average15, dif, total;
int main(){
total = 0.0;
for( index = 0; index < 6; index++ ){
printf( "Enter a voltage for hour %d: ", index+1 );
scanf( "%f", &voltage[index] );
total += voltage[index];
}
average = total / 6.0;
average10 = average / 10;
average15 = average / 100 * 15;
printf("The average is %1.1f\n", average);
printf("10%% = %1.1f\n", average10);
printf("15%% = %1.1f\n", average15);
for(index = 0; index < 6; index++){
dif = fabs(voltage[index] - average);
if(dif > (average10)){
problem++;
if(problem == 1){
printf("The following problems occurred:\n");}
printf("%d. Voltage at hour %d was %1.1f (difference of %1.1f volts)\n", problem, (i ++)+1, voltage[index], dif);
}
}
for(index = 1; index < 6; index++){
dif = fabs((voltage[i] - voltage[i-1] > average15));
if(dif > average15){
problem++;
if(problem == 1){
printf("The following problems occurred:\n");}
printf("%d Voltage change from hour %d to %d was %1.1f", problem, i, (i ++)+1 , dif);
}
}
if(problem = 0) printf("No problems were encountered.");
}
This displays the first part fine apart from the problem hours dont always display the right values (as seen here for problem number 2 not enough rep to embed sorry) http://gyazo.com/34fa038b11bf85effa195232f952cd76
but absolutely nothing appears for the second part or the printf if no problems occur . Do you guys have any ideas on how to make the values on the problems correctly line up and on why im not getting anything back from the second for loop
It is just a typo here:
dif = fabs((voltage[i] - voltage[i-1] > average15));
/* This means dif = fabs((0));
* dif = fabs((1));
* as the result of the > operator is 0 or 1
**/
Probably should be
dif = fabs(voltage[i] - voltage[i-1]);

Restaurant simulation

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define Empty 1
#define Full 0
float ChanceOfArrival (float CustomersPerMinute)
{
float CustomersArrivalChance;
int i;
/*Customers arriving per second */
CustomersArrivalChance = (CustomersPerMinute / 60) * 100;
printf ("The chance of customers arriving is: %0.3f%%\n", CustomersArrivalChance);
/* each 10 minute interval */
for (i = 0; i <= 18; i++)
{
intervals (CustomersArrivalChance);
}
return CustomersArrivalChance;
}
int intervals (CustomersArrivalChance)
{
int totalCustomers = 0, totalWait = 0, queue = 0, SecondsInterval, waitingCustomers = 0;
int Cash1Salad, Cash1Buger, Cash2Salad, Cash2Burger;
int Cash1 = 1, Cash2 = 1;
int cointoss;
int x, Empty1, Empty2;
int CustomersServed = 0;
float RatePerMinute = 0, AverageWait = 0;
static int intervalNumber;
srand(time(NULL));
/*What could possibly happen every second in a 10 minute interval */
for (SecondsInterval = 0; SecondsInterval <= 600; SecondsInterval++)
{
x = rand() % 101;
if (CustomersArrivalChance >= x)
{
/*Customers Arrive this second */
totalCustomers++;
queue++;
/*Choosing a cash at random */
cointoss = rand()%2;
if (queue > 0)
{
/* Cash 1 is open cash 2 is busy so the customer goes to cash 1 and chooses
at random what they want to eat */
if ((Cash1 == Empty) && (Cash2 != Empty) || (cointoss == 1) )
{
Cash1 = Full;
queue--;
switch ((rand()%2))
{
case 0:
Cash1Salad = rand()% 66 + 55;
totalWait = totalWait + Cash1Salad;
Empty1 = Cash1Salad;
CustomersServed++;
break;
case 1:
Cash1Buger = rand()% 130 + 111;
totalWait = totalWait + Cash1Buger;
Empty1 = Cash2Burger;
CustomersServed++;
break;
}
}
/* Cash 1 is busy cash 2 is open customer goes to cash 2 and chooses what they want */
else if (Cash2 = Empty)
{
Cash2 = Full;
queue--;
switch ((rand()%2))
{
case 0:
Cash2Salad = rand()% 75 + 65;
totalWait = totalWait + Cash2Salad;
Empty2 = Cash2Salad;
CustomersServed++;
break;
case 1:
Cash2Burger = rand()% 140 + 121;
totalWait = totalWait + Cash2Burger;
Empty2 = Cash2Burger;
CustomersServed++;
break;
}
}
/*Both cashes are busy so the customer has to wait until one cash opens */
else
{
totalWait++;
waitingCustomers++;
}
/*Clearing Cash 1 if someone went there */
if (Empty1 > 0)
{
Empty1--;
}
/*empty1 is equal to 0 then cash 1 is empty */
else
{
Cash1 = Empty;
}
/*Clearing cash 2 is someone went there */
if (Empty2 > 0)
{
Empty2--;
}
/*empty2 is equal to 0 then cash 2 is empty */
else
{
Cash2 = Empty;
}
}
}
else
{
/*nothing happens because no customer showed up */
}
}
intervalNumber++;
AverageWait = ((totalWait*1.0)/ (totalCustomers));
printf ("The average waiting time per customer in seconds is %0.2f in the interval %d\n\n", AverageWait, intervalNumber);
printf ("The total customers that arrived in the interval %d is %d\n\n", intervalNumber, totalCustomers);
}
int main (void)
{
float CustomersPerMinute;
printf ("Enter in the number of customers you want to arrive per minute:\n");
scanf ("%f", &CustomersPerMinute);
ChanceOfArrival(CustomersPerMinute);
return 0;
}
Hi, I have this program that is suppose to simulate a restaurant that only serves a salad or burger, there are only two cashiers and only one line up that the customers can line up in to get served.
I am not sure why it is not working but as everything makes logical sense I believe, but when i run this program is just prints out the average waiting time as it is suppose to.
But the problem is that the average wait time that is printed is the same in every interval. Which is not what is I want.
Logically I think the average wait time should be different in each interval because customers are being generated at random. However this is not the case as the average wait time is always the same, how can i fix this?
As well the total number of customers is the same for each interval when is should be different because the customers are generated at random, how can i fix this problem as well?
It looks like you are looping srand. So it may give you the same rand() results at a given second.
Stack overflow is not a debuging forum though ;) .

Resources