• 11-24-2008
aniramg69
Hello!
I don't know if you can help me but I've got an assignment that I can't solve.

Suppose we have 2 ferry boats, S and Q (stack and queue implementation). We have to build a program with the following conditions:
there's a file to read data (carplates)

car0.txt
NBE1016 S<new line>
XKZ2016 Q<new line>
IPN1017 S<new line>
PP21017 Q<new line>
NNB2018 Q<new line>

then suppose that at first, ALL cars from dock fill boat S (which ships when boat is full or there's no car left in the dock)

and travell until the middle dock where ALL cars leave S,and a new file is written with the new order.

Then, cars for Q fill that boat (which ships when boat is full or there's no car for Q left in the dock) until the final destination.

We MUST use the following structures:
Code:

```/* Node structure */ typedef struct dnode {     char *Name; /* CAR ID */     struct dnode *Next; /* Pointer to next node in the linked-list */ } DNODE; /* Stack structure type */ typedef struct Stack {     int Capacity; /* the max elements stack is permitted to hold */     int NumElements; /* current number of elements in stack */     DNODE *Top; /* pointer to top node */ } STACK; /* Queue structure type */ typedef struct Queue {     int Capacity; /* the max elements in queue */     int NumElements; /* num of elements in queue */     DNODE *First; /* pointer to first node */     DNODE *Last;  /* pointer to last node */ } QUEUE;```

We run the program with the following arguments:
./as.exe <capacity of S> <capacity of Q> cars0.txt out0_s out0_q

eg
./as.exe 2 1 cars0.txt out0_s out0_q

and the output must be
out0_s0.txt
XKZ2016<new line>
NBE1016<new line>

out0_q0.txt
XKZ2016<new line>

out0_q1.txt
NBE1016<new line>

out0_s1.txt
PP21017<new line>
IPN1017<new line>

out0_q2.txt
PP21017<new line>

out0_q3.txt
IPN1017<new line>

out0_s2.txt
NNB2018<new line>

out0_q4.txt
NNB2018<new line>

BUT I MESSED UP everything ....

I attach my code so far.................

Thanks!
• 11-24-2008
itCbitC
IMHO figure out the difference between C source and header files.
Are you sure that stack.h and queue.h are indeed headers or do they need to be source files?
• 11-25-2008
aniramg69
Yes, that's how we've been told to build them...
So, we must follow the rules!

Thanks.
• 11-25-2008
laserlight
Quote:

Originally Posted by aniramg69
BUT I MESSED UP everything ....

How does it not work? If you really did mess up everything, then restart from scratch, but this time do one part, test, and then do another part, test, etc.
• 11-25-2008
itCbitC
I would heed laserlight and start from scratch as the whole thing is messed up.
This time take care to separate the statements that should go into the source file (as3b.c)
from the ones that should go into the header files (stack.h and queue.h).
• 11-26-2008
newprogr80
Did u find the solution finally? i have the same problem in my exercise.
• 11-26-2008
itCbitC
For the sake of everyone could you post the problem. It's not completely clear to me what the OP has in mind.
• 11-29-2008
aniramg69
I've made some improvements to the code
BUT the result is not what I want...

here it is

I'd appreciate some real help...
thanks
• 11-29-2008
MK27
I think things would be easier for everyone, especially yourself, if you describe more clearly what you are supposed to do AND how it is, specifically in relation to what you wrote, that you intend to get it done.

For example, in your original post you mention "a new file is written with the new order". What new order? All you've said is that a line of cars drive onto one ferry, go somewhere, drive off, and get onto another ferry. Why would their order change? You also mention other details that may be important, but maybe not, such as "when boat is full or there's no car for Q left". Are any cars left seperate at some point? Are the ferries the same size? Since you claim your program doesn't work the way you want it too, there is not much point in trying to figure out what you wanted by looking at that, is there?

The first thing I noticed when I looked at as3b.c was this:
Code:

```fpin=openFile(argv[3], "r"); [...] FILE *openFile(char *filename, char *mode) {   FILE *fp;   fp= fopen(filename, mode);   if (fp==NULL){       printf("Cannot open file: %s.\nExiting...\n", filename);       exit(1);       return NULL;   }   else{       return fp;   } }```
Why would you call exit, and then return??? What's suppose to happen here:
1) The program ends.
2) The function ends.
3) The function returns a NULL pointer.
It certainly won't be the last case, anyway. Do you see now how trying to deduce intentions from code you already know is flawed might be a waste of time?

This would be better, perhaps, as this (I'm guessing as to your intentions):
Code:

```if ((fpin=openFile(argv[3], "r"))==NULL) {     printf("Cannot open file: %s.\nExiting...\n", argv[3]);     exit (1); } [...] FILE *openFile(char *filename, char *mode) {   FILE *fp;   fp= fopen(filename, mode); }```
Hmmm, looks like "openFile" is really just fopen! Maybe this would have been fine then:
Code:

```if ((fpin=fopen(argv[3], "r"))==NULL) {     printf("Cannot open file: %s.\nExiting...\n", argv[3]);     exit (1); }```
My confidence in your ability to think, much less program a computer, is fading fast. How many other superfluous functions are in here? Before I go any further, you will have to answer my questions.
• 11-29-2008
aniramg69
Suppose

Port 1: there are cars waiting for the ferry: A, B, C, D, E

the ferry "STACK" comes in and lets say its capacity is 2 - so car A gets in and then car B. The ferry sails and reaches the next port 2. Cars must leave the ferry in the NEW ORDER of the stack: first car B and then car A.

Now there is another ferry "QUEUE" waiting for cars with capacity lets say 1. Car B gets in, ferry QUEUE sails reaches a third port, car B leaves QUEUE. QUEUE goes back to port 2, car A gets in, QUEUE sails to port 3, car A gets out and QUEUE goes back to port 2.

A new cycle begins at port 1. the ferry STACK (with capacity 2) sails with car C and car D
etc etc...

So, the files created have to look like this:

out0_s0.txt
carB
carA

out0_q0.txt
carB

out0_q1.txt
carA

out0_s1.txt
carD
carC

out0_q2.txt
carD

out0_q3.txt
carC

out0_s2.txt
carE

out0_q4.txt
carE

the concept is to implement the idea of stacks and queues
of course the number of cars is bigger, so is capacity of STACK and QUEUE

thanks
• 11-29-2008
MK27
Now, how had you meant to do this using the code you have? Try going through all the functions you wrote and -- referring to them by name -- explain the intended purpose of the function, how it is called and fits into the flow of the program, and whether you think it accomplishes this purpose. Then we can get closer to helping you solve all your problems.

I'm going out now but I will probably be back in the next 24hrs.