funtion that counts coloumn.

Pages: 12
I have this mtx file:

%%MatrixMarket matrix coordinate integer general
%
5 5 10
1 1 11
1 5 15
2 3 23
2 4 24
3 2 32
3 5 35
4 1 41
5 2 52
5 4 54
5 5 55

i want to make a function that counts which coloumn am i in.

also i need a fuction so i can ignore the line with '%' charecter
and the first entry as it represent dimensions and the entries of the matrix.

(the one i poste is a 5x5 matrix with 10 entrees)
Last edited on
Somewhat pseudo-code-ish.
It's unlikely to compile, but simple research should be able to fix it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string line;
bool hasDimensions = false;
while ( getline(cin,line) ) {
    if ( line[0] == '%' ) continue;
    istringstream is(line);
    if ( !hasDimensions ) {
        int nRows, nCols, nEntries;
        is >> nRows >> nCols >> nEntries;
        hasDimensions = true;
    } else {
        int row, col, value;
        is >> row >> col >> value;
    }
}
my apologies but here is the entire code here:
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

string wordcolumn(string column,int count){
for (int i=0;i<count;i++) {
}
return 0;
}

int main(int argc, char *argv[])
{
string filename, content, line;
int dimensions, entrees = 0, entrees1 = 0, entrees2 = 0;
double element;
bool sparse;
string pattern_general, word1, word2;
float frobenius_norm;
ifstream openfile;
filename = argv[1];
openfile.open(filename);
cout <<"Filename: " << filename << endl;
while(openfile.good())
{
openfile >> word1 >> word1 >> word1 >> word1 >> word2;
while(openfile.peek()=='%'){
openfile.ignore(0,'\n');
while(getline(openfile,content))
{
entrees2++;
}
}
// openfile >> line >> dimensions >> entrees;
}
while(!openfile.eof()){
while(getline(openfile,content)){
if(content != "0"){
sparse = true;
}
}
}



if(!openfile.is_open()){
cout << "Sorry, but the file don't exist!\n";
exit(1);
}

openfile.close();
cout <<"Dimensions: " << dimensions << " x " << dimensions << endl;
cout <<"Entries: " << entrees2 <<endl;
cout <<"Sparse: "<< "true" <<endl;
cout <<"Type: "<< word1 << ' ' << word2 <<endl;
cout <<"Frobenius norm: "<<'-' <<endl;
return 0;
}
Please edit to put [code][/code] tags around your code.
right

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
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

string wordcolumn(string column,int count){
for (int i=0;i<count;i++) {
}
return 0;
}

int main(int argc, char *argv[])
{
string filename, content, line;
int dimensions, entrees = 0, entrees1 = 0, entrees2 = 0;
double element;
bool sparse;
string pattern_general, word1, word2;
float frobenius_norm;
ifstream openfile;
filename = argv[1];
openfile.open(filename);
cout <<"Filename: " << filename << endl;
while(openfile.good())
{
openfile >> word1 >> word1 >> word1 >> word1 >> word2;
while(openfile.peek()=='%'){
openfile.ignore(0,'\n');
while(getline(openfile,content))
{
entrees2++;
}
}
// openfile >> line >> dimensions >> entrees;
}
while(!openfile.eof()){
while(getline(openfile,content)){
if(content != "0"){
sparse = true;
}
}
}



if(!openfile.is_open()){
cout << "Sorry, but the file don't exist!\n";
exit(1);
}

openfile.close();
cout <<"Dimensions: " << dimensions << " x " << dimensions << endl;
cout <<"Entries: " << entrees2 <<endl;
cout <<"Sparse: "<< "true" <<endl;
cout <<"Type: "<< word1 << ' ' << word2 <<endl;
cout <<"Frobenius norm: "<<'-' <<endl;
return 0;
}
I was rather hoping to see some indentation.
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
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

string wordcolumn(string column, int count)
{
  for (int i = 0; i < count; i++) {
  }
  return 0;
}

