HELP!!!!!!!!

Pages: 12
An industrial engineering, it is sometimes necessary to simulate manufacturing and service operations for the purpose of improving efficiency and optimizing the system. In this problem you are to model a rapid oil change and lubrication business. The queue data structure can be used to do this. Assume that the station has the following:
a. There are three bays for changing oil.
b. It takes 15 minutes to service each car.
c. One car arrives randomly between every 2 and 20 minutes.
d. At the end of the day, all waiting cars are sent away.
Write your program to model 30 days of operation. Determine the average waiting time for the cars and the total amount of idle time for the bays. Run your program 12 times to get an understanding of the variability of the results. Report all of your answers.

Here is my program so far:
#include<iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;


typedef struct BAYS
{
int ideal_Time;
int oper_Time;
};

BAYS B;

void BayOperation(int min)
{
int aTime;
aTime = rand() % 20 + 2;
int Total = 0;;
if(min <= 0)
{
return;
}
else
{
B.ideal_Time = B.ideal_Time + aTime;
B.oper_Time = B.oper_Time + 15;
BayOperation((min - aTime - 15));
}
return;
}

int main()
{
BAYS B1,B2,B3;
int Time = 30 * 12 * 60; // (30 Days for 12 hrs each day in min)

//Call Recursive Function
B.oper_Time = 0;
B.ideal_Time = 0;
BayOperation(Time);
B1.oper_Time = B.oper_Time;
B1.ideal_Time = B.ideal_Time;
B.oper_Time = 0;
B.ideal_Time = 0;
BayOperation(Time);
B2.oper_Time = B.oper_Time;
B2.ideal_Time = B.ideal_Time;
B.oper_Time = 0;
B.ideal_Time = 0;
BayOperation(Time);
B3.oper_Time = B.oper_Time;
B3.ideal_Time = B.ideal_Time;

cout << endl << "Time of Operation for Bay 1 : " << (B1.oper_Time / 60) << " hrs." << endl;
cout << "Time of Operation for Bay 2 : " << (B2.oper_Time / 60) << " hrs." << endl;
cout << "Time of Operation for Bay 3 : " << (B3.oper_Time / 60) << " hrs." << endl;

cout << endl << "Ideal Time for Bay 1 : " << (B1.ideal_Time / 60) << " hrs." << endl;
cout << "Ideal Time for Bay 2 : " << (B2.ideal_Time / 60) << " hrs." << endl;
cout << "Ideal Time for Bay 3 : " << (B3.ideal_Time / 60) << " hrs." << endl;

cout << endl << "Average Time of Operation : " << ((B1.oper_Time + B2.oper_Time + B3.oper_Time) / (60*3)) << " hrs." << endl;
cout << "Average Ideal Time : " << ((B1.ideal_Time + B2.ideal_Time + B3.ideal_Time) / (60*3)) << " hrs." << endl;


system("pause");
return 0;
}

My Teacher is saying I'm missing:

But I think you want a BAY class vs. a struct, with your struct's variables included as data elements, and your BayOperation function should be a function of this class. Then make 3 instances of this BAYS class.

Although your current program executes it doesn't really seem to simulate the arrival and servicing of cars as required.

and

I don't see where you're using a queue structure in this program either as required.

WHAT AM I MISSING????????????? This is all object oriented as well.
Last edited on
threads... when you listen something about traffic ...
Last edited on
not sure i'm understanding you.
Then search on Google multithreading
you can also look for
dining philosophers problem c++
in Google .
ok, now i'm really lost. can you please tell me what i'm missing?
I will get a look to that question .
Last edited on
This is not Object Oriented program.
It is clear example of structured programming in C++. With some traces of C seasoned with bad habits as using global variables unnecessarily.
Also there is no queue (either standard one or whichever your class is using) here too. And its use was required.

And on top of that it does not do what is required. Currently it works like that: Each bay is independend (instead of belonging to same shop) and each car arrives in each bay 2-20 minutes after previous car leaves (instead of 2-20 minutes after previous carr arrives).

Threading won't do anything in this case, but introduce unneeded complexity. Do not even bother with them, it is too early for you to even think about it.

It seems (from te secondary requirements) that you are supposed to run minute-by-minute simulation of show work.
@MiiNiPaa
structured programming in C++
do you mean procedural programming ?

The thing about minute-by-minute is that you have to simulate 30 days . So I would suggest to create at least a method Convert() so that in real time a second is 15 min , or something.
do you mean procedural programming ?
No, this program doent reach level of procedural programming.

The thing about minute-by-minute is that you have to simulate 30 days .
Why? I am sure that even 50-years old computers can process all what could happen in given minute and move to processing next one dozens thousands times per second. We are not making a video game, so there is no need to bind processing minute in virtual shop should be boung to some real time duration. We want to process everything as fast as possible.
@MiiNiPaa,

How would you approach this then??
I would create class Bay, which would handle itself: won't expose any intrnals but allow other part of program to send requests to its interface (encapsulation: importartant part of OOP). You will need methods is_free (to check if bay is ready to accept next car), accept_car (to simulate sending car to it), process (to simulate minute passing — changing time till bay finishes servicing car, incrementing idle time, etc.) and get_idle_time (so we could request it).

