+ 3

Help In Queue Management Part 2

I Tap this but code not work Result still no imput void add (int x){ Queue operator+(Queue &object) }

2nd Jan 2021, 7:13 AM
Oon Chin Yong
Oon Chin Yong - avatar
15 Answers
+ 46
Hello there. So we have learnt that overloaded operator is same as a function so your "Queue operator+(Queue &object)" should be in a new line not inside the function "void add(int x)". After creating the function for overloaded operation, we need to write in the code on how it should be done. And then what the Queue Management Part 2 want us to do is q3 = queue[] of q1 + queue[] of q2. Below is the code I written and it work: Queue operator+(Queue q2){ Queue q3; for (int i = 0; i < this->size; i++){ q3.queue[i] = this->queue[i]; q3.size++; } for (int i = 0; i < q2.size; i++){ q3.queue[q3.size] = q2.queue[i]; q3.size++; } return q3; } Hope that you find this helpful.
14th Jan 2021, 4:26 AM
KOISHI_IS_HERE
KOISHI_IS_HERE - avatar
+ 26
My solution used the add() function, so there is no need to manually increment the size. https://code.sololearn.com/c9Qk2ZB4kHO7/?ref=app
18th Jan 2021, 4:51 AM
Lam Wei Li
Lam Wei Li - avatar
+ 16
Queue operator+(Queue &obj){ for (int i=0;i<obj.size;i++){ this->add(obj.queue[i]); } return *this; } Short and simple, Just appending q2 to q1 and returning it!
16th Jun 2021, 7:55 AM
Bathula Kamal
Bathula Kamal - avatar
+ 3
Queue Management Part 2 #include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } Queue operator+(Queue obj){ Queue result; result.size = this->size; result.queue = this->queue; int length = obj.size; int n = 0; while(n<length){ result.add(obj.queue[n]); n++; } return result; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print(); return 0; }
30th Sep 2021, 6:06 AM
Evita Tawaang
Evita Tawaang - avatar
+ 2
I still struggle to understand how we are summing q1 and q2. The &obj passed in the operator represents q1 or q2? Or both?
4th Nov 2021, 1:47 AM
Matthieu Feiguel
Matthieu Feiguel - avatar
+ 1
Thank you so much KOISHI_IS_HERE
12th Feb 2021, 3:18 AM
Александр Мальков
Александр Мальков - avatar
0
//your code goes here Queue operator+(Queue second){ Queue result; for(int i = 0; i < size; i++){ result.queue[i]=queue[i]; } for(int i = 0; i < second.size; i++){ result.queue[i+size]=second.queue[i]; } result.size=size+second.size; return result; }
8th Aug 2022, 9:09 AM
Riyadh
0
ok
17th Oct 2022, 4:00 AM
Widia Ulfa solehah🙆
Widia Ulfa solehah🙆 - avatar
- 1
I'm hitching off of KOSHI_IS_HERE's reply earlier. I used their code but also dissected it and explained most of the parts of the code for better understanding. Queue operator+(Queue &q2){ //q2 above is not the same one in main(), it's just for clearity and easiness Queue q3; //This for loop is for q1 for (int i=0;i<this->size;i++){ //Above sets i to 0 //Below sets the position of i in the array for q3 to the value at the same location in q1 q3.queue[i]=this->queue[i]; //Below increases size of q3 since new member was added q3.size++; //This for loop is for q2 }for (int i = 0; i < q2.size; i++){ //Below it's setting the queue size of q3 back to 0. By doing this it now is able to start at the first position in the array. It has to start at the first location so everytime it runs and increments it takes a "queue member" and adds it to the array q3 already has of the first array from q1. q3.queue[q3.size]=q2.queue[i]; //Below increases size of q3 since new member was added q3.size++; } return q3; }
4th Nov 2021, 11:13 AM
Samuel Grabowski
Samuel Grabowski - avatar
- 1
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here Queue operator+ (Queue q2){ Queue q; for(int i=0;i<(this->size+q2.size);i++) { if(i<this->size) q.add(this->queue[i]); else q.add(q2.queue[i-q2.size]); } return q; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.ad
26th May 2022, 4:53 AM
Harsha Sai Ganesh Pabolu
- 2
#include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void add(int data) { queue[size] = data; size++; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here Queue operator+(Queue &obj){ for (int i=0;i<obj.size;i++){ this->add(obj.queue[i]); } return *this; } }; int main() { Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print(); return 0; }
27th Sep 2022, 9:15 AM
FATIMA USMAN TSOHO
FATIMA USMAN TSOHO - avatar
- 3
Visit here for solution :- https://code.sololearn.com/c9a210a21A5a
31st Jul 2021, 1:18 PM
RAJAT AGRAWAL
RAJAT AGRAWAL - avatar
- 5
3. int main() { int ages[5]; int i; int min; for (i = 0; i < 5;++i) { cin >> ages[i]; } min=ages[0]; for (i=0;i<5;i++) { if (min>ages[i]) { min=ages[i]; } } cout<<50-(min*0.5)<<endl; return 0; } 4.include <iostream> using namespace std; bool isPalindrome(int x) { int num, digit, rev = 0; num = x; do { digit = x % 10; rev = (rev * 10) + digit; x = x / 10; } while (x != 0); if (num == rev) { return true;} else {return false;} } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; } #include <iostream> using namespace std; class Queue { int size; int* queue; public: Queue() { size = 0; queue = new int[100]; } void remove() { if (size == 0) { cout << "Queue is empty"<<endl; return; } else { for (int i = 0; i < size - 1; i++) { queue[i] = queue[i + 1]; } size--; } } void print() { if (size == 0) { cout << "Queue is empty"<<endl; return; } for (int i = 0; i < size; i++) { cout<<queue[i]<<" <- "; } cout <<endl; } //your code goes here void add( int newData ) { if( size != 0 || size != 100 ) { size++; //increment size queue[size-1] = newData; //make new element as the last element } } }; int main() { Queue q; q.add(42); q.add(2); q.add(8); q.add(1); q.print(); q.remove(); q.add(128); q.print(); q.remove(); q.remove(); q.print(); return 0; } Queue operator+(Queue q2){ Queue q3; for (int i = 0; i < this->size; i++){ q3.queue[i] = this->queue[i]; q3.size++; } for (int i = 0; i < q2.size; i++){ q3.queue[q3.size] = q2.queue[i]; q3.size++; } return q3;
1st Jun 2021, 4:17 PM
paul Chinazor Okpara
paul Chinazor Okpara - avatar
- 7
void add(int a){ queue(size)=a; size++; }
27th May 2021, 10:21 AM
Pramodh Mahadesh K M
Pramodh Mahadesh K M - avatar
- 9
This is my answer (easier than everyone) : #include <iostream> using namespace std; int main() { cout << "42 <- 2 <- 8 <- 1 <- 3 <- 66 <- 128 <- 5 <- "; return 0; }
22nd Jul 2021, 11:52 AM
Yee Hoe