Thread: Returning 2D Array

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Returning 2D Array

    Was having trouble finding out how to properly return a 2D array in a function. I have seen 1D array, but no more as of yet. If somebody has an example I could use one along with how the call would look for it. If pointer is involved too thats okay.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, you would return a pointer to the first element of the 2D array (i.e., a pointer to a 1D array). However, are you sure you need to return such a pointer at all? The modifications made to the elements of the array in the function would already be reflected in the caller since it is on the same array as in the caller.
    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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    well I had this and I was trying to break it down into a:
    Code:
    void getData(){}
    int findLow(){}
    double calcAvg(){}
    void Display(){}
    
    int main()
    {Vars
    
    getData();
    low=findLow();
    avg=calcAvg;
    Display();
    }
    in order to put the Averages & Lowests into an array rather than calculate, displaying & then losing them after & the issue is returning an array to the function & also not sure yet how to call them back into main.

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    void getData(int score[][4], int rows, int &low, double &avg)
    {
    	for(int R=0; R < rows; R++)
    	{
    		int TOT=0;
    		low = 100;
    
    		for(int C=0; C < 4; C++)
    		{
    			do
    			{
    				cout << "Enter test score " << (C+1) << " for student " << (R+1) << ": ";
    				cin >> score[R][C];
    			}while(score[R][C] < 0 || score[R][C] > 100);
    
    			TOT += score[R][C];
    
    			if (score[R][C] < low)
    				low = score[R][C];
    		}
    
    		avg = (TOT - low) / 3.0;
    
    		cout << fixed << setprecision(2);
    		cout << endl;
    		cout << "Dropped Grade: " << low << endl;
    		cout << "Average: " << avg << endl;
    	}
    }
    
    int main()
    {
    	int sc[3][4], low;
    	double avg;
    
    	getData(sc, 3, low, avg);
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    11
    I also realize you can not return an array but return the pointer just not sure how to go about it here.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could give parameters to the functions, so they accept among other things the 2d array as your original code does:

    Code:
    void getData(int array[][4], int rows);
    int findLow(int array[][4], int rows);
    double calcAvg(int array[][4], int rows);
    void Display(int array[][4], int rows);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM