hi guys , how to correct add two queues(arrays) inside of operator overloading? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

hi guys , how to correct add two queues(arrays) inside of operator overloading?

How to do that properly to refer and add members value inside of operator overloading, which is the same array? https://code.sololearn.com/cszS65vqc673/?ref=app

22nd Jul 2021, 10:15 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
11 Answers
+ 2
If you follow the task description, you are not fulfilling the task. Instead of storing the elements from the first queue, followed by the elements from the second queue, you are adding the elements. The correct output would be: 42 <- 2 <- 8 <- 1 <- 3 <- 66 <- 128 <- 5 To do this, you will need two consecutive loops, one to copy the elements of the first queue into the fields at indices [0, this->size), and one to copy the elements of the second queue into the fields at indices [this->size, Q.size). Edit: I switched q and Q. The second range should be [this->size, q.size).
23rd Jul 2021, 6:51 AM
Shadow
Shadow - avatar
+ 2
Yes, the issue is the second loop. You are trying to access the second queue at the indices [this->size, Q.size), but the elements of the second queue will be at the indices [0, Q.size). The size of the first queue is irrelevant to the second queue. For example, if i = this->size = 4, the loop won't execute at all because Q.size = 4 is already greater or equal to i. What you would have to do is to copy the elements of the second queue, located at indices [0, Q.size) in the original array, to the indices [this->size, q.size) in the new array. You can do this by iterating over the original array as you would normally from beginning to end, and adding this->size to the current index when accessing the new array: ... q.queue[ this->size + i ] = Q.queue[ i ]; // i = 0, ..., Q.size - 1 ...
23rd Jul 2021, 1:57 PM
Shadow
Shadow - avatar
+ 1
Why are you adding both queues size here, q.size = this->size + Q.size ?? Only use either one , "this" is q1 and "Q" is q2
22nd Jul 2021, 10:26 PM
Abhay
Abhay - avatar
0
how to use one size indide of oparator overloading
22nd Jul 2021, 10:30 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
this is my task You are asked to add a new functionality: adding two queues together. The result should be a new queue, where the elements of the first queue come first, followed by the second queue's elements. Given the Queue class, overload the + operator, so that the code in main works and successfully adds two queues.
22nd Jul 2021, 10:45 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
I have already done it yesterday firstly i did not get what I am doing wrong ,but after i created loops and trying to put second queue after first
23rd Jul 2021, 7:42 AM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
but with loops have not done properly every time when initialize the second queue i had a junk at the second part of array
23rd Jul 2021, 7:57 AM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
https://code.sololearn.com/cszS65vqc673/?ref=app I have changed it what i’ve commented, it really works , but two loops are not!
23rd Jul 2021, 8:04 AM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
Yes it works, I got you q.queue[this->size+i] = Q.queue[i] Firstly we are preparing place for putting in second array and free places start from (this->size+i) this equals to 4 and gonna grow while loop is increasing and at same time we put ours values from 0 to 4 on that places from 4 to 7. Thanks a lot for help Shadow!!
23rd Jul 2021, 3:43 PM
Aleks kryzhanivskyi
Aleks kryzhanivskyi - avatar
0
Thanks for your help, guys! It's easy to overthink this one, but all one must really do is pay very close attention to what's taking place within each loop. Also, recognizing how to access elements of each queue was tricky.
14th Dec 2021, 10:14 PM
Sloane Tribble
Sloane Tribble - avatar
0
Remember there is already a function called add(); which will add a new element to an array whenever it is called. I used 2 for loops to repeatedly call the add() function as it iterates over the size of each array. Here is my function def: Queue operator+(Queue q1,Queue q2) { Queue newQ; for(int i=0;i<q1.size;i++) { newQ.add(q1.queue[i]); } for(int i=0;i<q2.size;i++) { newQ.add(q2.queue[i]) } } Make sure you declare the overloaded operator as a "friend" to class Queue because the function will take 2 different parameters... The declaration should be located INSIDE the class. Then you can define it OUTSIDE of the class as a "free" function. This is mandatory if you want to pass 2 arguments to operator+, otherwise you will get a compiler error. Let me know if anyone has questions :)
2nd Jan 2022, 12:35 PM
Alex Davis