Restaurant simulation - c

#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 ;) .

Related

IDLE situation using Round Robin

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;
}

monty hall %50 - %50

i am trying to simulate monty hall problem. i didn't realized any problem but i recieve approximately %50 %50 output. i know that there are explanations but i couldnt understand these
please help me
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define win 1
#define lose 0
#define yes 1
#define no 0
int main(){
//i assume that prize is behind the first door
srand(time(NULL));
int lose_counter = 0;
int win_counter = 0;
for(int i = 1;i <= 10000;i++){
int game_status;
int chosen_door = rand() % 3 + 1;
int choice;
//first step that i chose a door
if(chosen_door == 1){
game_status = win;
}
else if(chosen_door == 2){
game_status = lose;
}
else if(chosen_door == 3){
game_status = lose;
}
//host says "do you want to change your door"
choice = rand() % 2;
if(choice == yes){
if(chosen_door == 1 ){//this is the case i have chosen
//first door and change it after question
game_status = lose;
}
if(chosen_door == 2 ){
game_status = win;
}
if(chosen_door == 3 ){
game_status = win;
}
}
if(game_status == win){
win_counter++;
}
else if(game_status == lose){
lose_counter++;
}
}
printf("win: %d\nlose: %d\n",win_counter,lose_counter);
return 0;
}
The chances to win when not switching the doors are 1/3.
If you switch the chances increase to 2/3.
Your code takes samples of 50% switching and 50% not switching, hence the average of 1/3 and 2/3 comes up as 0.5.
The odds of winning are 2/3 if you switch.
Using
choice = 1; // Switch
results in win in about 2/3 of your trials as expected.
The odds of winning are 1/3 if you don't switch.
Using
choice = 0; // Keep
results in win in about 1/3 of your trials as expected.
If you randomly decide to switch or keep, the odds of winning are
( 1/2 * 2/3 ) + ( 1/2 * 1/3 ) = 1/2
Using
choice = rand() % 2;
results in win in about 1/2 of your trials as expected.
Fixed:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
// I assume that prize is behind the first door
srand(time(NULL));
unsigned N = 10000;
unsigned win_if_keep_counter = 0;
unsigned win_if_switch_counter = 0;
for (unsigned i=N; i--; ) {
int chosen_door = rand() % 3 + 1;
if (chosen_door == 1) {
// Monte open door 2 or 3.
// You win if you keep door 1.
++win_if_keep_counter;
}
else if (chosen_door == 2) {
// Monte open door 3.
// You win if you switch to door 1.
++win_if_switch_counter;
}
else if (chosen_door == 3) {
// Monte open door 2.
// You win if you switch to door 1.
++win_if_switch_counter;
}
}
printf("win if keep: %.1f%%\n", ((double)win_if_keep_counter)/N*100);
printf("win if switch: %.1f%%\n", ((double)win_if_switch_counter)/N*100);
}

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).

C programming while loop. Variable compare with variable

Can you help me? When I execute this statement, it keeps saying out of upper bound even if it isn't. What is the problem? Thanks.
if(number_o_1 <= higher)
{
printf("Random number has reached upper bound.\n");
}
else
{
printf("Number 1: %d\n", number_o_1);
number_o_up=number_o_1+n_multiple*(x-1);
while(number_o_up<=higher)
for(x=2;x<number_r;x++)
printf("Number %d: %d\n", x,number_o_up);
}
number_o_1<= higher
Sure you want to print that the number has reached the upper bound when it's lesser than or equal to your upper bound?
It is little hard to figure the logic you would like to implement, but please see the example with exact values and comments to hit the else block.
#include <stdio.h>
int x = 2;
int higher = 100;
int number_o_1 = 10;
int number_r = 4;
int number_o_up = 1;
int n_multiple = 2;
int main()
{
if(number_o_1 >= higher) // probably want >= here to hit else
{
printf("Random number has reached upper bound.\n");
}
else
{
printf("Number 1: %d\n", number_o_1);
number_o_up = number_o_1 + n_multiple * (x-1); // number_o_up = 12
while(number_o_up <= higher) // 12 <= 100
{
for(x = 2; x < number_r; x++) // cond. 2 < 4
{
printf("Number %d: %d\n", x, number_o_up);
}
number_o_up++; //needs to change higher or number_o_up to get out of while
}
}
}
Output:
sh-4.3$ main
Number 1: 10
Number 2: 12
Number 3: 12
Number 2: 13
Number 3: 13
Number 2: 14
...
Number 3: 100

