Not Producing output?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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= new int[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;
}
Last edited on
closed account (S6k9GNh0)
http://liveworkspace.org/code/Rzjxw$1

This doesn't seem to compile friend.
It compiles in VC++ 2008
The first for-loop at line 13 terminates when placeValue<decimal is false.

The second loop at line 22 will never execute because it has the same while-expression.

Jim
Never mind, got it. Thanks jim80y!
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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= new int[i];
	placeValue= new int[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.
Last edited on
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= new int[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.
Last edited on
Ok I corrected double pow (i did this because i was getting an error, but I just had to initialize the base as a double)

How do I initialize an array that I don't know the dimension size of yet? I thought that was what I was doing with placeValue= new int[p].

The for loop doesn't make sense in the end because I'm stuck before that part and I am constantly trying to debug before I continue.

Also i'm using global variables because right under this function goes int toOctal(){..} function and I want to reuse the variables.
Last edited on
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= new int[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.
Last edited on
I see.

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;
Last edited on
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.
Last edited on
Sounds great. How do I make it so placevalue[x] is always tested.

would this work, if i placed this new loop, before the other loop?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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= new int[i];
	placeValue= new int[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.

I will remove the globals. Thanks for the tips.
Last edited on
This line int * placeValue = new int[p]; allocates an array containing p elements.

Therefore I would recommend something like this:
1
2
3
4
    for (int j=0; j<p; j++)
    {
        placeValue[j] = pow(base,j);
    }

This way, you are guaranteed to assign a value to every element, no more and no less.
Thanks
Last edited on
Topic archived. No new replies allowed.