m1 X m2

multiplies two matrices (M1 × M2) of
integers using pointers and dynamic arrays (you are not allowed to use the vector class). At the command
line, require four integer arguments: (1) M1 rows, (2) M1 columns, (3) M2 rows, (4) M2 columns.
read in the matrices one cell at a time.



I just want a little help on how to code matrices. Here is what im beinging with:
Last edited on
I added this bit about specifying two arrays, but do not know if this is the correct path.
Last edited on
At the command line, require four integer arguments

Four. Proper program call will thus have 5==argc, won't it?

However, that is not enough. The arguments must be integers, obviously all greater than 0, and there was that extra dependency too.

If you are rusty on matrix multiplication, or have not yet encountered it, refer to Wikipedia1, Math is Fun2, or Purplemath3.


So i should ask the user to enter 4 integers in separate lines?

Like this:


1
2
3
4
5
6
7
8
9
10
11
12
cout << "Enter M1 rows:  "<< endl;
cin >> m1r;

cout << "Enter M1 columns:  "<< endl;
cin >> m1c;

cout << "Enter M2 rows:  "<< endl;
cin >> m2r;

cout << "Enter M2 columns:  "<< endl;
cin >> m2c;
require arguments

We were already talking about argc. What are argc and argv? Your previous homework have used them.
Yes okay, but is what I have right before this what it should look like?

argc is the count of total command line arguments.

argv is array of character string

So how is this so far:

1
2
3
4
int main(int argc, char *argv[])
{
	
}
Last edited on
You are supposed to got the array dimensions as command line arguments. You current code ignores the arguments and reads values from std::cin.

Why do you show usage on line 4? You don't know at that point whether the user has given required arguments.

In your current code the lines 6-16 do read dimensions from the user. What does the lines 17-21 achieve?


argv is array of character string

Yes. Each string is one command line argument. The array has argc strings. The first string is the name of the program. The other strings you should convert to integers and store to the variables that you have declared on line 3.


Note on line 20. The most portable range of return values is probably 0--127. The language standard requires int, but some operating systems expect smaller range.
can you show an example, Im very confused.
Thanks
bool s2i( const char *, int & ); // string to int

IF (5==argc && s2i(argv[1], m1r) && s2i(argv[2], m1c) &&
     s2i(argv[3], m2r) && s2i(argv[4], m2c))
  IF (0<m1r && 0<m1c && m1c==m2r && 0<m2c)
    do all the real work
  ELSE
    error: invalid dimensions
ELSE
  error: usage
Okay, I'll try and work out something. Thanks
Last edited on
Thanks for the help, Im closer now.
Last edited on
Topic archived. No new replies allowed.