Help filling new dynamic array of type pointer

Help I am making a program to add sparse matrices from two files into an output file. My problem is I cannot figure out how to grab the correct information from my two files and put them in my dynamic array. My attempt is not grabbing the right information.
Input 1
#Matrix A
2 2
1 1 1.12
2 2 2

Input 2
2 2
#Matrix B
#Aliens are real
1 1 3
2 2 4

output
2 2
1 1 4.12
2 2 6

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
#include <string>Input 2
#include <fstream>
#include <sstream>
using namespace std;

struct element        // i been instructed to use this specifically
{
	int r;
	int c;
	double value;
	
};
void rowCol(ifstream &file, int &r, int &c)
{
	string line = "";
	while (getline(file, line))
	{

		if (line[0] == '#' || line.length() == 0)
			continue;
		istringstream fn(line);
		fn >> r >> c;
		break;
	}
}
void rowcheck(int row1, int row2, int col1, int col2)
{
	if (!(row1 == row2 && col1 == col2))
	{
		cout << "Cannot compute size of sparse matrix " << endl;
		system("pause");
		exit(1);
	}
	if (row1 < 0 || row2 < 0 || col1 < 0 || col2 < 0)
	{
		cout << "Error size cannot be less than 0 " << endl;
		system("pause");
		exit(1);

	}


}
void inputcheck(ifstream &file1,ifstream &file2)
{
	if (!file1 && file2)
	{
		cout << "Error opening file 1! " << endl;
		system("pause");
		exit(1);

	}
	if (file1&&!file2)
		{
			cout << "Error opening file 2! " << endl;
			system("pause");
			exit(1);

		}
	if (!(file1 || file2))
	{
		cout << "Error opening both files! " << endl;
		system("pause");
		exit(1);
	}


}
int main()
{
	string f1, f2;

	cout << "Enter name of first file to be read in format name.txt" << endl;
	cin >> f1;
	cout << "Enter name of second file to be read in format name.txt" << endl;
	cin >> f2;

	ifstream file1,file2;
	file1.open(f1);
	file2.open(f2);
	inputcheck(file1, file2);


	int size1, size2;
	string line = ""; 
	int row1 ,col1,row2, col2;
	

	rowCol(file1, row1, col1);
	rowCol(file2, row2, col2);
	rowcheck(row1, row2, col1, col2);
	size1 = row1 * col1;
	size2 = row2 * col2;
	
	element *m1 = new element[size1];    // i been instructed to use this
	for (int x = 0; x < size1; x++)
	{
		
		file1>> m1[x].r >> m1[x].c >> m1[x].value;

	}
	for (int x = 0; x < size1; x++)
	{
		if (m1[x].r>0)
			cout << m1[x].r << " " << m1[x].c << " " << m1[x].value << endl;
	}




	cout << "___________" << endl;
	




	element *m2 = new element[size2];
	int count = 0; int a, b;
	for (int x = 0; x < size2; x++)
	{
		
		file2 >> m2[x].r >> m2[x].c >> m2[x].value;

	}
	for (int x = 0; x < size2; x++)
	{
		if (m2[x].r> 0)
			cout << m2[x].r << " " << m2[x].c << " " << m2[x].value << endl;
	}
		file1.close(); file2.close();
		cout << size1 << endl;
		cout << size2 << endl;










	
	delete[] m1;
	m1 = NULL;
	delete[] m2;
	m2 = NULL;


	system("pause");
	return 0;

}
Last edited on
output
2 2
1 1 4.12
2 2 6

Is that the expected output?

Your program obviously does not print anything like that. Could you show the actual output (which presumably should resemble data from the input files)?


Note, you have repetition. Compare lines 96-107 to lines 118-130. What if you had a function to read data from file into the array (and another to print an array)?


Now, lets think about the format of of an input file.

* There seems to be a line with two integers: rows and columns. (Hard to say which is which from the examples.) This line is before the data points.

* A data point is a line with two integers and one double. The integers are row and column index, and they are in [1..N] syntax rather than the C++'s [0..N[ style.

* Almost anywhere there may be comment lines that starts with a hashtag. (You ignore only the comments that are before the rows and colunms line; an error.)

* A full matrix has rows*columns elements. You try to read (and show) that many data points. You don't have that many data points in the input, because you have sparse matrices. That is an another error.
* output is correct, but I have not made it that far. I am trying to make sure my array of struct is filled correctly so I can make another function to add the two arrays.

* i do plan to make the repetition into a function to shorten my code in my main

*the first two numbers on one line are the size of the sparse matrix row by columns // how would i ignore this for the entry

* the next lines have three numbers: (entry row) (entry column) (value in entry)

* my current cout is just to show me what i am doing step by step // i delete it as i go

Last edited on
Note that you try to print size1 elements from m1.
There are no size1 elements in the input.

Yes, the m1 represents an array that does have size1 elements, but the size1 is the capacity of the array; how many data points it can hold at most.

How many of those elements are actually used for data points is a different number. The count data points. Lets call it 'size'. You don't keep track of the 'size'. You should determine it when reading a file and make use of it later.


Note: Both examples are "boring", for they both are 2x2 matrices -- 4 elements -- and both have only 2 data points, even at same elements -- (1,1) and (2,2) -- in each matrix. In C++ 2D array convention foo[0][0] and foo[1][1].


Lets say that input A (3 points) is
a . c
. e .

and input B (4 points) is
. . r
x y z

Elementwise sum matrix of them should then have value in 5 out of 6 elements:
a  . c+r
x e+y z

(Unless I have misunderstood the "sparseness".)
Last edited on
Topic archived. No new replies allowed.