Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

matrices and structures 1

Status
Not open for further replies.

chineerat

Technical User
Oct 12, 2003
42
TT
hello once again.

i am having a problem, the bottom is an example of a file i want to read.


0 0 50 2
3 0 1 2
12 2 1 0

0 11 2 3
1 0 3 20
1 0 0 0

as you guessed it is matrices

i am storing both matrices (separated by a new line)in separated structures.

eg.
struct matrix_1 {
string number_1;
string row_1;
string col_1;
}data_1;

struct matrix_2 {
string number_2;
string row_2;
string col_2;
}data_2;


i am reading the file line by line so i can have a column count.
eg.

while(!inputfile.eof()){
string t;
getline(inputfile, t);
}

i am having trouble with creating an appropiate loop to ignore the zeros and store an integer when present and its row and column position, into the structure.

therefore for the above example my first structure will store:
50 row and column number
2 row and column number
3 row and column number
1 row and column number
2 row and column number
etc....


i've tried a million and one things already...
any suggestions will worth their vowels in gold.

THANKS in advance.

 
Sorry but I can't understand what is it: data_1.number_1 etc. I see two matrix 4x4 integers but what about your structures with string (text) members?
What's a true problem?
 
i may have stated my problem wrongly.

here i go again.
i have a text file with the following data.
two matrices

0 0 50 2
3 0 1 2
12 2 1 0

0 11 2 3
1 0 3 20
1 0 0 0

I would like to store the non-zero integers and their repective row and column postitons into 2 structures. Each structure representing a diferent matrix.

I hope I cleared up any misunderstanding.
THANKS
 
Well, it looks like your homework (see this forum rules;).

Try to define a structure type with int triad (value, row, column) then define a structure type with an int counter (of these triads) and an array (calculate 4x4 for extent value) of these triads.

Now you may read your data in two enclosed for loops (by rows and by columns) to fill this structure with the non-zero integers and their repective row and column postitons (use if statement in the inner loop body;).

Form this alg to a proper function. Call this function for matrix #1 then for matrix #2 input file portions.

Try this and present your code if problems occur...
 
I'm so so sorry.
I honestly didn't mean for it to sound like an assignment for you.
Is jus that i am having so many problems with this, i didn't define my problems specifically.
It won't happen again.
so I started from scratch and my first problem is :

I am getting the ERROR message
"error C2664: 'matrix_store' : cannot convert parameter 1 from 'std::ifstream' to 'std::ifstream'"

for the folloing code"
#include<iostream>
#include<vector>
#include<fstream>
#include<iostream>
#include<string>
using namespace std ;
using std::ifstream;

int matrix_store(ifstream file ){
int col_count = 0 ;
string t ;
while (getline(file, t , '\n')){
if(!t.empty()){
cout <<col_count<<endl;
col_count++ ;
}}
return 0;
}

int main(){
ifstream inputfile("matrix.txt");

while(!inputfile.eof()){
matrix_store( inputfile);
}
return 0;
}

2nd PROBLEM:

is there any conditional loop whereby the exit condition is a blank line eg. the following loop exits for each new line.

ifstream inputfile("matrix.txt");
string t;
while (getline(inputfile, t , '\n'))

Technically a blank line is two new lines. I've tried while (getline(inputfile, t , '\n''\n'))

any hints?

THanks in advance.
 
1. You need a simple istream (not more specific ifstream) reference to a stream (not a stream by value) in the 1st parameter of matrix_store function:
Code:
int matrix_store(istream& f) // Where is a matrix here?
2. Try this pattern to catch an empty (or blank!) line:
Code:
const int eos = std::string::npos; // 'bad' string position

int CntRows(istream& is)
{
  string s;
  int n, rows;

  for (rows = 0; getline(is,s); ++rows)
  {
     n = s.find_first_not_of(" \t"); // blanks or tabs
     if (n == eos)
	break;
     // Try to process the row contents here...
  }
  return rows;
}

3. The true problem: where is your column parser? It's a rather strange cout << col_count << endl stmt in your snippet (but it's equal to 0, of course).
 
THANKS ArkM.
but one more thing and i'll be out of your hair.
i not too sure if it is possible to do this
eventhough in the follow function it is returning a vector "ret"
i would like to get both "ret" and "pee" values from the split function into the main funtion.

std::vector<int> split( string& s)
{
int pee = 0 ;
int q = 0;
std::vector<int > ret;
typedef string::size_type string_size;
string_size i = 0;
while ( i != s.size() ) {
while ( i != s.size() && isspace(s) )
++i;
string_size j = i;
while ( j != s.size() && !isspace(s[j]) ){
++j;
if ( i != j ) {
stringstream ss(stringstream::in | stringstream::eek:ut);
ss<<s.substr(i, j-1);
ss>>q ;
if( q != 0){
ret.push_back(q);

}
else break;
}}
pee++ ;
i = j;
}
return ret ;
}


int main(){
....code
std::vector<int>words = split(tt) ;
// would get value of "pee" from "split" function
}

THANKS ALOT in ADVANCE
 
Try to extract integers from istringstrem directly by >> op, don't use isspace, substr etc low-level stuff: s >> q expression converts to false if no more integers in the basic string. Try this (extract and push into the vector in while loop). Better pass a reference to vector (or to full parsing result structure/class object) as the 2nd arg (it's a matter of taste;).
See you(r posts) later (timezone issue;)...
 
i am sorry i dont understand exactly what you're saying.
BUt i'll post my question in a new thread, (no offence), I appreciate he help you gave me earlier.

timezone issue?? by the way i am from trinidad in the caribbean, i think it is -4:00 GMT. what about you?

 
+04:00.

Code:
#include <sstream>
...
int x;
string s; // input string with integers

istringstream ss(s);
while (ss >> x)
  v.push_back(x);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top