Need feedback on code

This is a discussion on Need feedback on code within the C++ Programming forums, part of the General Programming Boards category; Hi there, just made this little application(but borrowed some code from another classmate) which prints the following: Code: 5*5*5*5*5 4*4*4*4 ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    Need feedback on code

    Hi there, just made this little application(but borrowed some code from another classmate) which prints the following:

    Code:
    5*5*5*5*5
    4*4*4*4
    3*3*3
    2*2
    1
    Here is the code:

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	int iNum1 = 6;
    	int iNum2 = 5;
    	char cStar = '*';
    	
    	do
    	{
            for (int x=1;x<iNum2;x++)
            {
            	cout << iNum2 << cStar;
            }
            cout << iNum2;
            iNum2--; iNum1--;
            cout << endl;
    	} while(iNum1>1);
    	
        cin.get();
        return 0;
    }
    Im just wondering how i could possibly optimize this code, and also would like your thoughts about the code, whatever it may be, some hints or pointers about anything of this code which i should take into consideration in fututre, thanks.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Seriously consider using better indentation. It really does help others view your code and get a real feel for what it does at a glance.

    As far as optimizing it.... not sure you really could worry about optimiziation at this level. With that said, I don't think you need two different ints for this. Consider something like this:

    Code:
    #include <iostream>
    
    int main()
    {
    	int i, j, num = 5;
    	char star = '*';
    	
    	for(i=num;i>0;i--)
    	{
    		std::cout << i;
    		for(j=0;j<i-1;j++)
    		{
    			std::cout << star << i;
    		}
    		std::cout << std::endl;
    	}
    	
    	return 0;
    }
    Very similar, so there's not that much room to really improve it.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Yeah, there is a minimum amount of work that needs to be done to solve any problem, and it looks like you have done it. Any optimisation would just be a side-effect of refactoring, methinks.

    I would write something like:
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
        for (int i = 5; i > 0; --i)
        {
            for (int j = 1; j < i; ++j)
            {
                cout << i << '*';
            }
            cout << i << endl;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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