Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a factory production line around a single a conveyor belt.

Components (of type A and B) come onto the start of the belt at random

intervals; workers must take one component of each type from the belt
as they come past, and combine them to make a finished product.

The belt is divided into fixed-size slots; each slot can hold only one
component or one finished product. There are a number of worker

stations on either side of the belt, spaced to match the size of the
slots on the belt, like this (fixed-width font ASCII pic):
     v   v   v   v   v workers
   ---------------------
-> | A |   | B | A | P | -> conveyor belt
   ---------------------
     v   v   v   v   v workers


In each unit of time, the belt moves forwards one position, and there
is time for a worker on one side of each slot to EITHER take an item

from the slot or replace an item onto the belt. The worker opposite
them can't touch the same belt slot while they do this.
(So you can't have one worker picking something from a slot while
their counterpart puts something down in the same place).


Once a worker has collected one of both types of component, they can
begin assembling the finished product. This takes an amount of time,
so they will only be ready to place the assembled product back on the

belt on the fourth subsequent slot. While they are assembling the
product, they can't touch the conveyor belt. Workers can only hold
two items (component or product) at a time: one in each hand.

Create a simulation of this, with three pairs of workers. At each

time interval, the slot at the start of the conveyor belt should have
an equal (1/3) chance of containing nothing, a component A or a
component B.

Run the simulation for 100 steps, and compute how many finished

products come off the production line, and how many components of each
type go through the production line without being picked up by any
workers.


Implementation in C or C++?

What I have tried:

There are 4-5 classes:

class Worker
{

public:
int parts_required;
int time_Remaining;
int slave_worker;
int finished_slot;

void execute();


};


