By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

Help with a Matrix Multiplication program

P: 1
Ok, I have an assignment to write a program that multiplies two matrices. The matrices dimensions will be user defined as well as the numbers (floating point values) inside of them. The program must check to see if the two matrices are able to be multiplied.


This is the code that I have so far:

#include "stdafx.h"
#include <iostream>

using namespace std;

void main()
{
int row1, row2, col1, col2 = 0;

cout << "This program will multiply two matrices that you create. Enter in the number of rows in the first matrix: ";
cin >> row1;
cout <<"Now please enter in the number of columns: ";
cin >> col1;
cout << "Please enter the number of rows in the second matrix: ";
cin >> row2;
cout << "Please enter in the number of columns: ";
cin >> col2;

if(col1 != row2)
{
cout << "in order to multiply two matrices the number of columns in the first matrix must match the number of rows in the second << endl << Enter in the number of rows in the first matrix: ";
cin >> row1;
cout <<"Now please enter in the number of columns: ";
cin >> col1;
cout << "Please enter the number of rows in the second matrix: ";
cin >> row2;
cout << "Please enter in the number of columns: ";
cin >> col2;
}


}

Now where I get confused is creating the arrays for the two matrices. The professor says that they must be dynamically allocated and they must be 1-dimensional arrays. This is his advice on how to do that:
[row][col] -> [row*rowSize + col]
I don't quite understand what exactly that statement means, any explanation on that would be appreciated.
Dec 10 '07 #1
Share this Question
Share on Google+
1 Reply


mschenkelberg
P: 44
It means make a one-D array row*col in size and when you want to access an item in 2-D format, instead of having a 2D array and using arr[row][col] you would use your 1d array and access it by doing arr[ NUMROWS*ROWyouWANTtoACCESS + COLyouWANTtoACCESS].

Max
Dec 10 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.