Then I would create a function simulate_day which sumulates a passing of single day.

Main part of that function would be loop
1
2
3
4
5
6
7
8
9
10
11
12
13
const int end_time = 60 /*minutes per hour*/ * 12 /*work hours*/;
int current_time = 0;
while(current_time < end_time) {
    //Handle arriving cars — push them into queue, set time when next car is going to arrive

    //Send cars to avaliable bays. Find avaliable bay, send first car in queue in it
    //Do not forget to check waiting time for that car and handle your average calculations
    //Repeat until there is no free bays or there is no cars in queue

    //Run minute simulation for bays — call .process() for each bay

    ++current_time;
}
Currently I work on something without threads , it should be done soon.
try this . It is not 100 % complete. As you can see . The number of the cars are actually the time it was arriving at a bay (randomly chosen) . To fix that look in the main and try to avoid doublons . Tell me what you think of it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <windows.h>
#include <algorithm>
#include <time.h>

class Car
{
	DWORD m_time;
	Car() 
	{
		m_time = ( ( rand() % 19 ) + 2 );//range [2,20] minutes
	}

public:
	DWORD getTime(void) const
	{
		return m_time;
	}
	void setTime(DWORD _time)
	{
		m_time = _time;
	}
	static Car create(void)
	{
		return Car();
	}

};

class Station
{	
	struct Compare
	{
		bool operator()(Car const & p1, Car const & p2) 
		{
			return p1.getTime() > p2.getTime();
		}
	};
	std::priority_queue<Car , std::vector<Car> , Compare > m_cars;
	std::queue<Car> m_carsOnWaitingAtBay1;
	std::queue<Car> m_carsOnWaitingAtBay2;
	std::queue<Car> m_carsOnWaitingAtBay3;

public:
	Station() : m_cars() , m_carsOnWaitingAtBay1() ,m_carsOnWaitingAtBay2() ,m_carsOnWaitingAtBay3() {}
	void SimulateOneDay(unsigned int nCars)
	{
		std::cout << "Expecting " << nCars << " cars ..." << std::endl;
		size_t n = 0;
		for ( ; n < nCars ; ++ n)
			m_cars.push( Car::create() );

		int nHours = 24;
		const int timeMultiplier = 60;
		const int aDay = nHours * timeMultiplier ; // in  minutes 
		 int currentTime = 0;
		
		 int currentTimeAtBay1 = 0;
		 int currentTimeAtBay2 = 0;
		 int currentTimeAtBay3 = 0;

		while( currentTime ++ < aDay )
		{
			if(!m_cars.empty())
			{
				if(m_cars.top().getTime()  < currentTime )
				{
					std::cout << "Frame " << currentTime << " .Car n." << m_cars.top().getTime() << " arriving at bay n.";
				
					Car _car = Car::create();
					_car.setTime(m_cars.top().getTime());

					int Bay = 1 + (rand() % 3);

					switch(Bay)
					{
					case 1:
						std::cout << "1\n";
						m_carsOnWaitingAtBay1.push( _car);
						break;
					case 2:
						std::cout << "2\n";
						m_carsOnWaitingAtBay2.push( _car);
						break;
					case 3:
						std::cout << "3\n";
						m_carsOnWaitingAtBay3.push( _car);
						break;
					default:break;
					}
					
					m_cars.pop();
				}
			}

			if(!m_carsOnWaitingAtBay1.empty())
			{
				if( ++ currentTimeAtBay1 > 15 )
				{
					std::cout << "Frame " << currentTime  << " .Car n." << m_carsOnWaitingAtBay1.front().getTime() << " leaving bay n.1" << std::endl;
					m_carsOnWaitingAtBay1.pop();
					currentTimeAtBay1 = 0;
				}
				
			}
			if(!m_carsOnWaitingAtBay2.empty())
			{
				if( ++ currentTimeAtBay2 > 15 )
				{
					std::cout << "Frame " << currentTime  << " .Car n." << m_carsOnWaitingAtBay2.front().getTime() << " leaving bay n.2" << std::endl;
					m_carsOnWaitingAtBay2.pop();
					currentTimeAtBay2 = 0;
				}
				
			}
			if(!m_carsOnWaitingAtBay3.empty())
			{
				if( ++ currentTimeAtBay3 > 15 )
				{
					std::cout << "Frame " << currentTime  << " .Car n." << m_carsOnWaitingAtBay3.front().getTime() << " leaving bay n.3" << std::endl;
					m_carsOnWaitingAtBay3.pop();
					currentTimeAtBay3 = 0;
				}
				
			}
			
		}
	}
};

int main(int argc , char * argv[])
{
	srand((unsigned)time(NULL));
	{
		Station station;

		int nCarsThatDay = 1 + ( rand() % 100 ) ; // range [1 , 100]
		station.SimulateOneDay(nCarsThatDay);
	}
	system("pause");
	return EXIT_SUCCESS;
}
Last edited on
Further damage control inbound in the form of harsh solution critique.

@Ericool: We do not, as a matter of principle, give out full code solutions on this forum, and here's why: http://www.cplusplus.com/articles/DjGEy60M/

