Help with a Matrix Multiplication program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dcatunlucky
    New Member
    • Dec 2007
    • 1

    #1

    Help with a Matrix Multiplication program

    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.
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    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*ROWyouW ANTtoACCESS + COLyouWANTtoACC ESS].

    Max

    Comment

    Working...