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
    23,617
    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;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 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