How to detect data beyond certain value out of a fixed range

I've written a c program which can read a text file with single column of data. All the data can be read into the program with the following codes:
#include <stdio.h>
#include <cstdlib>
main()
{
char temp[20];
float t;
int a = 0, x = 0; //a is no. of data greater than 180 and x is no of data
FILE *fpt;
fpt = fopen("case i.txt", "r");
fscanf(fpt, "%s", temp); // read and display the column header
printf("%s\n", temp);
while (fscanf(fpt, "%f", &t) == 1)
{
printf("%.2f\n", t);
++x; //count for number of data
if (t > 180) {
++a; //count for number of data > 180
}
if (x > 2 && a == 2) { //place where bug is expected to occur
printf("2 out of 3 in a row is greater than 180");
a=0; //reset a back to zero
x=0;
}
}
fclose(fpt);
system("pause");
}
The problem comes when I want to detect like 2 out of 3 data are beyond 180 degree Celsius. I tried some ideas like when (no. of data > 2) and (two data > 180) then generate an error message, but it will have bug as it may have two data > 180 but when 4 data are read, that means it become 2 out of 4, not 2 out of 3, is it possible to be programmed? Thank you in advance for every help.
The following is the sample data and output:
You'll need to keep a "sliding window" of 3 values indicating how many are over 180.
So one approach would be something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char temp[20];
float t;
const int min_over = 2;
const int max_window = 3;
const int max_value = 180;
char over[max_window]; // a 1 means over, 0 otherwise
int oi = 0;
int num_values = 0;
FILE *fpt;
fpt = fopen("case i.txt", "r");
fscanf(fpt, "%s", temp); // read and display the column header
printf("%s\n", temp);
memset(over, 0, max_window);
while (fscanf(fpt, "%f", &t) == 1)
{
int num_hit, i;
printf("%.2f\n", t);
// Calculate num_hit: how many over in a window of max_window
//
over[oi] = (t > max_value) ? 1 : 0;
if (++oi >= max_window)
oi = 0;
for ( num_hit = i = 0; i < max_window; i++ ) num_hit += over[i];
// Only check for min_over/max_window after at least
// max_window values read; Reset the check
//
if ((++num_values >= max_window) && (num_hit >= min_over))
{
printf("It happened!\n");
memset(over, 0, max_window);
num_values = 0;
}
}
fclose(fpt);
system("pause");
}
Since you want a ratio of 2/3, that corresponds to min_over / max_window values.
I ran this on your commented data sample:
Temperature
190.00
190.00
170.00
It happened!
200.00
190.00
100.00
It happened!
100.00
190.00
190.00
It happened!
There are about a million billion different ways to do this, but you just need to keep track of how many samples exceed the threshold and then do whatever you want to do when you hit that mark.
Let's say, once you find your "2 out of 3" samples that exceed 180 you want to print the list and stop reading from the file:
FILE *fpt;
float t;
float samples[3] = {0}; // keep a record of 3 samples
int total = 0, i;
fpt = fopen("file1.txt", "r");
while (fscanf(fpt, "%f", &t) == 1) // read until there are no more samples
{
total = 0; // clear our counter
samples[2] = samples[1]; // toss out the old 3rd sample
samples[1] = samples[0]; // and shift them to make room for the
samples[0] = t; // one we just read
for(i = 0; i<3; i++)
if(samples[i] > 180) // if any are over 180
total++; // increment our counter
if(total == 2) { // if 2 of the 3 are over 180, we got 2 out of 3
printf("2 out of 3 samples are greater than 180!\n");
printf("1: %f\n2: %f\n3:%f\n", samples[2],samples[1],samples[0]);
break;
}
}
fclose(fpt);
It's not very efficient.. but should be pretty easy to understand.

Resources