@Line 5: #include <windows.h>
There is no reason to have this header or any of the typedefs you are using in it in this code. In fact, there are reasons not to have it. How do you know that the OP isn't coding on a remote Linux machine?

@Line 12: Private default constructor for Car.
Why is this private? To screw up the people who want to use alternate allocation or placement new?

@Line 14: ( ( rand() % 19 ) + 2 )
Use of C-style random functions. However, since <random> is in fairness a bit beginner unfriendly and lesser-known, I'll cut some slack here.

@Line 26: static Car create(void)
This code would be pointless if your default constructor wasn't private. Seriously, why?

@Line 42: std::priority_queue<Car , std::vector<Car> , Compare > m_cars;
I suspect you are doing something very incorrect here. You are aware that priority_queue sorts, and the initial m_time value of the cars is the time each car arrives after the previous one, right? Admittedly, sleep deprivation isn't doing good things to me, but in either case, even if this is by some miracle correct, it is seriously overdesigned. An integer in Station's simulation loop that's randomly set from 2 to 20 would have sufficed, and probably been better.

@Lines 43-45: std::queue<Car> m_carsOnWaitingAtBayN;
How about a 3-long array of them queues?

@Line 48: Station() : m_cars() , m_carsOnWaitingAtBay1() ,m_carsOnWaitingAtBay2() ,m_carsOnWaitingAtBay3()
Redundant default constructor calls. This isn't Java.

@Line 74: int Bay = 1 + (rand() % 3); //Michael?
This is neither optimal nor realistic. Even if sending cars to bays is taken out of the station's hands, the drivers themselves will each tend to go for the shortest line. There's nothing that I saw in the problem prompt that called for random assignment.

@Lines 76-128:
And that's why the 3-long array of queues would have been better.

@Line 143: system("pause");
http://www.cplusplus.com/articles/iw6AC542/
This is typically a problem on Windows, caused by really dumb IDEs that don't know enough to keep the console open after the program finishes. However, it does strike right at one of the main philosophical problems with the way you are thinking about programming. After all, a console program should be run from the console

http://www.cplusplus.com/articles/j3wTURfi/
TL;DR: system() is not something that should be used here.

In other words, system("pause") is bad design both on a practical and philosophical level.

@Program:
This does not provide any sort of aggregate data that the prompt specifically called for, namely "the average waiting time for the cars and the total amount of idle time for the bays". It additionally does not simulate 30 days.

-Albatross
Last edited on
@TheRabbitologist

How would you approach this??
Well, the problem does have an ambiguity. The place has 3 bays, but is there one line per bay, or one line to all the bays? Whether there are or not results in an overall minor design change.

Either way, what I would put into each of those queues (or just the one queue) was the time (in minutes from the start of the day) at which a car arrived. A while or questionably-used for loop would do for the simulation, with every iteration representing one minute. Whenever a bay is open to take car from its queue, the timestamp for the car is removed from the queue and the time the car had to wait for service is computed, from which your average wait time can be calculated (make sure to keep track of the number of cars served).

For handling new cars' arrival, a variable could be used to count down minutes until the next car arrives. When it hits 0, a new car arrives, and its time is put down in the queue (or one of them). That variable is then set for sometime between 2-20, and it counts down again.

Do you have an idea of how to continue based on that?

-Albatross
As I said this is not complete .

For private constructors look at design patterns .

the include of Windows.h can be removed and system pause for pause ..,

For priority_queue I sort them by their arrival time . This part is important for the simulation.


For the 3-length array yes of course , I did a class for Bay but I removed it .

For rand() functions , you can look at c++11 uniform_distribution ..

Line 48 redundant since you have already mentionned that you would put it in a single array .

Line 74 : simply because my switch have case 1 , 2 ,3 to make it obvious which bay to choose , but we can use another algorithm in there for choosing which bay , since it was not specify in the question .

I regret that some people see prototype as release . My intention was to provide some code to start with not a complete source , because as you can see it simulate for a single day 100 random arrival cars.
For private constructors look at design patterns .
https://en.wikipedia.org/wiki/Law_of_the_instrument

For priority_queue I sort them by their arrival time . This part is important for the simulation.
If you add cats to queue as they arrive, they would be naturally sorted in it.

Additionally what you are doing does not make sense. Cars are not arriving all in [2;20] interval from the day beginning, they are ariving after each other. It is possible to have car enter in 2 minutes after opening, then another car after 20 minutes and another after 10.

Line 48 redundant since you have already mentionned that you would put it in a single array
No it is redundand because C++ guarantees to call default constructors if members are missing from initialization list. It has nothing to do with your program overrall structure.

we can use another algorithm in there for choosing which bay
So why use algorithm which most likely gives incorrect results.

My intention was to provide some code to start
By provideing code which does something other than was asked, you are confusing person asking for help further.
If you are going to write quick dirty plug, at leas comment it, so OP can replace it with something meaningful.

Your code does different thing from what was requested and is not easily adapted to work with original requirements (you will have to completely remake whole car arriving routine from scratch)
Then use threads , or come up with something working at least .
Last edited on
Pages: 12