+ 1

Hello, I'm having a hard time on codecoach with "queue management part 1". Can you please help me???

It tells me that I need to make an "add();" but I did. Infact I have all of the code, but won't output the correct answer after I've changed the code

5th Jan 2021, 9:22 PM
Shapa
Shapa - avatar
19 Answers
+ 2
Hello John Robotane, I hope you are ok, I would like to know why the size starts with the number 1 and goes to the number 4. It is not defined in the add function the beginning of this variable. The variable i is equal to 42 next 2 , 8 and 1. I appreciate your help with this part of the code
14th Apr 2021, 2:31 AM
Jaime Tovar
Jaime Tovar - avatar
+ 2
Thank you very much for your time and help John Robotane, the last questions that I have are these: queue[size] = i// line 51 on Shapa´s code. means that everytime that somebody adds an item this value will be stored in the variable i? for (int i = 0; i < size - 1; i++) // line 32 on Shapa´s code. when I changed the condition to size without -1, the code does not vary and runs the same with the condition size-1.
18th Apr 2021, 12:18 AM
Jaime Tovar
Jaime Tovar - avatar
+ 1
the end of the queue is 'queue[size]', so add 'i' to the queue at the index 'size' with: queue[size] = i; then increment the size with; size ++; in fact what I mean by 'the end of the queue' is the tail of the array 'queue'!
5th Jan 2021, 11:31 PM
John Robotane
John Robotane - avatar
+ 1
hi Jaime Tovar, at the initialization, the size is 0 (line 23 of Shapa code above). it changes when we add or remove an item: each time an item is added to the queue, the size is incremented (line 52). then, when an item is deleted, the size is decreased (line 35). so, when you'll call 'q.add(42)' the size will be incremented to 1 (with size ++), this means that the queue has one item. likewise, adding 2 the the queue will increment the size to 2. after calling 'q.add' 4 time without removing an item, the size will be 4. the size depends on the number of elements of the queue, we don't have to define a range for that. but the queue itself is an array of 100 integers, so the max size will be 100 and it will be reached only when 100 items will be added to the queue.
17th Apr 2021, 11:34 AM
John Robotane
John Robotane - avatar
0
can you share your code link? we will try to help you to fix it.
5th Jan 2021, 9:24 PM
John Robotane
John Robotane - avatar
5th Jan 2021, 9:50 PM
Shapa
Shapa - avatar
0
you haven't replied to the question: your 'add' function is just a clone of the print function. what you have to do is just to add the parameter of your function, ('i' in your case) at the end of the queue (using size) and update the size (increment it has you have added a new element to the queue).
5th Jan 2021, 10:12 PM
John Robotane
John Robotane - avatar
0
My function "I"??? That's just my Paremeter in all the other functions. I still don't get it.
5th Jan 2021, 10:52 PM
Shapa
Shapa - avatar
0
Hello???
5th Jan 2021, 11:16 PM
Shapa
Shapa - avatar
0
yes, I said that 'i' is the parameter. have you tried what I suggested?
5th Jan 2021, 11:22 PM
John Robotane
John Robotane - avatar
0
At the end of the queue class or constructor?
5th Jan 2021, 11:26 PM
Shapa
Shapa - avatar
0
This is so annoying and confusing 😭
5th Jan 2021, 11:30 PM
Shapa
Shapa - avatar
0
What line??
6th Jan 2021, 11:27 AM
Shapa
Shapa - avatar
0
it should look like this: //your code goes here void add(int i){ queue[size]=i; size++; }
6th Jan 2021, 12:24 PM
John Robotane
John Robotane - avatar
0
OH BTW, THANK YOU!!!
8th Jan 2021, 12:52 AM
Shapa
Shapa - avatar
0
I got it, but I still don't understand how exactly, but no worries, I'll continue to study hard.
8th Jan 2021, 12:52 AM
Shapa
Shapa - avatar
0
for your first question, 'i' is a parameter, when an item is added with q.add(42), as you said, this value will be temporarily stored in 'i', but on line 51 we store this value in the queue (which is a list) so that we can retrieve it back later. for the second, this is due to the fact that 'queue' is a list of 100 int. so, as here we have just max 4 items, even if we replace 'size - 1' with 'size + 10', we won't see a difference but it's incorrect. indeed, imagine we fill the list with 100 items, what will happen when 'i' will be 99? we will have something like this queue[99] = queue[100]; which is impossible, the max index for a list of 100 items is 99, this why we stop the loop at i<size-1 which means i<=size-2.
19th Apr 2021, 8:35 AM
John Robotane
John Robotane - avatar
0
Thank you so much John Robotane, your explanations are quite helpful and now I understand this code better.
19th Apr 2021, 12:30 PM
Jaime Tovar
Jaime Tovar - avatar
- 3
I don't think I can share a code coach link
5th Jan 2021, 9:49 PM
Shapa
Shapa - avatar