Returning 2D Array

This is a discussion on Returning 2D Array within the C++ Programming forums, part of the General Programming Boards category; Was having trouble finding out how to properly return a 2D array in a function. I have seen 1D 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
    22,181
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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, 03:07 AM
  3. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 01:28 PM

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