473,414 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Problem with structure and dynamic array

how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus.
Your program should display the following data :
Id
First Name
Last Name
DestinationAddress
The id is to be incremented automatically. By default, it can be set
to 0.
*
* IMPLEMENTATION USING AN ARRAY
*/
#include <iostream>
using namespace std;

struct passenger{
int id;
char first_name[15];
char last_name[30];
char dest_add[20];
};

typedef passenger QueueElementType;

class Queue{
private:
QueueElementType * elements;
int MaxQueue;
int Front;
int Rear;
int fullqueue();
int emptyqueue();
public:
Queue(int MS);
void Enq(QueueElementType apassenger);
QueueElementType Deq();
void printQueue();
~Queue();
};

Queue::Queue(int MS){
elements = new QueueElementType;
MaxQueue = MS;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

Queue::~Queue(){
delete [] elements;
MaxQueue = 0;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

int Queue::fullqueue(){
return(Front = (Rear + 1) % MaxQueue);
}

int Queue::emptyqueue(){
return (Front == Rear);}

void Queue::Enq(QueueElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>apassenger.first_name;
cout<<endl<<"Enter passenger last name:";
cin>>apassenger.last_name;
cout<<endl<<"Enter destination address:";
cin>>apassenger.dest_add;
apassenger.id = 1;
elements[Rear] = apassenger;
}
}

QueueElementType Queue::Deq(){
QueueElementType mypass;
if (emptyqueue())
cout<<"Queue is empty";
else{
Front = (Front + 1) % MaxQueue;
mypass = elements[Front];
}
return(mypass);
}

void Queue::printQueue(){
for(int i = Front ; i<= Rear; i++){
cout<<elements[i].id;
cout<<elements[i].first_name;
cout<<elements[i].last_name;
cout<<elements[i].dest_add;
}
}
int main(int argc, char** argv)
{
Queue myqueue(5);
QueueElementType mypass;
myqueue.Enq(mypass);
myqueue.printQueue();

myqueue.Enq(mypass);
myqueue.printQueue();
return 0;
}
Jun 27 '08 #1
1 1767
On Wed, 07 May 2008 02:57:31 -0700, curiousEngine wrote:
how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus. Your
program should display the following data : Id
First Name
Last Name
DestinationAddress
The id is to be incremented automatically. By default, it can be set to
0.
Does your homework assignment specifically state that you aren't allowed
to use std::queue? Otherwise, you're re-inventing a (*very* wonky) wheel.

[...]
Queue::Queue(int MS){
elements = new QueueElementType;
Here you allocate *one* element to 'elements'. I think you probably meant:

elements = new QueueElementType[MS];
Queue::~Queue(){
delete [] elements;
Here you delete an *array* of elements. This is not going to work.
void Queue::Enq(QueueElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>apassenger.first_name;
cout<<endl<<"Enter passenger last name:";
cin>>apassenger.last_name;
cout<<endl<<"Enter destination address:";
cin>>apassenger.dest_add;
apassenger.id = 1;
elements[Rear] = apassenger; <------
Since you don't allocate an array of elements this is going to access an
unallocated element. Ditto many other places in your code.

This is a really bad attempt at implementing a queue. Either use
std::queue (*way* easier) or, if you have to implement it yourself, read
a good book on data structures (and C++) first - it's tricky.

--
Lionel B
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: simon | last post by:
From my previous post... If I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2; int iSomeNum1; int iSomeNum2;
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
6
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure...
2
by: Opteron64 | last post by:
Hi, I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed: (C++ code) typedef unsigned char uchar; typedef unsigned int uint; ...
1
by: =?Utf-8?B?VGhvbWFzUg==?= | last post by:
Hi together, I have following little problem with a structure which I need for unmanaged code: Public Structure Info Public Header As HeaderInfo ...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.