473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

matrix 2x2

How write class of matrix 2x2 acted following program in order to?

int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 a(tab), b(1.0, 0.0, 0.0, -1.0), c = a + b, d;

d = -c;

cout << "\n matrix a: " << a;
cout << ", matrix b: " << b;
cout << ", matrix c: " << c;
cout << ", matrix d: " << d;
cout << ", det a: " << a.det();
cout << ", det b: " << b.det();
cout << ", det c: " << c.det();
cout << ", det d: " << d.det();

return 0;
}

Apr 9 '06 #1
8 4256
In article <11*********************@j33g2000cwa.googlegroups. com>,
wo********@gmail.com wrote:
How write class of matrix 2x2 acted following program in order to?

int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 a(tab), b(1.0, 0.0, 0.0, -1.0), c = a + b, d;

d = -c;

cout << "\n matrix a: " << a;
cout << ", matrix b: " << b;
cout << ", matrix c: " << c;
cout << ", matrix d: " << d;
cout << ", det a: " << a.det();
cout << ", det b: " << b.det();
cout << ", det c: " << c.det();
cout << ", det d: " << d.det();

return 0;
}


Simple. Read the first compiler error you get when you try to compile
the above, then write some code that will fix the error, recompile.
Repeat until no compiler errors occur. Then do the same with the link
errors, then you are done.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 9 '06 #2
wo********@gmail.com wrote:
How write class of matrix 2x2 acted following program in order to?

int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 a(tab), b(1.0, 0.0, 0.0, -1.0), c = a + b, d;

d = -c;

cout << "\n matrix a: " << a;
cout << ", matrix b: " << b;
cout << ", matrix c: " << c;
cout << ", matrix d: " << d;
cout << ", det a: " << a.det();
cout << ", det b: " << b.det();
cout << ", det c: " << c.det();
cout << ", det d: " << d.det();

return 0;
}


Wow! If that's the best you can do, I suggest you start with something
much simpler... I've only been programming in C++ for about 3 months but
at least I could do the above code much better than you.

In case you're really lost, you could start with a text-book on c++ and
make a "hello world"-program and do the exercises.

I don't think anyone wants to give you the complete solution to your
problem when you present a program that lacks so much...

For instance: Where's your matrix2x2 class? Do you think your program
will compile as it looks now?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 9 '06 #3
In article <g7************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote:
wo********@gmail.com wrote:
How write class of matrix 2x2 acted following program in order to?

int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 a(tab), b(1.0, 0.0, 0.0, -1.0), c = a + b, d;

d = -c;

cout << "\n matrix a: " << a;
cout << ", matrix b: " << b;
cout << ", matrix c: " << c;
cout << ", matrix d: " << d;
cout << ", det a: " << a.det();
cout << ", det b: " << b.det();
cout << ", det c: " << c.det();
cout << ", det d: " << d.det();

return 0;
}


Wow! If that's the best you can do, I suggest you start with something
much simpler... I've only been programming in C++ for about 3 months but
at least I could do the above code much better than you.

In case you're really lost, you could start with a text-book on c++ and
make a "hello world"-program and do the exercises.

I don't think anyone wants to give you the complete solution to your
problem when you present a program that lacks so much...

For instance: Where's your matrix2x2 class? Do you think your program
will compile as it looks now?


Martin,

Not so harsh on the newbies. :-) Actually, he has an excellent start
in that he knows what the interface of his class should look like, and
that should always be the first thing you figure out.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 10 '06 #4
Daniel T. wrote:
In article <g7************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote: -snip-
Martin,

Not so harsh on the newbies. :-) Actually, he has an excellent start
in that he knows what the interface of his class should look like, and
that should always be the first thing you figure out.


Uh, sorry... I thought perhaps it was some homework exercise and would
appreciate some more specific questions other than "this doesn't work, I
need a solution. Please give me a complete solution, so I don't have to
think". :-)

But you have a really good point, and I can also remember how it is to
be a newbie. So, I am willing to help with problems, if the OP posts his
problems... I can probably learn something still... :-)

This is untested but I believe he could/perhaps should start with
something like:
class matrix2x2
{
private:
// private variables here
double m1, m2, m3, m4;

public:
// member functions and constructur goes in here
matrix2x2() : m1(0), m2(0), m3(0), m4(0) { }

matrix2x2(double mat1, double mat2, double mat3, double mat4) :
m1(mat1), m2(mat2), m3(mat3), m4(mat4) { }
// initialize variables...

det()
{ // insert code for calculating determinant here
}

matrix2x2 operator + (matrix2x2 m1, matrix2x2 m2)
{
// operator overloading of plus "+" <- insert code here
}

(return type) operator << (.... bla. bla.)
{
// operator overloading of plus "<<" <- insert code here
}
}
But I didn't test it... I would like to see what OP ends up with... At
least there should be something to work on now... And we can answer any
questions that arise, I think... The most difficult part (at least for
me) would be overloading this "<<"-thing. AFAIR it has something to do
with streams and I would have to look that up in my book in order to
figure that part out.

I guess one could also declare a 2D array in the private-part of the
class, so it works more like this: const double tab[][2]= {{1.5, 2.5},
{3.5, 4.5}};

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 10 '06 #5
In article <dt************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote:
Daniel T. wrote:
In article <g7************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote: -snip-
Martin,

Not so harsh on the newbies. :-) Actually, he has an excellent start
in that he knows what the interface of his class should look like, and
that should always be the first thing you figure out.


