Minimizing my code

This is a discussion on Minimizing my code within the C++ Programming forums, part of the General Programming Boards category; I'm crazy about minimizing the size of my programs and using the most minimial amount of code! I've finished a ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    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;
    	
    	dectobasemenu(a, 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;
    			dectobasemenu(a, decimal);
    			}
    		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.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102
    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.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    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. #5
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    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.
    I am Error. When all else fails, use fire.

    My Current Screenshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21