void execute()
{
	if(part a && part B)
	{
		if(time < 0)
		{
			print("Worker A is ready");
			
			if(empty_place)
			{
				cache = empty;
				clk = time_required;
				slot = item.worker a
			
			}
			else
			{
				run again
			}
		


}

class Source
{

public:

int item_a;
int item_b;


};

class Stage
{};


class Conveyor
{};
Posted
Updated 11-Dec-19 23:51pm
v2

Your code looks like a random assortment of thoughts, not a structured program.

First you should look at your objects. Going by the textual description, you have a production line that consists of one conveyor belt and multiple workers. That's three classes, right from the text:
C++
class Worker {
   // add details later
};
class Conveyor {
   // add details later
};
class ProductionLine {
   Conveyor the_conveyor;
   std::vector<Worker> workers;
   // add more details later
};

Second, check activities: the conveyor can move its belt, the workers can get items, put items, or assemble items. You should add functions for that:
C++
class Conveyor {
public:
   void move();
};
class Worker {
public:
   void getPart(Conveyor& conveyor);
   void putItem(Conveyor& conveyor);
   void assembleItem();
};

Next, there are conditions to be considered: a worker can't get an item while assembling, and can't assemble if it doesn't have the required parts, nor can it put an item while it hasn't assembled one. To check these conditions, you should add functions to query the current state of the worker:
C++
class Worker {
   ...
   bool isAssembling() const;
   bool hasAllParts() const;
   bool hasAssembly() const;
};

This should cover most of the interface you need for your classes. Now you can go into the details of the classes: what member variables do you need to manage their internal state, and process the functions that you specified?

Of course the above doesn't cover everything: e. g. I haven't bothered about where the workers are positioned, how parts are initially placed on the conveyor, or how to evaluate the queries specified in your task description. You will need to add more functions (and more member variables) for that.
 
Share this answer
 
Comments
Member 13999630 12-Dec-19 5:50am    
Hi Stefan,
Thanks for your mail.

Based on your initial code,i have created few lines of code based on below assumptions:

a) Parts are coming to convyor belt in fixed sequence a,b,a,b,null
b) Considering the complexity up till now i have made it as if there is only on workeron one side of conveyor belt(Need to generalize it for 6 workers(3 on either side).

I am also working on generalizing it.Please help me to improve that.I am not getting how to generalize it for 6 workers,3 on either side of conveyor belt

Once again thanks for help and cooperation:
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
So read your instructions, and work out what needs to be stored in each class. Then think about what processing each class needs to do on each object. If necessary, grab a pad of paper and do it manually, using separate pages for each class instance.

Working out how to store and process the data is a big part of this task - and it's up to you to learn that process, because it's fundamental to designing a working system - and if we do it for you then you learn nothing, and still can't do it next time. Since homework will almost certainly get more complex with time, you need to learn this on relatively simple stuff, because if you don't have it "down pat" by exam time, you will fail the course.
 
Share this answer
 
#include <string>
#include <iostream>
#include <vector> 
#include <list>

using namespace std;

enum {
	STATE_WORKING = 0,
	STATE_FINISHED = 1,
	STATE_SUBMITTED = 2
} WORKER_STATE;

typedef enum {
	CACHE_STATE_EMPTY = 0,
	CACHE_STATE_ONLY_PART_A = 1,
	CACHE_STATE_ONLY_PART_B = 2,
	CACHE_STATE_BOTH_PARTA_PARTB = 3
}CACHE_STATE;

class Conveyor {
public:
	string move();
	void init();
private:
	vector<string> parts;
	
};

void Conveyor::init()
{
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_A");
	parts.push_back("part_B");
	parts.push_back("part_NULL");


		
}

string Conveyor::move()
{

	/*
	list <string> ::iterator it;
	for (auto it = parts.begin(); it != parts.end(); ++it)
		cout << '\t' << *it;
	cout << '\n';
	*/
	
	string partno;
	partno = parts.front();

	parts.erase(parts.begin());
	
	return partno;

}

class Worker {
public:
	void getPart(Conveyor& conveyor);
	void putItem(Conveyor& conveyor);
	void assembleItem(string part_a, string part_b);
	bool isAssembling() const;
	bool hasAllParts() const;
	bool hasAssembly() const;
private:
	int parts_reqd;
	int time_reqd;
	string slave;
};

void Worker::getPart(Conveyor& conveyor)
{
	bool cache_empty = true;
	CACHE_STATE cachestate;
	string incoming_part;
	
	incoming_part = conveyor.move();
	if (strcmp(incoming_part.c_str(), "part_A") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_A; 
		cache_empty = false;
	}

	if (strcmp(incoming_part.c_str(), "part_B") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_B;
		cache_empty = false;
	}
	if (strcmp(incoming_part.c_str(), "part_NULL") == 0)
	{
		cachestate = CACHE_STATE::CACHE_STATE_EMPTY;
		cache_empty = true;
	}
	
}

void Worker::putItem(Conveyor& conveyor)
{
	unsigned int finished_part = 0;
	/*string partcoming;
	
	partcoming = conveyor.move();*/
	
	finished_part++;
	cout << "No of finished part = ", finished_part;


}

void Worker::assembleItem(string part_a,string part_b)
{
	string fin_part;
	unsigned int time_taken = 0;
	fin_part = part_a + part_b;
	time_taken = 5;
}

class ProductionLine {
public:
	Conveyor the_conveyor;
	std::vector<Worker> workers;
	void runprodline();
};

void ProductionLine::runprodline()
{
	string part[100];
	string finished_part;
	Worker workerone;

	CACHE_STATE state;
	unsigned int assembled_part = 0;
	the_conveyor.init();
	for (int i = 0; i < 100; i++)
	{
		part[i] = the_conveyor.move();
		if (strcmp(part[i].c_str(), "part_A") == 0)
			state = CACHE_STATE_ONLY_PART_A;
		if ((strcmp(part[i].c_str(), "part_B") == 0) && (state == CACHE_STATE_ONLY_PART_A))
			state = CACHE_STATE_BOTH_PARTA_PARTB;

		if (state == CACHE_STATE_BOTH_PARTA_PARTB)
		{
			assembled_part = assembled_part + 1;
		}
			
		if ((strcmp(part[i].c_str(), "part_NULL") == 0) && (assembled_part>0))
		{
			workerone.putItem(the_conveyor);
			assembled_part = 0;
		}

	}
}

int main()
{
	ProductionLine obj_prodline;
	obj_prodline.runprodline();
}
 
Share this answer
 
Comments
Stefan_Lang 12-Dec-19 11:13am    
Hello

I have only now noticed your response, and the code you posted here.

On first glimpse, this looks like a good starting point. There are a few things i would change before going on:

1. I would encode the different parts as integer values or enums, not strings.
2. You should implement a function that generates a random part (A, B, or nothing), instead of a fixed number of predefined items. That makes your program more flexible if you want to run it for an extended time.
3. the conveyor needs to maintain a list of positions on its belt, where each position can hold part A, part B, an assembled item, or nothing.
4. The worker should be assigned to a position on the belt. Each worker gets a different position and stores it when you set up the production line. Every time your worker interacts with the conveyor, he should pass his position as a function parameter, so the conveyor knows what belt position it needs to refer to.

I'm a bit in a hurry right now, but maybe that is enough to extend your program to use multiple workers.
Member 13999630 13-Dec-19 21:30pm    
Hi Stephen

Thanks for your help.I will complete the part 2.If you could help me in completing the part 3,4 as you have described and also reviewing the code that i have developed it will be great.I need to submit this before Monday.

Many many thanks for your help and guiding me.

Thank you

Best Regards
Mayank
Member 13999630 14-Dec-19 11:50am    
Hi Stefan,

Thanks for your continuous support.
Please help me improve my solution:


#include <string>
#include <iostream>
#include <vector>
#include <list>#include<ctime>
#include <math.h>

using namespace std;

#define NULL 0

enum {
STATE_WORKING = 0,
STATE_FINISHED = 1,
STATE_SUBMITTED = 2
} WORKER_STATE;

enum CACHE_STATE {
CACHE_STATE_EMPTY = 0,
CACHE_STATE_ONLY_PART_A = 1,
CACHE_STATE_ONLY_PART_B = 2,
CACHE_STATE_BOTH_PARTA_PARTB = 3
};

class Conveyor {
public:
string move();
private:
vector<string> parts;


};

std::vector<string> randomize()
{
srand(time(0));
string choices[] = { "Part_A", "Part_B", "Part_A" };

std::vector<string> random;
for (int i = 0; i < 3; i++)
{
random.push_back(choices[rand() % 3]);
}
return random;
}

string Conveyor::move()
{

srand(time(0));

randomize();
std::vector<string> randomResult = randomize();

return randomResult[0];


/*
for (iter = randomResult.begin(), iterEnd = randomResult.end();
iter != iterEnd; ++iter)
cout << *iter << endl;

return *iter;


string partno;
partno = parts.front();

parts.erase(parts.begin());

return partno;
*/

}

class Worker {
public:
void getPart(Conveyor& conveyor);
void putItem(Conveyor& conveyor);
void assembleItem(string part_a, string part_b);
bool isAssembling;
bool hasAllParts;
bool hasAssembly;


int cachestate;
private:
int parts_reqd;
int time_reqd;
string slave;
int worker_position;

};

void Worker::getPart(Conveyor& conveyor)
{
bool cache_empty = true;
CACHE_STATE cachestate;
string incoming_part;

incoming_part = conveyor.move();
if (strcmp(incoming_part.c_str(), "part_A") == 0)
{
cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_A;
cache_empty = false;
}

if (strcmp(incoming_part.c_str(), "part_B") == 0)
{
cachestate = CACHE_STATE::CACHE_STATE_ONLY_PART_B;
cache_empty = false;
}
if (strcmp(incoming_part.c_str(), "part_NULL") == 0)
{
cachestate = CACHE_STATE::CACHE_STATE_EMPTY;
cache_empty = true;
}

}

void Worker::putItem(Conveyor& conveyor)
{
unsigned int finished_part = 0;

finished_part++;
cout << "No of finished part = ", finished_part;


}

void Worker::assembleItem(string part_a,string part_b)
{
string fin_part;
unsigned int time_taken = 0;
fin_part = part_a + part_b;
time_taken = 5;
}

class ProductionLine {
public:
Conveyor the_conveyor;
std::vector<worker> workers;
void runprodline(unsigned int pairs,int no,string ar[2]);
};

void ProductionLine::runprodline(unsigned int worker_pairs,int totalworkers,string partposns[2])
{
string part[100];

Worker workerone;

Conveyor objConv;
unsigned int i = 0;


unsigned int assembled_part = 0;
unsigned int elapsed_time_part_complete = 0;

unsigned int finished_part = 0;
Worker workeronline[2];
Worker worker_two;

cout << "Part no 0 = " << partposns[0] << endl;
cout << "Part no 1 = " << partposns[1] << endl;
cout << "Part no 1 = " << partposns[2] << endl;

workeronline[1].hasAllParts = false;
workeronline[1].isAssembling = false;

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

for (int workerposition = 0; workerposition < 2; workerposition++)
{
if (strcmp(partposns[i].c_str(), "part_A") == 0)
{
workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_A;

partposns[i] = "";
}
if (strcmp(partposns[i].c_str(), "part_B") == 0)
{
workeronline[workerposition].cachestate = CACHE_STATE_ONLY_PART_B;

partposns[i] = "";
}
if( (strcmp(partposns[i].c_str(), "part_A") == 0) && (workeronline[workerposition].cachestate == CACHE_STATE_ONLY_PART_B))
{
workeronline[workerposition].cachestate = CACHE_STATE_BOTH
Stefan_Lang 18-Dec-19 2:57am    
Hello

For some reason your last two comments didn't show up for me until now.

I've seen the other thread you've opened. At this point I should point out that it is against forum rules to open multiple posts on the same question. I suppose it's not that bad this time, since you now have much more code to look at, and that changes the nature of the question.

However, with that much code, you're starting to strain the limits of this forum: you are supposed to ask concise questions that can be answered with a few sentences at most. You however are asking for a full blown analysis and tutoring. That takes a lot of time, and no one gets paid for doing that.

You haven't mentioned where you got this task from, but whereever that is, are there not people you could ask instead? I'm suggesting this because these people might in fact have an interest to help you, or are even paid to do so.

If you got this task from a website, then let me tell you that you should start with simpler tasks instead. For this kind of task you should have a good idea on how to analyze specifications, how to identify objects, types, and data that are important for your project, and how to split up the individual tasks across functions and classes. To be honest, you are not showing any of these qualifications. You clearly need more experience on how to write simple functions and programs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900