Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program. Please help me figure it out, I solve the print method but fail in

ID: 3579421 • Letter: C

Question

C++ program. Please help me figure it out, I solve the print method but fail in  the last method. I post this question on chegg several times but none of it run perfectly, so I upload this test case. Thank You!

Copy and paste the code below and complete the necessary portions. The Number class will represent a number, except that number will be stored in an array. For example, the number 3278 would be stored as [3, 2, 7, 8].

class Number {

private:
int *arr;
int index;
int size;

public:

Number(int s) {
arr = new int[s];
// Fill in the rest of this constructor as needed.
}

void addDigit(int dig) {
// ex. arr currently = [1, 2], addDigit(5) is called, arr should = [1, 2, 5]
}

void printNumber() {
// print the number such as 124 etc.
}

bool operator>(const Number& n) {
// Your Logic here
}
};

Answer:

Thank You

Test Expected Number n (2); 14 lt n add Digit (1) Vtn.addDigit (4) Mtn add Digit (7) Mtn printNumber lt Number b (4) 13 lt b.add Digit (1) ltb.ad git (3) ltb.printNumber lt Number n (2) Vtn. add git (1) lt n add Digit (4) Mtn addDigit (7); Number b (4); ltb add Digit (1); lt b.add Digit (3) Atcout (n b) endli Vtcout (b n) endl; ltb add Digit (5) lt cout (b n) endl Number a (3) a. add Digit (1) a. add Digit (5) a. add Digit (0) Number b (3) ltb add Digit (1) lt b.add Digit (5); lt b .add Digit (1) t cout (b>a) endl;

Explanation / Answer

#include<iostream>
using namespace std;

class Number {
private:
int *arr;
int index;
int size;
public:
Number(int s) {
arr = new int[s];
// Fill in the rest of this constructor as needed.
index = 0; //set index to 0 for each object
size = s; //set size of the array as passed argument.
}
void addDigit(int dig) {
// ex. arr currently = [1, 2], addDigit(5) is called, arr should = [1, 2, 5]

if(index < size) //if index is less than size of the array then we can add the element
{
arr[index] = dig;
index++;
}
else // if index goes beyond size we are skipping element
return;
}
void printNumber() {
// print the number such as 124 etc.
for(int i = 0; i < index; i++)
cout<<arr[i];

cout<<endl;
}
//for dynamically created array we have to write subroutine to compare array element.The relational operator is
//available for array class in C++. Here we are comparing first object with passed object and returning result depending on that
bool operator>(const Number& n) {
// Your Logic here
if(this->index > n.index)
return 1;

for(int i = 0; i < n.index; i++)
{
if(arr[i] < n.arr[i])
return 0;
}
return 1;
}
};

int main()
{
/*Number n(2);
n.addDigit(1);
n.addDigit(4);
n.addDigit(7);
n.printNumber();
*/

/* Number b(4);
b.addDigit(1);
b.addDigit(3);
b.printNumber();
*/

Number n(2);
n.addDigit(1);
n.addDigit(4);
n.addDigit(7);
//n.printNumber();


Number b(4);
b.addDigit(1);
b.addDigit(3);
//b.printNumber();
cout<<(n>b)<<endl;
cout<<(b>n)<<endl;

b.addDigit(5);
//b.printNumber();
cout<<(b>n)<<endl;

/*
Number a(3);
a.addDigit(1);
a.addDigit(5);
a.addDigit(0);
//n.printNumber();


Number b(3);
b.addDigit(1);
b.addDigit(5);
b.addDigit(1);
//b.printNumber();
cout<<(b>a)<<endl;
*/


return 0;
}

please refer below ouput

1
0
1

Process returned 0 (0x0) execution time : 0.030 s
Press any key to continue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote