Two dimension arrays

Hi guys, I am trying to figure out how to set up two dimension arrays. I might not be using the right terms but i have 7 columns and a few hundred rows. What I am trying to do is compare the columns and two rows at a time. So comparing row 1 column 1 with row 2 column 1, then would cout if it doesnt fit into the argument. Then move to compare row 2 column 1 with row 3 column 1 and so on... How can I do so? Below is the example of data i will be using. Thanks.


19970929,162.5800,170.3200,157.4200,169.8100,31427893,125.0600
19970930,171.8700,172.9000,170.8400,172.9000,901641,127.3400
19971001,172.9000,177.5500,172.3900,177.0300,1101598,130.3900
yoitsmejy wrote:
then would cout if it doesnt fit into the argument.
Could you explain what this means? What do you mean by "fit into the argument"?
Last edited on
Sorry. If the argument is false.
Ah. Well, you can loop through each column, and in that loop loop for #rows-1 times, then just compare current row,col to row+1,col
LB, I've been trying to figure it out but was not able to get it to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
ifstream indata("Test.csv");
	

	while (!indata.eof())		// keep reading until end-of-file
	{  
  
		float colA,  colB,  colC,  colD,  colE,  colF,  colG;	
		float pcolA, pcolB;
		
		pcolA=colA+1;

		//Get data
		indata >> colA >> colB >> colC >> colD >> colE >> colF >> colG;

		ofstream gaps("Rec.txt");
		//Do your comparisons
		if( abs(colA - pcolA) > 5 && pcolA > 0)
			gaps << "Gap in days" << colA << endl;
		gaps.close();
		//Set your references for next time

	}
	indata.close();
The programming problems are:
1) while (!indata.eof())
2) pcolA is recreated for every line and given an indeterminate value
3) indata >> colA >> colB >> colC >> colD >> colE >> colF >> colG; requires whitespace separators by default, but you have comma separators

fixing those gives

1
2
3
4
5
6
7
8
9
10
11
12
    ifstream indata("Test.csv");
    unsigned long pcolA = 0;
    string line;
    while (getline(indata, line))
    {
        unsigned long colA;
        istringstream(line) >> colA;
        ofstream gaps("Rec.txt");
        if( abs(colA - pcolA) > 5 && pcolA > 0)
            gaps << "Gap in days at " << colA << '\n';
        pcolA = colA;
    }


But there is an error in the algorithm as well: subtracting *dates* in YYYYMMDD format does not exactly give the difference in *days*: 19971001 - 19970930 == 71, not 1. Consider parsing dates as actual dates, using a suitable library (try boost if you can, or if your compiler is sufficiently modern to have C++11's std::get_time in <iomanip>, use that)
Last edited on
the code works great. I tried adding another if argument and when it runs, rec.txt file is empty.

Below is the code that i added,
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
int main () 
{
  ifstream indata("Test.txt");
    unsigned long pcolA = 0;
	unsigned long pcolB = 0;
	unsigned long pcolC = 0;
	unsigned long pcolD = 0;
	unsigned long pcolE = 0;
	unsigned long pcolF = 0;
	unsigned long pcolG = 0;

    string line;

    while (getline(indata, line))
    {
		unsigned long colA;
		unsigned long colB;
		unsigned long colC;
		unsigned long colD;
		unsigned long colE;
		unsigned long colF;
		unsigned long colG;
		istringstream(line) >> colA >> colB >> colC >> colD >> colE >> colF >> colG;

        ofstream rec("Rec.txt");
		string gaps;
		string change;
        if((colA - pcolA) > 5 && pcolA > 0)
            cout << gaps << "Gap in days at " << colA << endl;
		if(colB > (1.1 * pcolB) > 0)
			cout << change << "More than 10% change at" << colA << endl;

		cout << rec << gaps << '\n' << change << endl;

        pcolA = colA;
		pcolB = colB;
		pcolC = colC;
		pcolD = colD;
		pcolE = colE;
		pcolF = colF;
		pcolG = colG;
		}
	indata.close();
  return 0;
}


How can i fix the error?
Topic archived. No new replies allowed.