int main(int argc, char *argv[])
{
  string filename, content, line;
  int dimensions, entrees = 0, entrees1 = 0, entrees2 = 0;
  double element;
  bool sparse;
  string pattern_general, word1, word2;
  float frobenius_norm;
  ifstream openfile;
  filename = argv[1];

  openfile.open(filename);
  cout << "Filename: " << filename << endl;
  while (openfile.good()) {
    openfile >> word1 >> word1 >> word1 >> word1 >> word2;
    while (openfile.peek() == '%') {
      openfile.ignore(0, '\n');
      while (getline(openfile, content)) {
        entrees2++;
      }
    }
    // openfile >> line >> dimensions >> entrees;
  }

  while (!openfile.eof()) {
    while (getline(openfile, content)) {
      if (content != "0") {
        sparse = true;
      }
    }
  }

  if (!openfile.is_open()) {
    cout << "Sorry, but the file don't exist!\n";
    exit(1);
  }

  openfile.close();
  cout << "Dimensions: " << dimensions << " x " << dimensions << endl;
  cout << "Entries: " << entrees2 << endl;
  cout << "Sparse: " << "true" << endl;
  cout << "Type: " << word1 << ' ' << word2 << endl;
  cout << "Frobenius norm: " << '-' << endl;
  return 0;
}


You should not have three nested while loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  openfile.open(filename);
  if (!openfile.is_open()) {
    cout << "Sorry, but the file don't exist!\n";
    exit(1);
  }

  // Deal with this line
  // %%MatrixMarket matrix coordinate integer general

  // Deal with any comments
  // %

  // Deal with the size and count line
  // 5 5 10

  // Deal with the remainder of row col value 

After completing each step, TEST your code.
I have hard time dealing with the lines and how i seperates them...
Your examples of other files seem to have disappeared.
i dont find that code viable though
i really dont know with how to deal with this line;

%%MatrixMarket matrix coordinate integer general

any comment containing "%" i tried.


those have i biggset issue with:

// Deal with the size and count line
// 5 5 10

// Deal with the remainder of row col value

i had to renovate my code;
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
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

string wordcolumn(string column,int count){
    for (int i=0;i<count;i++) {
        column;
    }
}

int main(int argc, char *argv[])
{
    string filename, content, line;
    int dimensionx, dimensiony, entrees = 0, entrees1 = 0, entrees2 = 0;
    double element;
    bool sparse;
    string pattern_general, word1, word2;
    float frobenius_norm;
    ifstream openfile;
    filename = argv[1];
    openfile.open(filename);

    while(!openfile.eof())
    {
        getline(openfile,content);
        word1;
        while(getline(openfile,content,'%')||getline(openfile,content,' ')){
            entrees++;
        }
////
   }

    if(!openfile.is_open()){
        cout << "Sorry, but the file don't exist!\n";
        exit(1);
    }
    cout <<"Filename: " << filename << endl;
    cout <<"Dimensions: " << dimensionx << " x " << dimensiony << endl;
    cout <<"Entries: " << entrees <<endl;
    cout <<"Sparse: "<< "true" <<endl;
    cout <<"Type: "<< word1 << ' ' << word2 <<endl;
    cout <<"Frobenius norm: "<<'-' <<endl;
        return 0;
}
You keep failing, because you keep trying to do everything in one disorganised mess.

Start small, compile and test OFTEN!
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;

void processLine(const string &line)
{
    if ( line[0] == '%' ) {
        cout << "Comment=" << line << endl;
    } else {
        cout << "Data=" << line << endl;
    }
}

void processFile(const char *filename)
{
    ifstream inf("foo.txt");
    if ( inf.is_open() ) {
        string line;
        while ( getline(inf,line) ) {
            processLine(line);
        }
    } else {
        cout << "Sorry, but the file don't exist!\n";
    }
}

int main(int argc, char *argv[])
{
    if ( argc < 2 ) {
        cout << "Need a filename\n";
    } else {
        processFile(argv[1]);
    }
}



$ ./a.out 
Need a filename
$ ./a.out foo.txt
Comment=%%MatrixMarket matrix coordinate integer general
Comment=%
Data=5 5 10
Data=1 1 11
Data=1 5 15
Data=2 3 23
Data=2 4 24
Data=3 2 32
Data=3 5 35
Data=4 1 41
Data=5 2 52
Data=5 4 54
Data=5 5 55


Make sure this works as you would expect for all the files you have.

Then you proceed by making ONE refinement (say parsing the first line) and then retesting with all your files to make sure you're still on the right track.

Here is my updated code:
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
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

string wordcolumn(string column,int count){
    for (int i=0;i<count;i++) {
        column;
    }
}