Uh, sorry... I thought perhaps it was some homework exercise and would
appreciate some more specific questions other than "this doesn't work, I
need a solution. Please give me a complete solution, so I don't have to
think". :-)

But you have a really good point, and I can also remember how it is to
be a newbie. So, I am willing to help with problems, if the OP posts his
problems... I can probably learn something still... :-)


The thing is, I think his assignment was to come up with the interface
(ie exactly what you did.) By all means do other people's homework if it
seems at all challenging (you can learn a lot from that,) but try to
just post hints to the OP after you get it done.
This is untested but I believe he could/perhaps should start with
something like:
class matrix2x2
{
private:
// private variables here
double m1, m2, m3, m4;

public:
// member functions and constructur goes in here
matrix2x2() : m1(0), m2(0), m3(0), m4(0) { }

matrix2x2(double mat1, double mat2, double mat3, double mat4) :
m1(mat1), m2(mat2), m3(mat3), m4(mat4) { }
// initialize variables...

det()
{ // insert code for calculating determinant here
}

matrix2x2 operator + (matrix2x2 m1, matrix2x2 m2)
{
// operator overloading of plus "+" <- insert code here
}

(return type) operator << (.... bla. bla.)
{
// operator overloading of plus "<<" <- insert code here
}
}
But I didn't test it... I would like to see what OP ends up with... At
least there should be something to work on now... And we can answer any
questions that arise, I think... The most difficult part (at least for
me) would be overloading this "<<"-thing. AFAIR it has something to do
with streams and I would have to look that up in my book in order to
figure that part out.

I guess one could also declare a 2D array in the private-part of the
class, so it works more like this: const double tab[][2]= {{1.5, 2.5},
{3.5, 4.5}};


I would use a one dimensional array over a two dimensional one, but what
you have is fine.

Of course, 'det' needs a return type. I would put the operator+ outside
the class since it doesn't work on 'this', and operator<< needs to be
outside the class as well:

ostream& operator<<( ostream& os, matrix2x2 mat ) {
return os << //... blah, blah...
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 10 '06 #6
Daniel T. wrote:
In article <dt************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote: -snip-
The thing is, I think his assignment was to come up with the interface
(ie exactly what you did.) By all means do other people's homework if it
seems at all challenging (you can learn a lot from that,) but try to
just post hints to the OP after you get it done.
Yep, agreed. But I'm also a bit busy working on my own bs.c project so I
hope the OP soon will come with some new inputs :-)
This is untested but I believe he could/perhaps should start with
something like:
class matrix2x2
{
private:
// private variables here
double m1, m2, m3, m4;

public:
// member functions and constructur goes in here
matrix2x2() : m1(0), m2(0), m3(0), m4(0) { }

matrix2x2(double mat1, double mat2, double mat3, double mat4) :
m1(mat1), m2(mat2), m3(mat3), m4(mat4) { }
// initialize variables...

det()
{ // insert code for calculating determinant here
}

matrix2x2 operator + (matrix2x2 m1, matrix2x2 m2)
{
// operator overloading of plus "+" <- insert code here
}

(return type) operator << (.... bla. bla.)
{
// operator overloading of plus "<<" <- insert code here
}
}
But I didn't test it... I would like to see what OP ends up with... At
least there should be something to work on now... And we can answer any
questions that arise, I think... The most difficult part (at least for
me) would be overloading this "<<"-thing. AFAIR it has something to do
with streams and I would have to look that up in my book in order to
figure that part out.

I guess one could also declare a 2D array in the private-part of the
class, so it works more like this: const double tab[][2]= {{1.5, 2.5},
{3.5, 4.5}};

I would use a one dimensional array over a two dimensional one, but what
you have is fine.


How is that... "a one-dim. array over a two-dim. array"?

The problem with what I posted is likely that it's not possible to scale
up the solution in order to add something like 3x3 matrixes... But at
least he got something to look at and modify...
Of course, 'det' needs a return type. I would put the operator+ outside
the class since it doesn't work on 'this', and operator<< needs to be
outside the class as well:
Oh... I thought it didn't matter if it were inside or outside the class...?
ostream& operator<<( ostream& os, matrix2x2 mat ) {
return os << //... blah, blah...
}


Oh, yeah... This I couldn't remember but I could have looked it up, but
then again I think it's the OP who should try to solve this problem
before I do it :-)

At least he got plenty of hints to get started now, I think :-)

And this is actually a "funny" exercise I think, so he don't have any
excuses for not posting his solution (or his attempt to get a solution)...
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 11 '06 #7
In article <jd************@news.tdc.dk>,
Martin Jørgensen <un*********@spam.jay.net> wrote:
Daniel T. wrote:

I would use a one dimensional array over a two dimensional one, but what
you have is fine.


How is that... "a one-dim. array over a two-dim. array"?


class maatrix2x2 {
double m[4];
public:
//...
};
Of course, 'det' needs a return type. I would put the operator+ outside
the class since it doesn't work on 'this', and operator<< needs to be
outside the class as well:


Oh... I thought it didn't matter if it were inside or outside the class...?


You can put any function inside the class but why? If the only functions
in the class are ones that use "this" then your interface will be
cleaner, and intentions will be pure.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 11 '06 #8
Daniel T. wrote:
-snip-

Ok...

Seems like the OP perhaps isn't interested in learning C++ anyway or he
was only interested in a solution?
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
Apr 12 '06 #9

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

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.