+ 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) }
17 odpowiedzi
+ 48
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.
+ 26
My solution used the add() function, so there is no need to manually increment the size.
https://code.sololearn.com/c9Qk2ZB4kHO7/?ref=app
+ 19
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!
+ 6
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;
}
+ 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?
+ 1
Thank you so much KOISHI_IS_HERE
0
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;
}
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;
}
0
ok
0
Queue operator+(Queue &obj){
for(int i = 0; i < obj.size; i++, this->size++)
this->queue[this->size] = obj.queue[i];
return *this;
}
Simple alternative also appending q2 to q1 and returning it like some did.
- 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
- 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 &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;
}
- 3
Visit here for solution :-
https://code.sololearn.com/c9a210a21A5a
- 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;
- 7
void add(int a){
queue(size)=a;
size++;
}
- 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;
}