Thread: My Programming Technique

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    My Programming Technique

    I am curious as to what others think of how I went about this:

    I defined a multi dimensional array, and then outputted it in the middle of a program. Instead of outputting it VIA a string, I chose to output it using programming loops, and it outputs exactly as it was declared, without the type and name.

    Feedback on my form if you don't mind:

    Code:
    	//multidimensional array
    	int y[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    	
    	//output the array
    	cout << "The array contains: ";
    	for(int s = 0; s < 3; s++)
    	{
    		for(int k = 0, z = 0; k < 3; k++, z++) 
    		{
    			if(s == 2 && k == 2) cout << y[2][2] << "}}";
    			else if(s == 0 && k == 0) cout << "{{" << y[0][0] << ",";
    			else 
    			{
    				if(k == 0) cout << "{";
    				cout << y[s][k] << ",";
    				if(k == 2) cout << "\b},";
    			}
    		}
    	}

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would have done it in this way:
    Code:
    //output the array
    cout << "The array contains: {";
    for (int i = 0; i < 3; ++i)
    {
        if (i != 0)
        {
            cout << ',';
        }
    
        cout << '{';
        for (int j = 0; j < 3; ++j)
        {
            if (j != 0)
            {
                cout << ',';
            }
    
            cout << y[i][j];
        }
        cout << '}';
    }
    cout << "}\n";
    EDIT:
    Actually, because trailing commas in array aggregates are superfluous, if you so wish you could have simplified to:
    Code:
    //output the array
    cout << "The array contains: {";
    for (int i = 0; i < 3; ++i)
    {
        cout << '{';
        for (int j = 0; j < 3; ++j)
        {
            cout << y[i][j] << ',';
        }
        cout << "},";
    }
    cout << "}\n";
    Last edited by laserlight; 01-15-2011 at 11:13 PM.
    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. a more advanced technique
    By Yarin in forum C++ Programming
    Replies: 53
    Last Post: 08-07-2008, 04:57 AM
  2. continuous key pressing technique
    By hdragon in forum Game Programming
    Replies: 3
    Last Post: 06-09-2006, 09:25 PM
  3. Help with with Lisp Reader Technique
    By tuxinator in forum C Programming
    Replies: 15
    Last Post: 05-07-2006, 05:30 PM
  4. Lisp Reader Technique
    By tuxinator in forum Game Programming
    Replies: 2
    Last Post: 05-04-2006, 12:34 AM
  5. Learning Merge Sort Technique
    By JoshR in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:48 PM