A couple of questions concerning arrays

This is a discussion on A couple of questions concerning arrays within the C++ Programming forums, part of the General Programming Boards category; First of all, thanks for all those who have helped me in the past and those who are about to. ...

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

    A couple of questions concerning arrays

    First of all, thanks for all those who have helped me in the past and those who are about to.

    I only have my prog written for the first part of my Q, the 2nd part, i need help setting up.

    1) How do i store the numbers in a 5 x 3 array called Data[5][3]. I can have the user input data in the following prog, but how do i specifically call it "Data [5][3]. The reason I need to know this is becuase:

    2) I need to have the program pass the array Data[ ][ ] from main ( ) to a function. My prof told me to use, float mean (const int Data [ ][ ], int, int ). I think it should look something like, mean (Data [ ][ ], 5, 3)

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

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    When your professor told you to call the function as func(int [][numcol], int, int), the second and third arguements aren't the array size. Those are already global constants. What he wants them to be is the specific index you want to access, I'd imagine. Say you want to access arry[2][2], you'd call
    Code:
    func(arry, 2, 2);
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Remember that data[4][2] is the 3rd element in the 5th array.
    Code:
    // Try to visualise the 2D array indexes something like this, 
    0: [0] [1] [2]
    1: [0] [1] [2] 
    2: [0] [1] [2]
    3: [0] [1] [2]
    4: [0] [1] [2]
    Also, remember that "data[]" is an array of pointers (to arrays). Hence, the function can only deduce how many arrays there are (more accurately, it checks the number of elements of data[]). So, it cannot deduce the size of each individual "sub" array (For lack of a better term) - so you must specify that explicitly.

    I believe you may have misunderstood his instructions, but the closest to what you described would be something like:
    Code:
    void output(int arr2D[][3], int a) // some number of arrays, as defined by 'a', of size 3
    {
        for (int i=0; i!=a; ++i)
        {
            for (int j=0; j!=3; ++j)
                cout << arr2D[i][j] << " ";
            cout << endl;
        }
    }
    Personally, I would use STL vectors to do this instead (since vectors keep track of dimensions, life is made much easier).. as you can see, this sort of method is messy.

    But if you've been told not to use containers, and have to use arrays, then a better way to do this would be to create a Matrix class, as a wrapper for your 2D array (in other words, make your own container).
    Last edited by Bench82; 03-22-2006 at 07:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 05:55 PM
  2. a couple of questions about System.String
    By Elkvis in forum C# Programming
    Replies: 5
    Last Post: 02-17-2009, 02:48 PM
  3. A couple of Basic questions
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 09-26-2007, 05:06 PM
  4. Couple of Questions
    By toonlover in forum Windows Programming
    Replies: 10
    Last Post: 07-14-2006, 02:04 AM
  5. Couple Of Questions I'm Absolutly Stuck On
    By chriscolden in forum C Programming
    Replies: 8
    Last Post: 06-06-2006, 11:08 AM

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