473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

2-D vectors..string2int

HI,

I am working on a program which implements the simplex aglorithm. I
have decided to use a vector of vectors to set up my matrix. My
initial thought was to fill the row vector with type string, then
convert all but the operator(<=,>=,=) column to type int. This is
proving a bit harder then I thought. I thought the member function
atoi() did this, but I'm having problems. I'm also having some
problems with this code that I have been working on. I am able to fill
the first row, but that's about it. It seems to skip all but the first
iteration. Do I need to flush the stream buffer? Any tips on the
conversion from string to int(is it possible)? I'm a new user and
appreciate any input at all. Thanks for your time, this has sure taken
up some of mine. I'm compiler on Unix AIX 5.1

class TheMatrix
{
public:
vector <string> P;
vector< vector<string> > M;

~TheMatrix();
void ReadIn();
void AsktoReadIn();
};

// Deconstructor
TheMatrix::~TheMatrix()
{}

void TheMatrix::ReadIn()
{
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);

//vector <string> X;

while (ins >> temp)
{

P.push_back(temp);

}
M.push_back(P);
}
}

void TheMatrix::AsktoReadIn()
{
char ch;
//ReadIn();
do
{

ReadIn();
cout << "another equation?";
cin >> ch; //seems to be skipping by this???
}while (ch=='y');

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}

}

}

int main()
{
TheMatrix A;
A.AsktoReadIn();
return 0;
Jul 22 '05 #1
1 2011
On 31 Jul 2004 09:57:30 -0700, Rich <do*****@csusm.edu> wrote:
HI,

I am working on a program which implements the simplex aglorithm. I
have decided to use a vector of vectors to set up my matrix. My
initial thought was to fill the row vector with type string, then
convert all but the operator(<=,>=,=) column to type int. This is
proving a bit harder then I thought. I thought the member function
atoi() did this, but I'm having problems. I'm also having some
problems with this code that I have been working on. I am able to fill
the first row, but that's about it. It seems to skip all but the first
iteration. Do I need to flush the stream buffer? Any tips on the
conversion from string to int(is it possible)? I'm a new user and
appreciate any input at all. Thanks for your time, this has sure taken
up some of mine. I'm compiler on Unix AIX 5.1

class TheMatrix
{
public:
vector <string> P;
vector< vector<string> > M;

~TheMatrix();
void ReadIn();
void AsktoReadIn();
};

// Deconstructor
TheMatrix::~TheMatrix()
{}

void TheMatrix::ReadIn()
{
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
string eqn;
string temp;

{
cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);

//vector <string> X;

while (ins >> temp)
{

P.push_back(temp);

}
M.push_back(P);
}
}

void TheMatrix::AsktoReadIn()
{
char ch;
//ReadIn();
do
{

ReadIn();
cout << "another equation?";
cin >> ch; //seems to be skipping by this???
}while (ch=='y');

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}

}

}

int main()
{
TheMatrix A;
A.AsktoReadIn();
return 0;


There seems to be quite a long wrong with your code and your design. Here
are some miscellaneous points

1) What is the P doing as a class member? You are trying to build up a
matrix M, I can understand that, but then why P?

I think your code should look more like this

cout<<"Enter an equation:\n" << endl;
getline(cin, eqn);
istringstream ins;
ins.str(eqn);
vector <string> P;
while (ins >> temp)
{
P.push_back(temp);
}
M.push_back(P);

See how P is a local variable not a class member.

2) Look at the code that prints out the matrix.

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < P.size(); j++)
{
cout << P[j] << " ";
}
}

This just prints out values from P again. Values from the matrix M are
never printed at all. I think you want something more like this

for (int i = 0; i < M.size(); i++)
{
for (int j = 0; j < M[i].size(); j++)
{
cout << M[i][j] << " ";
}
cout << '\n';
}

3) In the function TheMatrix::ReadIn you seem to have duplicated the same
piece of code twice, except that you ignore what you read in the first
time. I don't know what the reason for that is. Maybe you news client
messed up your code?

4) atoi is used to convert strings to integers (its not a member function
however just an ordinary function). Here's an example of its use

string a_string;
cin >> a_string;
int an_int = atoi(a_string.c_str());

5) I don't see the skipping past behaviour that you describe and I can't
see any reason in the code you've posted why that would happen. You
certainly don't need to flush stream buffer however.

6) But really I think your design is wrong. I would think about starting
again with a better design. It's a while since I studied the simplex
algorithm but as I recall its about solving systems of linear
inequalities. I think you need something better than a vector<string> to
hold an equation. I think you will need to invent your own classes to hold
equation data.

For instance

// a term in an equation
struct Term
{
double coeff;
char var;
};

// equation types
enum
{
lessThan,
lessThanOrEqual,
greaterThan,
greaterThanOrEqual,
};

struct LinearEquation
{
vector<Term> terms; // the terms on the left hand side
int type; // the inequality operator
double value; // the value on the right hand side
};

So for instance if the user entered the equation 5x < 6y + 7 you would end
up executing code like this

Equation eqn;
Term t;
t1.coeff = 5.0;
t1.var = 'x';
eqn.push_back(t);
t1.coeff = -6.0; // -6 because 6y has moved from the rhs to the lhs
t1.var = 'y';
eqn.push_back(t);
eqn.type = lessThan;
eqn.value = 7.0;

Or something like that, that was just off the top of my head but I think
you need a bit more structure than just a simple vector<string>. It does
mean that the code to input an equation will be more complex, but I don't
think short cuts are advisable here, you will just end up with more
complexity later on.

john
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Michael Aramini | last post by:
I need to represent 1D and 2D arrays of numeric or bool types in a C++ program. The sizes of the arrays in my intended application are dynamic in the sense that they are not known at compile time,...
5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
2
by: wuzertheloser | last post by:
Use the program skeleton below (starting with #include <stdio.h>) as the starting point for quiz4. Add the necessary code to the functions prob1() and prob2(), and add the other 2 functions, as...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
2
by: joeme | last post by:
How would one using STL do the following tasks: 1) merge 2 sorted vectors with dupes, result shall be sorted 2) merge 2 sorted vectors without dupes, result shall be sorted 3) merge 2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.