How to call in a 2-d Array
Apr 23, 2013 at 12:17am UTC
How do I call these functions. Or have I set up the 2-d arrays totally wrong. The compiler is rejecting me in just about everything I have tried. Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
// global constant variables
const int YEARS = 8;
const int MONTHS = 12;
const int SPACER =5;
// function prototypes
// function to read in values
void getData(int [YEARS],double [][YEARS], int );
// function to display values in table format
void printData(int [YEARS], double [][YEARS], int );
// funtion to print data to scren in table format using arrays
int main()
{
int years [YEARS];
double rain [YEARS][MONTHS];
double thirdYearTotal;
double mayTotal;
int i; // for indexing 1-d array
int j; // for indexing 2-d array
getData();
printData();
return 0;
}
// function definitions
void getData(int yearArray[YEARS], double rainArray[][YEARS], int )
{
ifstream fin;
fin.open("rainfall.txt" );
if (!fin)
{
cout << "Error opening file, shutting down now.\n" ;
exit(EXIT_FAILURE);
}
else
{
for ( int i = 0; i < YEARS; i++)
{
fin >> yearArray[i];
for (int j = 0; j < MONTHS; j++)
{
fin >> rainArray[i][j];
}
}
}
fin.close();
}
void printData(int yearArray[YEARS], double rainArray[][YEARS], int )
{
for (int i = 0; i < YEARS; i++)
cout << yearArray[i] << SPACER;
for (int j = 0; j < MONTHS; j ++)
cout << rainArray[i][j] << SPACER << endl;
}
Apr 23, 2013 at 12:29am UTC
here is an example of a 2d array:
1 2 3 4 5
int twoDIntArray[5][8];
for (int i = 0; i<5; i++) for (int j = 0; j<8; j++){
twoDIntArray[i][j] = j+(i*8);
cout << "twoDIntArray[" << i << "][" << j << "]: " << twoDIntArray[i][j] << endl;
}
Topic archived. No new replies allowed.