|
I made some modifications to you code.
Note that your multiplication operation is out of standard (I haven't touched it and the division one): matrix multiplication is usually defined this way:
m[i,j] = a[i,0] * b[0,j] + a[i,1] * b[1,j] + ... + a[i,N] * b[N,j]
Beware, returning, as you did, element 0 when the requested index is out of bounds, it is not a good practice.
#include <iostream>
#include <string>
using namespace std;
class matrix
{
public:
struct Exception
{
string msg;
Exception(string emsg)
{
this->msg=msg;
}
};
matrix();
matrix(int m,int n);
~matrix(){ delete [] data; }
int getRows();
int getCols();
double& operator()(int, int);
friend ostream& operator<<(ostream& os, matrix& m);
matrix operator + (matrix&);
matrix operator * (matrix&);
matrix operator - (matrix&);
matrix operator / (matrix&);
private:
void init(int, int);
int nrows,ncols;
double *data;
};
matrix::matrix(){
init(1,1);
}
matrix::matrix(int m, int n){
init(m,n);
}
void matrix::init(int m, int n){
nrows=m;
ncols=n;
data= new double[m*n];
for(int i=0; i<m*n; i++)
data[i]=.0;
}
int matrix::getRows() { return nrows;}
int matrix::getCols() { return ncols;}
double& matrix::operator ()(int r, int c){
if (r <0 || r>= nrows || c<0 || c>= ncols)
throw matrix::Exception("out of bounds");
return data[r*ncols+c];
}
ostream& operator<<(ostream& os, matrix &m)
{
for(int r=0; r<m.nrows; r++)
{
for(int c=0; c < m.ncols; c++)
os<<m(r,c) << " ";
os<<endl;
}
return os;
}
matrix matrix::operator+(matrix& a)
{
if ( nrows != a.nrows || ncols!= a.ncols )
throw Exception("invalid operands");
matrix sum(nrows, ncols);
for (int r=0; r<nrows; r++)
for(int c=0; c<ncols; c++)
{
int i = r * ncols + c;
sum.data[i] = data[i] + a.data[i];
}
return sum;
}
matrix matrix::operator*(matrix& a)
{
matrix product(nrows, ncols);
for (int r=0; r<nrows; r++)
for(int c=0; c<ncols; c++)
{
product(r,c) = (r,c) * a(r,c);
}
return product;
}
matrix matrix::operator-(matrix& a)
{
if ( nrows != a.nrows || ncols!= a.ncols )
throw Exception("invalid operands");
matrix difference(nrows, ncols);
for (int r=0; r<nrows; r++)
for(int c=0; c<ncols; c++)
{
int i = r * ncols + c;
difference.data[i] = data[i] - a.data[i];
}
return difference;
}
matrix matrix::operator/(matrix& a)
{
matrix divide(nrows, ncols);
for (int r=0; r<nrows; r++)
for(int c=0; c<ncols; c++)
divide(r,c) = (r,c) / a(r,c);
return divide;
}
int main()
{
matrix a(2,2);
a(1,1)=5.0;
a(0,0)=6.0;
a(0,1)=7.0;
a(1,0)=8.0;
cout<<a(0,0)<<endl;
cout<<a(1,1)<<endl;
cout<<a<<endl;
matrix b(2,2);
b(0,0) = 5.0;
b(0,1) = 5.0;
b(1,0) = 5.0;
b(1,1) = 5.0;
cout<<b<<endl;
matrix c,d,e,f;
c=a+b;
d=a*b;
e=a-b;
f=a/b;
cout<<c<<endl;
cout <<"\n\n";
cout<<d<<endl;
cout<<e<<endl;
cout <<"\n\n";
cout<<f<<endl;
system("pause");
return 0;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified on Sunday, January 3, 2010 2:18 PM
|
|
|
|
|
CPallini wrote: m[i,j] = a[i,0] * b[0,j] + a[i,1] * b[1,j] + ... + a[i,N] * b[2,N]
b[2,N] or b[N,j]?
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Nelek wrote: b[2,N] or b[N,j]?
b[2,N] is the right one, of course: keep you up to date man, there are many-many advantages with that definition, for instance in relativistic computations...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: for instance in relativistic computations
Oh... I thought it was for quantic physics
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
such a long reply.. happy new year!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
ThatsAlok such a long reply..
Probably ignored: he posted again is question...
ThatsAlok happy new year!
Happy new year, Alok! Best wishes!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
The following program is taking the inputs in the structures but when it comes to print them the program terminates abruptly.
//OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers,
//model, car brand and to store them in structures
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct data{
char oname[20];
int chno;
int model;
char brand[20];
};
main()
{
int slot,i=0;
typedef struct data a;
printf("Enter the no. of data slots you want to make\n");
scanf("%d",&slot);
a c[slot];
for (i=0;i<slot;i++)
{
printf("Enter the Owner's Name\n");
//gets(c[i].oname);
scanf("%s",&c[i].oname[20]);
printf("Enter the chassis number\n");
scanf("%d",&(c[i].chno));
printf("Enter the model year\n");
scanf("%d",&(c[i].model));
printf("Enter the brand name\n");
//gets(c[i].brand);
scanf("%s",&c[i].brand[20]);
}
for (i=0;i<slot;i++)
{
printf("%s %d %d %s\n",c[i].oname[20], c[i].chno,c[i].model,c[i].brand[20]);
}
getche();
}
|
|
|
|
|
Razanust wrote: a c[slot];
This will not work, you cannot allocate an array with an unknown size. I also don't know how you got this to work in the first place is it does not comile (as is to be expected).
|
|
|
|
|
As Richard said, the program shouldn't compile at all.
Also you need to put a fflush(stdin) call after each scanf statement so that the remaining characters in the input stream which cannot be read by scanf are removed.
|
|
|
|
|
Hi,
With reference to Mr Richards reply i made some changes which will make ur program to work. The changes are commented. the slot size should be created dynamically as suggested. Hope u can understand the code. I am beginner too.
Good luck
//OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers,
//model, car brand and to store them in structures
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct data{
char oname[20];
int chno;
int model;
char brand[20];
};
const int slot =2; //changed here as pointed out by Mr Richard
void main()
{
int i=0;
typedef struct data a;
printf("Enter the no. of data slots you want to make\n");
//scanf("%d",&slot); //commented here
a c[slot];
for (i=0;i<slot;i++)
{
printf("Enter the Owner's Name\n");
//gets(c[i].oname);
scanf("%s",c[i].oname); //&c[i].oname[20] not necessary
printf("Enter the chassis number\n");
scanf("%d",&(c[i].chno));
printf("Enter the model year\n");
scanf("%d",&(c[i].model));
printf("Enter the brand name\n");
//gets(c[i].brand);
scanf("%s",c[i].brand); //&c[i].brand[20] not necessary
}
for (i=0;i<slot;i++)
{
printf("%s %d %d %s\n",c[i].oname, c[i].chno,c[i].model,c[i].brand);
}
getche();
}
|
|
|
|
|
I want to know the Brush file format, help me !
My email: dsclub@vip.qq.com
Thank You!
Coding, Learning, Smiling...
|
|
|
|
|
Please help me, I want to do some research, and develop a small tool to view abr file!
Coding, Learning, Smiling...
|
|
|
|
|
I'm trying to code a driver that will read from either virtual or physical memory given an address and report the contents.
Obviously I would vrather read from virtual memory, but at this point I still have a lot to learn about the mysterious kernel lol...
This is where im looking: im going to try and copy the contents of an address into a buffer. The goal is one byte
using:
VOID
RtlCopyMemory(
IN VOID UNALIGNED *Destination,
IN CONST VOID UNALIGNED *Source,
IN SIZE_T Length
);
My first problem is how I am going to pass these parameters to my driver from my GUI.
Secondly, I could benifit from some insight on how this will work.
I am thinking that when my driver calls RtlCopyMemory() its going to get a pointer to a byte to copy, but I was planning on passing a virtual address. In that case i have the feeling I should also be passing a process handle, which is obviously not the case. So then is RtlCopyMemory intended to copy from physical memory into a buffer?
Other questions (lol):
What IRQL should I be operating at?
How can I convert a system::string^ into a LPCVOID, (system::string^ is from taking the text from a windows forms textbox)
I'm totally willing to write my own algorithm, I just dont know anything about the fundamental structure of system::string^. I dont even know what that ^ is lol...
|
|
|
|
|
You can pass in a buffer from user mode to kernel mode by defining custom IO control codes with different transfer types.
Read about it here - Defining Custom I/O Control Codes[^]
|
|
|
|
|
Hello,
Could anyone tell me how to access the CPU register in C?
Thanks
|
|
|
|
|
In Windows and x86, you can use the __asm keyword to write inline assembly and then access the registers by its name (eax, ebx etc.).
__asm is not supported in x64.
|
|
|
|
|
Thanks, Superman. And I wonder whether the keyword "register" is possible to be used to specify a register in the processor? 
|
|
|
|
|
No. The register keyword is used as a hint to the compiler to store the variable into a general purpose register instead of a memory location.
|
|
|
|
|
There's no guarantee that the variable will be put into a register at all, so leave alone a "specific" register.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Hi guys!!
I have to change the colors of all windows in my MFC program for simulate a "nigth color/vision" effects, there's a fast way to do it?
For example change some register keys and restart the program.
Marco
|
|
|
|
|
Colours in windows are either set in the class (default background) or painted in the OnPaint() event, so your code needs to handle it. I don't think there is anything in the Registry that could help.
|
|
|
|
|
|
|
Hi,
I made one combobox which i canwrite some text, in C# and i build that as DLL.
And i call this to VC++.
My problem, when it is in C#,i can write text in combobox.
But in VC++,i cannot write.
When i press alphapets in keyboard nothing is displayed in combobox. only beep sound is coming.
Whats the mistake?
Anu
|
|
|
|
|