int main(int argc, char *argv[])
{
    string filename, nothing, line, type, header,tempstring;
    int dimensionx, dimensiony, entrees = 0, entrees1 = 0, entrees2 = 0;
    int d1,d2,entry;
    double element;
    bool sparse;
    float frobenius_norm;
    ifstream openfile;
    filename = argv[1];
    openfile.open(argv[1]);
    if(!openfile.is_open()){
        cout << "Sorry, but the file don't exist!\n";
        exit(1);
    }

        for(int i =0;i<2;i++){
            getline(openfile,line,' ');
    }
// i want to find the word pattern on the first stringline so the frobenious norm is '-'; but i dont know the commando that find the stringword...
    if(openfile.peek()=='pattern'){
        frobenius_norm = '-';
    } else {
        frobenius_norm = 0.0;
    }
    getline(openfile,type);
// here i want to check if the line has % as first charecter or empty
    if(openfile.peek()==('%'||' '))
    {
        getline(openfile,nothing);
        cout << nothing << endl;
    }
// this is where it checks the matrix dimensions and entrees.
    openfile >> d1 >> d2 >> entry;

//    getline(openfile,word2);

//    getline(openfile,tempstring);



    cout <<"Filename: " << filename << endl;
    cout <<"Dimensions: " << /*tempstring */ d1 << " x " << d2 /*dimensiony*/ << endl;
    cout <<"Entries: " << entry /*entrees*/ <<endl;
    cout <<"Sparse: "<< "true" <<endl;
    cout <<"Type: "<< type <<endl;
    cout <<"Frobenius norm: "<< frobenius_norm <<endl;
    return 0;
}



so far so good
Last edited on
Filename: sparse01.mtx
Dimensions: 9 x 9	
Entries: 50	
Sparse: true	
Type: pattern general	
Frobenius norm: -


that is how output should looklike... of this file:

%%MatrixMarket matrix coordinate pattern general
9 9 50
1 1
2 1
4 1
5 1
6 1
7 1
8 1
9 1
2 2
3 2
8 2
9 2
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3
4 4
5 4
6 4
7 4
8 4
9 4
4 5
5 5
6 5
7 5
8 5
9 5
4 6
5 6
6 6
7 6
8 6
9 6
1 7
2 7
3 7
8 7
9 7
8 8
9 8
1 9
2 9
3 9
8 9
9 9


well so far so good i.
Last edited on
this is my new set of code:

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
int main(int argc, char *argv[])
{
    string filename, line, type1, type2, header;
    int x, y, entry, count=0;
    double element;
    char norm;
    float frobenius_norm;
    ifstream openfile;

    filename = argv[1];
    openfile.open(argv[1]);

    if(!openfile.is_open())
    {
        cout << "Sorry, but the file don't exist!\n";
        exit(1);
    }

    for(int i =0;i<2;i++)
    {
        getline(openfile,line,' ');
    }

    getline(openfile,header,' ');
    getline(openfile,type1,' ');
    getline(openfile,type2);
// here i want to check if there is a '%' sign as first charecter beside the first line, if it is then i want to ignore that line, and i also want to check if there is blank line, which i also want to ignore it. But i cant make it work...
    if(getline(openfile,line).peek()=='%'){
        cout << "lol"<<endl;
    }
this is current matrix dimensions and entries:
    openfile >> x >> y >> entry;

// here i want to how i seperates matrix values as arrays/vectors, so i can calculate frobenius norm...

    cout <<"Filename: " << filename << endl;
    cout <<"Dimensions: " << x << " x " << y << endl;
    cout <<"Entries: " << entry <<endl;

    if(header == "coordinate")
    {
        cout <<"Sparse: "<< "true" <<endl;
    }
    else if ((x*y/2)<entry||header == "array")
    {
     cout <<"Sparse: "<< "false" <<endl;
    }

    cout <<"Type: "<< type1 << ' ' << type2 <<endl;

    if(type1 == "pattern"){
        norm = '-';
        cout <<"Frobenius norm: "<< norm <<endl;
    } else {
        frobenius_norm = 100;
        cout <<"Frobenius norm: "<< frobenius_norm <<endl;
    }

    return 0;
}


if i remove the if statement, reading sparse01.mtx file would give this result:
Filename: sparse01.mtx
Dimensions: 9 x 9	
Entries: 50	
Sparse: true	
Type: pattern general	
Frobenius norm: -


but if i include this then this result comes in:

Filename: sparse01.mtx
Dimensions: 1 x 1
Entries: 2
Sparse: true
Type: pattern general
Frobenius norm: -


and sparse02.mtx has this:

%%MatrixMarket matrix coordinate integer general
%
5 5 10
1 1 11
1 5 15
2 3 23
2 4 24
3 2 32
3 5 35
4 1 41
5 2 52
5 4 54
5 5 55

and sparse03 gives this matrix:
%%MatrixMarket matrix coordinate real general
%
% Hello
% How
% Are
% you
% doing
%

5 5 10
1 1 11.0
1 5 15.0
2 3 23.0
2 4 24.0
3 2 32.0
3 5 35.0
4 1 41.0
5 2 52.0
5 4 54.0
5 5 55.0

