Command line arguments and matrices

Use the Matrix class to hold the weather data.
Do not include the year - only the monthly averages.
Use command line arguments: name of file, starting year


I'm a bit wet behind the ears about command line arguments, even about why would one want to store data in argc & argv, its purpose beyond that, and it's usefulness. Nevertheless, I must use it to open a file and hold the starting year for the data. Being inexperienced with command line arguments, I not sure how to even access that info and store it into the matrix class.

Can anyone explain to me the things I don't understand. Just incase my code is needed this is how far I got before these questions plagued my mind.

.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include"WeatherMatrix.h"

matrix::matrix()
{
	//assigning
	numCol = 12;
	numRow = 144;
	Matrix = new double*[144];

	for (int row = 0; row < numRow; row++)
	{
		Matrix[row] = new double[numCol];
	}

}

matrix::~matrix()
{
	for (int i = 0; i < numRow; i++)
	{
		delete[]Matrix[i];
	}
	delete[]Matrix[];
}


.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class matrix
{
	int numCol;
	int numRow;
	double ** Matrix;

public:
	matrix();
	~matrix();
	double avgRow(int)const; //Average temperature for a given year
	double hghstRow(int)const; //Highest temperature for a given year
	double avgCol(int)const; //Average temperature for a given month
	double hghstCol(int)const; //Highest temperature for a given month
	double getTemp(int, int)const;

	static const int rowMaxNumOf = 2;
	static const int colMaxNumOf = 2;
};
argc and argv are simply a way for passing information from the command line to your program.

argc contains the number of arguments. argv is an array of pointers to C-strings containing those arguments. argv[0] is always the filename of your program. argv[1] (if present) contains the first actual argument.

1
2
3
4
5
6
7
8
9
10
11
12
int main (int argc, const char *argv[])
{  const char * prog = argv[0];
    const char * filename;
    int year;

    if (argc < 3)
    {  cout << "filename and year required" << endl;
        return 1;
    }
    filename = argv[1];
    year = atoi (argv[2]);
...


Topic archived. No new replies allowed.