I'm trying to convert dec to hex and I used dynamic memory to define the dimension count for the quotient array. However, by default the program should run through code and print test information (only cout there) but it produces nothing. ???
This is my first time attempting to declare an array in this way
// Debug
int placeValue(0); // Declare place value
int decimal(0); // Declare decimal
int base(0); // Declare base
int remainder(0); // Declare remainder
int * quotient; // Declare quotient as pointer, for dynamic array initalization
int toHexadecimal(int decimal) // to hexadecimal conversion function
{
double base(16.0); // initialize base to 16
int i; // Declare dynamic array counter
for(int j=0; placeValue<decimal;j++)
{
placeValue = double(pow(base,j)); // set place value for calculations (i.e. 16^0 || 16^1 etc...
i=j;
}
quotient= newint[i];
// Determine if decimal is greater than place value
for(int x=0;placeValue<decimal;x++)
{
placeValue=double(pow(base,x)); // set place value for calculations (i.e. 16^0 || 16^1 etc... until decimal is less than place value
placeValue=decimal?quotient[x]=1, remainder=0:quotient[x]=0, remainder=decimal;
cout << "Place value: " << placeValue << " Remainder: " << remainder << " Quotient: " << quotient[x] << endl; // // NOT DISPLAYING // // /////////////
}
return 0;
}
int main()
{
int decimal(132);
toHexadecimal(decimal);
return 0;
}
Can somebody explain to me why the for loop is generating 1 element over the amount I'm trying to get?
the loop is suppose to only generate a new element to placeValue for every time the decimal is smaller then the place value, but it's generating one extra every time (4096). I just need to see once why this happens and how to resolve it.
int * placeValue; // Declare place value
int decimal(0); // Declare decimal
int base(0); // Declare base
int remainder(0); // Declare remainder
int * quotient; // Declare quotient as pointer, for dynamic array initalization
int toHexadecimal(int decimal) // to hexadecimal conversion function
{
double base(16.0); // initialize base to 16
int i; // Declare dynamic array counter for quotient
int p; // Decalre dynamic array counter for placeValue
int placeTemp=0; // temporary, used to pow base and j and save value
for(int j=0; placeTemp<decimal;j++)
{
placeTemp=double(pow(base,j));
i=j;
p=j;
}
quotient= newint[i];
placeValue= newint[p];
// Determine if decimal is greater than place value
for(int x=0;placeValue[x]<decimal;x++)
{
placeValue[x]=double(pow(base,x)); // set place value for calculations (i.e. 16^0 || 16^1 etc...) until decimal is less than place value
if (decimal==placeValue[x]){
quotient[x]=1;
remainder=0;
}else{
remainder=decimal;
if (placeValue[x]<decimal){
}
}
cout << "Place value: " << placeValue[x] << " Remainder: " << remainder << " Quotient: " << quotient[x] << endl;
}
return 0;
}
int main()
{
decimal(132);
toHexadecimal(decimal);
return 0;
}
then also, i need to access the array backwards? Can I get a hint on how to do this as well?
Also it would be ironic if C++ has a function that will automatically just convert decimal to hexadecimal and octal automagically. If it does, I still want to create the conversion myself to practice, but am curious to know if it does?
EDIT* Yupp it does.
The code uses global variables unnecessarily. I'd strongly recommend to not use any global variables at all unless there is a reasonably good justification for doing so, rather than as a matter of routine.
The operator new [] is used without corresponding delete []. That is an error which leads to memory leaks.
Some variables are used without being initialised. In particular, in the loop:
22 23 24 25 26
placeValue= newint[p];
// Determine if decimal is greater than place value
for(int x=0;placeValue[x]<decimal;x++)
{
At line 25, placeValue[x] is not initialised, it doesn't make much sense, particularly in the end condition of the for loop.
Also, the pow() function returns a double. There is no benefit in casting it to a double before assigning it to an int.
As for pow() function, there may be some idiosyncrasy of the compiler you are using. Normally the compiler deduces which version of the overloaded function to call, based upon the types of its parameters. Sometimes it is necessary to cast the parameter, like this: pow(double(3),4);
I assume you are using #include <cmath>
This code allocates memory. placeValue= newint[p]
But the contents of that memory could be anything at all. Somewhere between allocating the array on line 22 and testing the value at line 25, there should be some code which stores some specific value in the element being tested.
Although you may subsequently store data in the array, the test at line 25 is always done on an element which has yet to be assigned any particular value.
Here is code, it is still producing one too many placeValues though?
1 2 3 4 5 6 7 8
placeValue[0]=pow(base,0); // set place value for calculations (i.e. 16^0 || 16^1 etc...) until decimal is less than place value
// Determine if decimal is greater than place value
for(int x=0;placeValue[x]<decimal;x++)
{
placeValue[x]=pow(base,x); // set place value for calculations (i.e. 16^0 || 16^1 etc...) until decimal is less than place value
cout << "Place value: " << placeValue[x] << " Remainder: " << remainder << " Quotient: " << quotient[x] << endl;
}
return 0;
This line placeValue[0]=pow(base,0); means the first pass through the loop will be safe. But what about when x==1 or more? The rest of the array is still not initialised.
On the topic of global variables, there is no good reason to declare int decimal globally and then to pass it as a parameter from main to toHexadecimal.
As for the other variables being re-used, I can only speak from experience when I say it sounds like a recipe for unpredictable behaviour, which will take longer to debug.
For example if toHexadecimal() is called first, it may leave certain values behind, which then affect the behaviour of toOctal(). The effect may appear to be beneficial. But what happens if function toOctal() is called first? The values it has come to depend on are no longer there. It would be better if there is a need to share information between functions to pass it as a parameter. But in this case I'd say there is nothing worth sharing, and it would be more beneficial to keep everything self-contained within the function, and avoid such dependencies and side-effects.
int toHexadecimal(int decimal) // to hexadecimal conversion function
{
int * placeValue; // Declare place value
int * quotient; // Declare quotient as pointer, for dynamic array initalization
int remainder(0); // Declare remainder
double base(16.0); // initialize base to 16
int placeTemp=0; // temporary, used to pow base and j and save value
int i=0; // Declare dynamic array counter for quotient
int p=0; // Decalre dynamic array counter for placeValue
quotient= newint[i];
placeValue= newint[p];
for(int j=0; placeTemp<decimal;j++)
{
placeTemp=pow(base, j);
placeValue[j]=pow(base,j);
i=j;
p=j;
}
// Determine if decimal is greater than place value
for(int x=0;placeValue[x]<decimal;x++)
{
placeValue[x]=pow(base,x); // set place value for calculations (i.e. 16^0 || 16^1 etc...) until decimal is less than place value
cout << "Place value: " << placeValue[x] << " Remainder: " << remainder << " Quotient: " << quotient[x] << endl;
}
return 0;
}
EDIT* This actually gets the placeValue up to 16, not 256 like I'm looking for. Now it's one short.