the % and empty spaces are hinderance and cant get a good result.. pls help
salem c: i really appreciate your effort to make this code and i made some slight modification to it:

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
void processFile(const char *filename)
{
    ifstream inf(filename);
    if ( inf.is_open() ) {
        string line,type;
        while ( getline(inf,line) ) {
          //  processLine(line);
          if ( line[1]&&line[0] == '%' )
          {
              cout << line << endl;
          } else if (line[0]=='%')
          {
              cout << line << endl;
          } else {
              cout << line << endl;
        }
    }
    }else {
        cout << "Sorry, but the file don't exist!\n";
    }
}

int main(int argc, char *argv[])
{
    if (argc==2){
    cout <<"Filename: " << argv[1] << endl;
        processFile(argv[1]);
} else {
    cout << "give a filename\n";
}
}


but the code will exactly the input files give and in the form of strings:
Filename: sparse01.mtx
%%MatrixMarket matrix coordinate pattern general
9 9 50
1 1
2 1
4 1
5 1
6 1
7 1
8 1
9 1
2 2
3 2
8 2
9 2
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 3
4 4
5 4
6 4
7 4
8 4
9 4
4 5
5 5
6 5
7 5
8 5
9 5
4 6
5 6
6 6
7 6
8 6
9 6
1 7
2 7
3 7
8 7
9 7
8 8
9 8
1 9
2 9
3 9
8 9
9 9


I really can't extract the last two words of the first matrix line (second, if we include the filename: sparse01.mtx)

and I can't separate the numbers below..

as for the sparse03.mtx

extract two words:%%MatrixMarket matrix coordinate real general
ignore the lines%
ignore the lines% Hello
ignore the lines% How
ignore the lines% Are
ignore the lines% you
ignore the lines% doing
ignore the lines%
this line has no charecters and i want to ignore that...
dimensions and antry amount, position and value of each matrix:
dimensions and antry amount, position and value of each matrix: 5  5  10
dimensions and antry amount, position and value of each matrix: 1  1  11.0
dimensions and antry amount, position and value of each matrix: 1  5  15.0
dimensions and antry amount, position and value of each matrix: 2  3  23.0
dimensions and antry amount, position and value of each matrix: 2  4  24.0
dimensions and antry amount, position and value of each matrix: 3  2  32.0
dimensions and antry amount, position and value of each matrix: 3  5  35.0
dimensions and antry amount, position and value of each matrix: 4  1  41.0
dimensions and antry amount, position and value of each matrix: 5  2  52.0
dimensions and antry amount, position and value of each matrix: 5  4  54.0
dimensions and antry amount, position and value of each matrix: 5  5  55.0

How many combinations of the first line are possible?
1
2
3
%%MatrixMarket matrix coordinate integer general
%%MatrixMarket matrix coordinate real general
%%MatrixMarket matrix coordinate pattern general


Having read a line, you could do this
1
2
3
4
5
if ( line.find("%%MatrixMarket") != std::string::npos ) {
    stringstream is(line);
    string ident, matrix, coordinate, type, general;
    is >> ident >> matrix >> coordinate >> type >> general;
}
thank you Salem c;

now I have to single out the top line of integers.

and assign the rest to an array;
1
2
3
4
5
        stringstream sa(line);
        int x,y,entry;
        sa >> x >> y >> entry;
        cout << "Dimensions: "<< x << " x " << y << endl;
        cout << "Entries: " << entry << endl;


but if i write it on the output, then cout would assign on every single line...

well i want to assign the top line as dimensions, and the rest bottom as array/vector;

Filename: sparse02.mtx
Type: integer general
Sparse: true
Dimensions: 5 x 5
Entries: 10
Dimensions: 1 x 1
Entries: 11
Dimensions: 1 x 5
Entries: 15
Dimensions: 2 x 3
Entries: 23
Dimensions: 2 x 4
Entries: 24
Dimensions: 3 x 2
Entries: 32
Dimensions: 3 x 5
Entries: 35
Dimensions: 4 x 1
Entries: 41
Dimensions: 5 x 2
Entries: 52
Dimensions: 5 x 4
Entries: 54
Dimensions: 5 x 5
Entries: 55
how do I store all integers into an array (or vector)

int x,y,entry;

x y entry
5 5 10
1 1 11
1 5 15
2 3 23
2 4 24
3 2 32
3 5 35
4 1 41
5 2 52
5 4 54
5 5 55
Last edited on
i give up...
if I put in on vector then the first vector holds all values.

things get even worse when the dimension has more than a single digit.
Pages: 12