Thread: Array output

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

    Array output

    I have a program where the user enters some numbers, and those numbers are outputted again, along with (input number) number of # signs on each line. I have the output, but I need some help for the rest.
    For example, input 1 2 3 should be this:
    Code:
    Number of values to graph: 3
    1 2 3
    
     1 | #
     2 | ##
     3 | ###
    but instead it's:
    Code:
    Number of values to graph: 3
    1
    2
    3
    1
     2
     3
    How do I get from the second one to the top one?
    My code is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        int numbers[10];
        int howmany;
        cout << "Number of values to graph: ";
        cin >> howmany;
        for( int i = 0; i < howmany; i++ )
        {
            cin >> numbers[i];
        }
        if( howmany <= 0 )
            cout << "No numbers entered." << endl;
        else
        {
            cout << numbers[0];
            for( int x = 1; x < howmany; x++ )
                cout << setw(2) << endl << numbers[x];
            cout << endl;
        }
    }

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    How do I get from the second one to the top one?
    My code is:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main( ) {
        int numbers[10];
        int howmany;
        cout << "Number of values to graph: ";
        cin >> howmany;
        for( int i = 0; i < howmany; i++ )
        {
            cin >> numbers[i];
        }
        if( howmany <= 0 )
            cout << "No numbers entered." << endl;
        else
        {
            cout << numbers[0];
            for( int x = 1; x < howmany; x++ )
                cout << setw(2) << endl << numbers[x];
            cout << endl;
        }
    }
    Use std::vector instead of that array.
    Don't use "howmany"; instead, get the numbers from the user until they don't give any more.

    Your loop (which I have highlighted) starts at 1, it should start at 0. And the call before it should not be there. I have no idea why you did that.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    OK, fixed the loop. I have to use the array though.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I'd also recommend doing a quick check to assure that the howmany is also less than or equal to 10. You want a number between 1 and 10 but as of now, you just check if it's zero or negative.

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    No, it's actually a check for 40, not 10. I'll add that though.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Kelton2 View Post
    No, it's actually a check for 40, not 10. I'll add that though.
    Okay. I was jsut going by:
    Code:
        int numbers[10];
        int howmany;
        cout << "Number of values to graph: ";
        cin >> howmany;
        for( int i = 0; i < howmany; i++ )
        {
            cin >> numbers[i];
        }
    Regardless, it'd best if you rewrote your code like this :
    Code:
    const int size = 40;
    
    int numbers[size] = { 0 };
    int how_many = 0;
    
    std::cin >> how_many;
    
    if (how_many <= 0 || how_many > size)
    {
        /* handle error */
    }

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Kelton2 View Post
    OK, fixed the loop. I have to use the array though.
    No you don't.

    If you really need access to contigious data, std::vector manages an array underneath that you can access with &x[0].

    Otherwise, if your teacher tells you that you must use an array, he/she is a horrible teacher.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array and output.
    By JOZZY& Wakko in forum C Programming
    Replies: 6
    Last Post: 11-09-2009, 12:23 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 3
    Last Post: 02-19-2003, 08:34 PM
  4. 2D array output help
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2002, 10:19 PM
  5. array output
    By runtojesus in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2001, 11:30 AM