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.
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.
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
well I had this and I was trying to break it down into a:
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:void getData(){} int findLow(){} double calcAvg(){} void Display(){} int main() {Vars getData(); low=findLow(); avg=calcAvg; Display(); }
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; }
I also realize you can not return an array but return the pointer just not sure how to go about it here.
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.