# Thread: Minimizing my code

1. ## Minimizing my code

I'm crazy about minimizing the size of my programs and using the most minimial amount of code! I've finished a homework assignment, works perfectly, now I just want to minimize the code and make it smaller.

Code:
```#include <iostream.h>

void decimaltobase(int a[], double decimal, int whole, double fraction, int type);
void dectobasemenu(int a[], double decimal);

main()
{
int a[100];
double decimal;

return 0;
}

void decimaltobase(int a[], double decimal, int whole, double fraction, int type)
{
int legnth1 = 0, temp = 0;

//for the whole value
for (int i = 0; whole > 0; i++)
{
a[i] = whole % type;
whole = whole / type;
legnth1++;
}
//for (int i= 0; fracion > 0; i++)
cout << decimal << " represented in base-"<<type<<" is ";

for (int i = legnth1 -1; i >=0; i--)
{
if ((type > 10)&&( a[i]> 9))
{
char c = 'a';
for (int j = 10; j < a[i]; j++)
{
c++;
}

cout << c;
}
else
{
cout << a[i];
}

}
if (fraction > 0)
cout << ".";
for (int i = 0; fraction != 0; i++)
{
fraction = fraction * type;
temp = int(fraction);
if ((type > 10) && (temp > 9))
{
char c = 'a';
for (int i = 10; i < temp; i++)
{
c++;
}
cout << c;
}
else
{
cout << temp;
}
fraction = fraction - double(temp);
}
cout << endl << endl;
}

void dectobasemenu(int a[], double decimal)
{
int type = 1;
do
{
cout << "Enter the number system base (2--36) (press 0 to quit): ";
cin >> type;
if ((type < 2)&&(type != 0))
{
cout << "That is not a valid base!"<<endl;
}
if (type == 0)
break;
do
{
cout << "Enter a decimal integer to convert to base-" <<type<<": ";
cin >> decimal;
int whole = int(decimal);
double frac = decimal - double(whole);

if (decimal == 0)
break;
decimaltobase(a ,decimal,  whole, frac,  type);
}while(decimal != 0);
}while (type !=0);
}```
Yes... I may go ahead and remove the functions, I had split the code up in anticipation of converting any given base to decimal being added to it, but it is no longer the case.

Please suggest any ideas on how I can minimize the amount of code I could use or any other improvements, but PLEASE do not reply to the post with code... pseudeo code is ok, but I'd mainly just like suggestions.

2. I don't think you could do a whole lot with that being a relatively small project. Perhaps if there was a completely different algorithm you used might make it smaller. I personally think you should use more commenting. whenever you declare int a[100] you should palce a comment as to what that is and what it will be used for.

You could combine a[100] and decimal into a struct since it seems those 2 pieces of data always get passed together, then you'd be one less function paramter in the paramter list that contain them.

3. There's little point in trying to minimize something. It's more
important that the code is easy to read. Don't use type
has a variable name use base instead. I'm a little confused
on what your functions actually do and the purpose of them.
You should have one specific function convert to the given base and one function print it out. But you can replace stuft
like

Code:
```if ((type > 10)&&( a[i]> 9))
{
char c = 'a';
for (int j = 10; j < a[i]; j++)
{
c++;
}
cout << c;
}
else
{
cout << a[i];
}```
With something like this
Code:
```char alpha[16] = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F'};
cout << alpha[c[i]] << endl;```
And making the function undefined for bases over 16.

4. This could possibly negate the purpose of your whole assignment, but you could probably use _itoa to reduce part of your code.

char* _itoa(int value, char* buffer, int radix);
This function converts the integer value of base radix into a character string buffer.

You can then change from string back into an int with atoi.

int atoi(const char* buffer);
This function converts the character string buffer into an integer and returns the value.

I didn't read too thoroughly through your code, but I thought this was something in the spirit of it.

5. Well, the assignment itself must allow the user to enter a base all the way up to 36, so I figure it's simpler just to increment the character. it's not true conversion, but more of a unbiased conversion (example: -4 is represented in binary as -100)

of course.. you guys may be right.. there really isn't anything I could do to minimize it more.

Popular pages Recent additions