Trying to implement a Sparse Matrix w/ input from file

Hi all, for my CS project I have to implement a Sparse Matrix from an input from a file or from input from the console. I keep getting a few errors and don't know where I went wrong. Any help would be greatly appreciated, I am rather new to C++.

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
#include "SparseMatrix.h"
#include <cstdlib>   // for exit() call
#include <iomanip>   // to format output from overloaded outstream operator <<
#include <iostream>
using namespace std;



//+------------------------------------------------------------------+
//| constructs a matrix according to the data in the file "filename" |
//+------------------------------------------------------------------+
SparseMatrix::SparseMatrix( char *filename )
   {
   infile.open( filename );


   struct SparseMatrix{
	   unsigned int l, c;
	   double val;
   };

  
}


SparseMatrix* transpose(double A[][100], unsigned int numlines, unsigned int numcols, \
	char *err, unsigned int *numEl){

	struct SparseMatrix{ unsigned int l, c; double val; };

	*numEl = 0; // number of elements in Matrix that aren't null
	unsigned int i, j; // i = line index, j = column index
	SparseMatrix *pTuplu;
	for (i = 0; i<numlines; i++)
	for (j = 0; j<numcols; j++)
	if (A[i][j])
		(*numEl)++;
	double pondere = *numEl;
	pondere = pondere / (numlines*numcols);
	if (pondere >= 0.0015 && pondere <= 0.03){
		*err = 1;
		pTuplu = new SparseMatrix[*numEl];
		unsigned int k = 0;
		for (i = 0; i<numlines; i++)
		for (j = 0; j<numcols; j++)
		if (A[i][j]){
			pTuplu[k].l = i + 1;
			pTuplu[k].c = j + 1;
			pTuplu[k].val = A[i][j];
			k++;
		}
		return pTuplu;
	}
	else{
		*err = 0;
		return 0;
	}
}

void main(){
	double Matrix[25][100], val;
	unsigned int numlines, numcols, i, j;
	unsigned int numEl;
	char err;
	numlines = 25;
	numcols = 100;
	SparseMatrix *pMatRara = NULL;
	for (i = 0; i<numlines; i++)
	for (j = 0; j<numcols; j++)
		Matrix[i][j] = 0;

	char opt = 'y';
	while (opt == 'y'){
		do{
			cout << "Give the row index of the not null element: ";
			cin >> i;
		} while (i>25 || i == 0);
		do{
			cout << "Give the column index of the not null \
										   element: ";
			cin >> j;
		} while (j>100 || j == 0);
		cout << "Give the value of the not null element: ";
		cin >> val;
		if (!Matrix[i - 1][j - 1])
			Matrix[i - 1][j - 1] = val;
		else{
			cout << "Given position contains an element with not \
										   null value! Overwrite the element?(y/n): ";
			cin >> opt;
			if (opt == 'y')
				Matrix[i - 1][j - 1] = val;

		}
		cout << "Continue?(y/n): ";
		cin >> opt;
	}

	pMatRara = transpose(Matrix, numlines, numcols, &err, &numEl);

	if (err){
		cout << "Structure of the type Sparse Matrix is:" << endl;
		for (i = 0; i<numEl; i++)
			cout << pMatRara[i].l << "\t" << pMatRara[i].c << "\t" << \
			pMatRara[i].val << endl;
		delete[] pMatRara;
	}
	else
		cout << "Two-dimensional array does not meet the \
								   criteria of sparse matrix!" << endl;
}


   


//+--------------------------------------------------+
//| return the dot product of the corresponding rows |
//+--------------------------------------------------+
ostream& operator<<( ostream& os, const SparseMatrix &sm )
   {
   os << "rows = " << sm.rows << "   cols = " << sm.columns << "\n";

   // replace this comment with your code

   return os;
   }



The header file is here:
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
#ifndef SPARSE_MATRIX_H
#define SPARSE_MATRIX_H

#include <iostream>
#include <fstream>

using namespace std;


class SparseMatrix     // class for a Sparse matrix
   {
   public:
      SparseMatrix( char fname[] );      // constructor takes a filename
      SparseMatrix* transpose() const;   // return the transpose (a new matrix)

      // return a new matrix that is the result of multiplying two matrices
      SparseMatrix* multiply( const SparseMatrix *sm ) const;

      // overload the outstream operator
      friend ostream& operator<<( ostream& os, const SparseMatrix &sm );
   private:
      // disable the copy constructor, assignment operator, and destructor
      // -----------------------------------------------------------------
      SparseMatrix( SparseMatrix& sm );
      ~SparseMatrix();
      SparseMatrix& operator=( SparseMatrix &other );

      // class for one element in the Matrix
      // -----------------------------------
      class Entry
         {
         public:
            Entry( int c, int v, Entry *n );  
            int column;
	    int value;
            Entry *next;
         };
      
      int rows;           // the number of rows in the matrix
      int columns;        // the number of columns in the matrix
      Entry **rowHead;    // dynamically allocated array of head pointers.
      Entry **rowTail;    // dynamically allocated array of tail pointers.
      ifstream infile;    // the input file
   };

#endif 
Last edited on
Topic archived. No new replies allowed.