In C++ using Dynamic Memory Please. Thanks! An array can be used to store large
ID: 3793207 • Letter: I
Question
In C++ using Dynamic Memory Please. Thanks!
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], please 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 seperate 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 other appropriate operators for this class.
Explanation / Answer
// must attach below header file in stdafx.h file
#include <stdio.h>
#include <tchar.h>
#include<iostream>
#include<conio.h>
//-------------------Below is code .cpp file-------------------
#include "stdafx.h"
using namespace std;
class Array
{
public :
int size; // size for dyanamic array
int Number; // Integer no entered by user
int Temp;
int *array;// Created array and it will assign value at run time and size also decide according to the number of digits in Number
//----Function for implemanting dyanamic array and store entered no in reverse order
void StoreNumber_in_array()
{
cout<<"Enter a Integer Number";
cin>>Number; // Enter Integer no from user
Temp= Number; // Taking Number is Temp variable for use in second while loop becouse in first loop Number become zero
int count=0;
//below loop is use for counting digits so that we create dyanamic array, count gives us size
while(Number>0)
{
Number= Number/10;
count++;
}
size= count;// this give us size of dyanamic array
array= new int[size];
int i=0;// Index for array starting from 0
while(Temp>0)
{
array[i]= Temp%10; //Number%10 gives last digit of number and it store in array[0] position
Temp= Temp/10; // Number/10 remove last digit and update Number with one less integer
i++; // iteration for loop and changes index for array
}
cout<<"Entered Array Elements are:";
for(int j=0;j<size;j++)
{
cout<<"Enter element at postion "<<" "<<j<<"is"<<" "<<array[j]<<endl;
}
}
};
void main()
{
Array obj;// object of class Array
obj.StoreNumber_in_array();// calling function for store digit of number in reverse order in dyanamic array
getch();
}