When data is large like Geoid Height Data (721*1440 elements), you cannot embed it into a header file. If you do and try to build in Visual Studio, the build might hang. You have to embed data into a cpp file and expose it in header via extern keyword.
To use two dimensional (2D) data in a function you can use the following example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Send one and two dimensional arrays to a function. | |
#include<stdio.h> | |
//Function to find max in 1D or 2D arrays | |
double findMax(const double *vals, const size_t nRow, const size_t nCol) { | |
double maxVal = -1; | |
for (size_t iRow = 0; iRow < nRow; iRow++) { | |
for (size_t iCol = 0; iCol < nCol; iCol++) { | |
double currentVal = *(vals + iRow * nCol + iCol); | |
if (currentVal > maxVal) { | |
maxVal = currentVal; | |
} | |
} | |
} | |
return maxVal; | |
} | |
int main() { | |
const double vals1D[] = { 1, 5, 7, 9, 3 }; | |
printf("maxVal1D = %1.1f\n", findMax(vals1D, 5, 1)); | |
const double vals2D[][2] = { {1, 3}, {9, 3}, {11, 4} }; | |
printf("maxVal2D = %1.1f\n", findMax(*vals2D, 3, 2)); | |
printf("Press enter...\n"); | |
getchar(); | |
} |
No comments:
Post a Comment