-
September 13th, 2012, 02:33 AM
#1
[RESOLVED] Help in Enqueue & Dequeue
9/14/2012 - I've not used any of the suggestions made by the members of the forums. I decided to fix the current problem I had instead.
Assignment
Use your language’s native queue class to write the following program: create a class, Patient. The class has 2 members: name of type string; wait_time, a non-negative integer. Read definitions of patients from a file and add the patients to a queue. Write a loop that dequeues a patient from the queue and then decrement the patient’s wait_time. If the patient’s wait time is greater than 0, then the program enqueues the patient again. Otherwise it discards the patient. The loop continues until the queue is empty.
I've worked the WHOLE day on this **** and it's not looking very good. It compiles and runs but after inputting stuff, the program stops responding. I'm not very good in data structures and my logic is so **** right now. Any from of help is appreciated.
Header File
Code:
#include <string>
class Patient
{
public:
Patient();
std::string name;
int wait_time;
bool empty() const;
private:
int count;
};
bool Patient::empty() const
{
//Post: Return true if the Queue is empty, otherwise return false.
return count == 0;
}
Main File
Code:
#include <stack>
#include "Patient.h"
#include <iostream>
using namespace std;
Patient::Patient()
{
name = name;
wait_time = 0;
count = 0;
}
void serve(string waiting_list[], int dequeue_in[], int maxperson, int rounds)
{
int serve_entry = rounds-1;
cout << "\n";
dequeue_in[serve_entry] = dequeue_in[serve_entry] - 1;
cout<< ">>P" << rounds <<" - "
<< waiting_list[serve_entry] << " : " << dequeue_in[serve_entry];
int next_name = serve_entry+1;
int next_wait = serve_entry+1;
for (; next_name < maxperson; next_name++, next_wait++)
{
cout<< "\n"<< "P" << next_name+1 <<" - "
<< waiting_list[next_name] << " : " << dequeue_in[next_wait];
}
cout << "\n";
}
void append(string waiting_list[], int dequeue_in[], int maxperson, int rounds)
{
int append_entry = rounds - 1;
for (; append_entry < maxperson; append_entry++)
{
string temp;
temp = waiting_list[append_entry]; //Holds the value for a bit
for ( int i = 1; i < maxperson; i++)
{
waiting_list[i-1] = waiting_list[i];
}
waiting_list[maxperson-1] = temp;
// int to_back = maxperson - 1 - append_entry;
// cout<< "\n"<< "P" << to_back+1 <<" - "
// << waiting_list[to_back] << " : " << dequeue_in[to_back];
}//maybe do a swap?
}
int main()
{
int total_patients;
Patient patients;
cout <<"How many patients total? ";
cin >> total_patients;
cout << "\n";
const int maxperson = total_patients; //Pass a varying variable to a constant like this.
string waiting_list[maxperson]; //Arrays always need a [size].
int dequeue_in[maxperson];
for (int i = 1, j = 0; i <= total_patients; i++, j++)
{
cout << "Enter patient #"<< i << "'s name: ";
cin >> patients.name;
waiting_list[i-1] = patients.name;
cout << "What is their wait time? ";
cin >> patients.wait_time;
dequeue_in[j] = patients.wait_time;
}
int rounds = 0;
for(int i=0;i < maxperson; i++)
{
rounds += dequeue_in[i];
}
cout << "Max rounds: " << rounds <<"\n\n";
/* for(int i = 0, j= 0; i < total_patients; i++, j++)
{
if (i < total_patients - 1)
cout<< waiting_list[i] << " : " << dequeue_in[j] << ", ";
else
cout<< waiting_list[i] << " : " << dequeue_in[j] << endl;
}*/
//Maybe put the round looping here instead.
for (int i = 1; i <= rounds; i++)
{
cout<< "\n"<< "_______Round " << i << " Serve________" << endl; //Rounds depend on the person with the most wait_time.
serve(waiting_list, dequeue_in, maxperson, i);
cout<< "\n"<< "_______Round " << i << " Append________" << endl; //Rounds depend on the person with the most wait_time.
append(waiting_list, dequeue_in, maxperson, i);
}
}
Last edited by Lethargic; September 14th, 2012 at 04:31 AM.
-
September 13th, 2012, 07:11 AM
#2
Re: Help in Enqueue & Dequeue
Originally Posted by Lethargic
I've worked the WHOLE day on this **** and it's not looking very good. It compiles and runs but after inputting stuff, the program stops responding. I'm not very good in data structures and my logic is so **** right now. Any from of help is appreciated.
You should **** the **** in the ****ing ****. (In other words, please refrain from using profanity; it's not helpful.)
Did you step through your program in the debugger? That's the first thing any of us would do to find the problem, so you should learn to use the debugger too. It's a mandatory part of learning C++.
Originally Posted by Lethargic
Code:
class Patient
{
public:
Patient();
std::string name;
int wait_time;
bool empty() const;
private:
int count;
};
bool Patient::empty() const
{
//Post: Return true if the Queue is empty, otherwise return false.
return count == 0;
}
The count variable and the empty function don't seem to make much sense here. What does it mean for a patient to be empty?
Originally Posted by Lethargic
Code:
int main()
{
int total_patients;
Patient patients;
cout <<"How many patients total? ";
cin >> total_patients;
cout << "\n";
const int maxperson = total_patients; //Pass a varying variable to a constant like this.
string waiting_list[maxperson]; //Arrays always need a [size].
int dequeue_in[maxperson];
This is not standard C++. In standard C++, you can only create an array of a fixed size that is known at compile time. The compiler cannot deduce the size of the array here. You need to use a std::vector.
Also, I didn't see you use a std::queue. The way I read the assignment, that's what is asked for.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
September 13th, 2012, 04:51 PM
#3
Re: Help in Enqueue & Dequeue
Originally Posted by D_Drmmr
You should **** the **** in the ****ing ****. (In other words, please refrain from using profanity; it's not helpful.)
Did you step through your program in the debugger? That's the first thing any of us would do to find the problem, so you should learn to use the debugger too. It's a mandatory part of learning C++.
The count variable and the empty function don't seem to make much sense here. What does it mean for a patient to be empty?
This is not standard C++. In standard C++, you can only create an array of a fixed size that is known at compile time. The compiler cannot deduce the size of the array here. You need to use a std::vector.
Also, I didn't see you use a std::queue. The way I read the assignment, that's what is asked for.
I've never used a debugger before. I'm pretty sure my program had to be object-oriented but I drifted off of that because I'm not used to it. So whatever class members I made it's just for show. For a patient to be empty, it's saying their wait time is equal 0. Can you show me an example of vectors and queue here?
-
September 13th, 2012, 07:21 PM
#4
Re: Help in Enqueue & Dequeue
Originally Posted by Lethargic
I've never used a debugger before.
Now is an excellent time to learn. Just search for a tutorial on debugging for the compiler (or IDE) you are using. Which compiler are you using?
Originally Posted by Lethargic
Can you show me an example of vectors and queue here?
First google hit for "std::vector example" and "std::queue example"... if you have any particular question about those examples (or any other example you find), I'd be happy to respond.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|