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

DATA STRUCTURES & OTHER OBJECTS Using C++ Please provide header file[.h] , drive

ID: 3595921 • Letter: D

Question

DATA STRUCTURES & OTHER OBJECTS Using C++

Please provide header file[.h] , driver file,[.cpp] and main file [.cpp]

Please provide your code and screenshot of how your code ran.

An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this project, you might find it easier to store the digits backward, that is, place 4 in a[0] , place 3 in a[1] , place 2 in a[2] , and place 1 in a[3] . Design, implement, and test a class in which each object is a large integer with each digit stored in a separate element of an array. You’ll also need a private member variable to keep track of the sign of the integer (perhaps a boolean variable). The number of digits may grow as the program runs, so the class must use a dynamic array. Discuss and implement the +operator for this class.

Please provide header file[.h] , driver file,[.cpp] and main file [.cpp]

Please provide your code and screenshot of how your code ran.

Explanation / Answer

//File Name: StoreDigiInArray.h

#include<iostream>
using namespace std;
//Class Number definition
class Number
{
//To store sign
char sign;
//To store number of digits
int counter;
//Integer pointer to store the number
int *arr;
//To store number entered by the user
string number;
public:
//Prototype of member functions
Number();
void setNumber(string);
string getNumber();
void setCounter(int);
int getCounter();
char getSign();
void setSign(string);
void acceptNumber();
void displayNumber();
void storeNumber();
};//End of class

-----------------------------------------------------------------------

//File Name: StoreDigiInArray.cpp

#include"StoreDigiInArray.h"
#include<cstddef>
#include<stdlib.h>
using namespace std;
//Default constructor
Number::Number()
{
//Default sign
sign = '+';
counter = 0;
//Dynamically allocate memory to integer array
arr = (int *)malloc(sizeof(int));
number = "";
}//End of constructor

//Function to return the sign of the number
char Number::getSign()
{
return sign;
}//End of function

//Function to set the sign
void Number::setSign(string s)
{
//Checks if the first character is '+' or '-'
if(s[0] == '+' || s[0] == '-')
//Set the sign to s[0] position value
sign = s[0];
//Otherwise
else
//Set the default sign
sign = '+';
}//End of function

//Function to convert the entered string number to integer
void Number::setNumber(string s)
{
int x;
//Loops till end of the string
for(x = 0; x < s.length(); x++)
{
//Checks if the character is '+' symbol
if(s[x] == '+')
//Store the ASCII value of the '+' symbol
arr[x] = 43;
//Checks if the character is '-' symbol
else if(s[x] == '-')
//Store the ASCII value of the '+-' symbol
arr[x] = 45;
//Otherwise convert the character form of digit to integer form
else
arr[x] = s[x] - '0';
}//End of for loop
//Calls the method to set number of digits
setCounter(x);
}//End of function

//Function to return number
string Number::getNumber()
{
return number;
}//End of function

//Function to set number of digits
void Number::setCounter(int n)
{
counter = n;
}//End of function

//Function to return the number of digits
int Number::getCounter()
{
//Checks if the first character is '+' or '-'
if(number[0] == '+' || number[0] == '-')
//Returns counter minus one because of sign
return counter-1;
//Otherwise return counter
else
return counter;
}//End of function

//Function to accept a number
void Number::acceptNumber()
{
//Accept a number
cout<<" Enter a number: ";
cin>>number;
//Calls the function to convert the string number to integer number and store it in integer pointer
setNumber(number);
//Calls the function to set the sign
setSign(number);
}//End of function

//Function to display the number stored in integer pointer
void Number::displayNumber()
{
//Loops till number of digits
for(int x = 0; x < counter; x++)
{
//Checks if the first number is 43 ASCII value for '+' symbol
if(arr[x] == 43)
//Display in character form
cout<<(char)arr[x];
//Checks if the first number is 45 ASCII value for '-' symbol
else if(arr[x] == 45)
//Display in character form
cout<<(char)arr[x];
//Otherwise display the number
else
cout<<arr[x];
}//End of for loop
}//End of function

-------------------------------------------------------------------------------------------------------------------

//File Name: StoreDigiInArrayMain.cpp

#include"StoreDigiInArray.cpp"
#include<iostream>
#include<string>
using namespace std;
//main function definition
int main()
{
//Number class object created
Number n;
//Calls the method to accept a number
n.acceptNumber();
//Displays the number
cout<<" Entered number: ";
n.displayNumber();
//Displays number of digits
cout<<" Number of digits: "<<n.getCounter();
//Displays sign
cout<<" Sign of the number: "<<n.getSign();
}//End of main function

Sample Run 1:

Enter a number: 123654512

Entered number: 123654512
Number of digits: 9
Sign of the number: +

Sample Run 2:

Enter a number: -123456789

Entered number: -123456789
Number of digits: 9
Sign of the number: -

Sample Run 3:

Enter a number: -111222333444555666

Entered number: -111222333444555666
Number of digits: 18
Sign of the number: -

Sample Run 4:

Enter a number: +999888777666555444333222111

Entered number: +999888777666555444333222111
Number of digits: 27
Sign of the number: +