Thread: Can't get a specifically ordered list to print properly

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Can't get a specifically ordered list to print properly

    I'm writing a program where the user inputs a number of rows, and that many rows of @ symbols are printed, where the first row has the same number of columns as rows, and each next row decrements the number of columns by 1. It prints out a totally wrong output and whatever I try it won't print the right thing- currently when I input 4 it prints out 5 @ symbols on the first row, and then 3 on all the other rows. Here is the code:

    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int rows;
        cout << "How many rows? ";
        cin >> rows;
        int columns = rows;
        cout << endl;
        for( int r = 0; r < rows; r++ )
        {
             for( int c = 0; c < columns; c++) { 
                cout << "@";
                }
             for(int useless = 0; useless < columns; columns--) {
                     if(columns = rows - 1) {
                                useless = 1000;
                                }
                     cout << "@";
                     cout << endl;
                        }
            cout << endl;
        }
    }
    I hope someone can help!

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    In pseudo-code, then:
    Code:
    nRows = input()
    for currentRowNumber in nRows...1
        print currentRowNumber of '@', then '\n'
    or, if you don't want to go backwards:
    Code:
    nRows = input()
    for i from 0 until nRows exclusive:
        print (nRows - i) of '@', then '\n'
    What are you trying to accomplish with the "useless" variable?

    You can use an inner loop to print the right number of '@', or you can use something like std::string's fill constructor.

    I recommend you remove everything within your outermost loop and start from there.
    Last edited by CodeMonkey; 12-21-2014 at 08:17 PM. Reason: English
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Thank you for your help. I still don't understand this part of your psuedocode explanation:
    for i from 0 until nRows exclusive:
    Here is what I currently have (The comments of the content of the 3rd for loop are intentional):
    Code:
    #include <iostream>
    using namespace std;
    int main ( ) {
        int rows;
        cout << "How many rows? ";
        cin >> rows;
        int columns = rows;
        cout << endl;
        for( int r = 0; r < rows; r++ )
        {
             for( int c = 1; c < columns; c++) { 
                cout << "@";
                }
             for(int useless = 1; useless < columns; columns--) {
                 //    if(columns = rows - 1) {
                   //             useless = 1000;
                     //           }
                                
                 //   cout << "@";
                   // cout << endl;
                        }
             
             cout << "@";
             cout << endl;           
        }
    }
    Also, useless does nothing. I don't really remember why I put it in there in the first place, but I prefer commenting out unused code to removing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with search function within an ordered list
    By drew99 in forum C Programming
    Replies: 8
    Last Post: 09-02-2011, 01:03 PM
  2. insertion in an ordered list ?
    By jack_carver in forum C Programming
    Replies: 1
    Last Post: 03-21-2009, 11:11 AM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  4. problme with ordered linked list
    By palku in forum C Programming
    Replies: 5
    Last Post: 09-19-2005, 04:33 PM