How to create a multi-dimensional user array?

This is a discussion on How to create a multi-dimensional user array? within the C++ Programming forums, part of the General Programming Boards category; I have a 3x4 array, and I need the user to input the data and fill it appropiately. I have ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    How to create a multi-dimensional user array?

    I have a 3x4 array, and I need the user to input the data and fill it appropiately. I have been working on this for days and I still can't figure it out. Thought it would have been very simple to google it but nothing helpful comes up.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    const int numrow = 3;
    const int numcol = 4;
    int grades [numrow][numcol];
    
    int main ()
    {
    
    	for (int i = 0; i <numrow; i++)
    {
    	for (int j = 0; j < numcol; j++)
    {
    	cout << "enter grades for row # " << (i + 1)<< ": ";
    	cin >> grades [i][j];
    }
    	cout << setw (4) << grades [i][j];
    }
    
    	return 0;
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    check out salem's wisdom

    dynamic allocation.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The only line that I see is wrong:
    Code:
    cout << setw (4) << grades [i][j];
    For one, j will be 4 each time this line gets called, which is out of bounds.

    Edit: Never mind the second part of my post
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    You asked this before
    Storing and printing matrices

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    how hard is it really?
    code for input:
    Code:
    for (int i = 0; i <numrow; i++)
    {
        for (int j = 0; j < numcol; j++)
        {
            // prompt for input and store in the proper place
        }
    }
    and code for output:
    Code:
    for (int i = 0; i <numrow; i++)
    {
        for (int j = 0; j < numcol; j++)
        {
            // output elements
        }
        // print newline here
    }
    and what was wrong with indigo0086's response when you posted this question a couple days ago?
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    > I have been working on this for days and I still can't figure it out.
    Perhaps if you indented your code properly, you'd see the position of your cout's make no sense.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    const int numrow = 3;
    const int numcol = 4;
    int grades [numrow][numcol];
    
    int main ()
    {
      for (int i = 0; i <numrow; i++)
      {
        for (int j = 0; j < numcol; j++)
        {
          // a cout for every value, not every row
          cout << "enter grades for row # " << (i + 1)<< ": ";
          cin >> grades [i][j];
        }
        // a cout after the j loop has finished
        // j is actually out of scope here
        // g++ reports
        // foo.cpp:21: error: name lookup of `j' changed for new ISO `for' scoping
        // foo.cpp:13: error:   using obsolete binding at `j'
        cout << setw (4) << grades [i][j];
      }
    
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 08:02 AM
  2. Help printing a multi array
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-05-2008, 02:35 PM
  3. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 06:29 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM
  5. Replies: 4
    Last Post: 11-07-2001, 02:46 PM

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