Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,065 people online right now. Registration is fast and FREE... Join Now!




Matrix Addition

 
Reply to this topicStart new topic

Matrix Addition

Kuhniva
11 Apr, 2007 - 02:20 PM
Post #1

New D.I.C Head
*

Joined: 26 Mar, 2007
Posts: 1


My Contributions
I'm trying to add to matrices together

CODE


#include <iostream>
#include <iomanip>
using namespace std;

//////////////////////////////////////////////////////////////////

const int SIZE = 10;

double Sum(double matrixA[SIZE], double matrixB[SIZE])
{
    int i = 0;
    int j = 0;
    double rows, columns;
    double rows2, columns2;
    double matrixC[SIZE][SIZE];

if(rows == columns2 && rows2 == columns)
        {
            cout << "The sum is: ";
            
            for(i = 0; i < rows2; i++)
            {
                for(j = 0; j < columns2; j++)
                {
                    {matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
                    
                    if(j < columns)
                        cout << matrixC[i][j] << " ";
                    else
                    {
                        cout << "\n" << matrixC[i][j];
                        j = 0;
                    }
                }
                }
            }
        }
        else
            cout << "\nSum: Can't perform this operation because the dimensions of the matrices are not the same\n";

    return(matrixC[SIZE][SIZE]);
}
//////////////////////////////////////////////////////////////////

int main()
{
    const int SIZE = 10;
    double matrixA[SIZE][SIZE];
    double matrixB[SIZE][SIZE];

    double rows, columns;
    double rows2, columns2;

        cout << "Please enter the dimensions of the first matrix:  \n";
        cin >> rows >> columns;

        cout << "Please enter the values for the first matrix:    \n";
        for(int rowValue = 0; rowValue < rows; rowValue++)                                            
            for(int columnValue =0; columnValue < columns; columnValue++)
                cin >> matrixA[rowValue][columnValue];


        cout << "Please enter the dimensions of the second matrix:  \n";
        cin >> rows2 >> columns2;

        cout << "Please enter the values for the second matrix:     \n";
        for(int rowValue2 = 0; rowValue2 < rows2; rowValue2++)
            for(int columnValue2 = 0; columnValue2 < columns2; columnValue2++)
                cin >> matrixB[rowValue2][columnValue2];

    double matrixAddition = Sum(matrixA[SIZE][SIZE], matrixB[SIZE][SIZE]);

        cout << matrixAddition;
        
    return(0);
}



I'm really close just getting a few errors, any help would be appreciated
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Matrix Addition
11 Apr, 2007 - 02:50 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
QUOTE
#include <iostream>
#include <iomanip>
using namespace std;

//////////////////////////////////////////////////////////////////

const int SIZE = 10;

double Sum(double matrixA[SIZE], double matrixB[SIZE])
{
int i = 0;
int j = 0;
double rows, columns;
double rows2, columns2;
double matrixC[SIZE][SIZE];


CODE
double Sum(double matrixA[SIZE][SIZE], double matrixB[SIZE][SIZE])



And think about the output
ok here's the code but I get one error that I can't resolve
CODE

#include <iostream>
#include <iomanip>
using namespace std;
#define SIZE 10
//////////////////////////////////////////////////////////////////



double Sum(double matrixA[SIZE][SIZE], double matrixB[SIZE][SIZE])
{
    int i = 0;
    int j = 0;
    double rows, columns;
    double rows2, columns2;
    double matrixC[SIZE][SIZE];

if(rows == columns2 && rows2 == columns)
        {
            cout << "The sum is: ";
            
            for(i = 0; i < rows2; i++)
            {
                for(j = 0; j < columns2; j++)
                {
                    matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
                    
                    if(j < columns) cout << matrixC[i][j] << " ";
                        
                    else
                    {
                        cout << "\n" << matrixC[i][j];
                        j = 0;
                    }
                }
                
            }
        }
        else
            cout << "\nSum: Can't perform this operation because the dimensions of the matrices are not the same\n";

    return(matrixC[SIZE][SIZE]);
}
//////////////////////////////////////////////////////////////////

int main()
{

    double matrixA[SIZE][SIZE];
    double matrixB[SIZE][SIZE];

    double rows, columns;
    double rows2, columns2;

        cout << "Please enter the dimensions of the first matrix:  \n";
        cin >> rows >> columns;

        cout << "Please enter the values for the first matrix:    \n";
        for(int rowValue = 0; rowValue < rows; rowValue++)                                            
            for(int columnValue =0; columnValue < columns; columnValue++)
                cin >> matrixA[rowValue][columnValue];


        cout << "Please enter the dimensions of the second matrix:  \n";
        cin >> rows2 >> columns2;

        cout << "Please enter the values for the second matrix:     \n";
        for(int rowValue2 = 0; rowValue2 < rows2; rowValue2++)
            for(int columnValue2 = 0; columnValue2 < columns2; columnValue2++)
                cin >> matrixB[rowValue2][columnValue2];


    double matrixZ[SIZE][SIZE]= Sum(matrixA[SIZE][SIZE],matrixB[SIZE][SIZE]);//ERROR HERE
        for(int h=0;h<SIZE;h++)
                for(int u=0;u<SIZE;u++)
                    cout<<matrixZ[h][u];
        
    return(0);
}



and the error is:
QUOTE

error C2664: 'Sum' : cannot convert parameter 1 from 'double' to 'double [][10]'
There is no context in which this conversion is possible


Experts have a go.

User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Matrix Addition
12 Apr, 2007 - 04:50 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
There are a lot of little errors here...

First off double Sum(double matrixA[SIZE], double matrixB[SIZE]) needs to be declared as:
double Sum(double matrixA[][SIZE], double matrixB[][SIZE])
This is because you want the matrices to be multiDimentional arrays, not single dimentional arrays.

Next up
double matrixAddition = Sum(matrixA[SIZE][SIZE], matrixB[SIZE][SIZE]); is passing two doubles (matrixA[10][10] and MatrixB[10][10]) and you MEAN to pass the arrays:
double matrixAddition = Sum(matrixA, matrixB);

Taking from that one:
inside of Sum() you have return(matrixC[SIZE][SIZE]); and this compiles fine, but it is a logic error. It translates to:
return(matrixC[10][10]); but the array is only defined to [9][9] (elements go from 0 to SIZE-1) so even if you DID mean to just return one element of the matrix, that element would be outside of your allocated memory... but what I belive you mean to return is the array itself (i.e. the SUM of the two matrices). The problem here is that C/C++ does not directly allow you to return multidimential arrays. You see matrixC exits in the stack space for the function call to Sum(), when Sum() exits, matrixC is thown away so even if you COULD return a pointer to matrixC (arrays are passed by referance... i.e. pointers) it would not really exits outside of Sum(). So what do we do???

We change the way sum works. There are two options:
Option1: we store the result of the addition is matrixA, this makes sum kinda like going matrixA += matrixB; (in fact overloading the += operator might be nice).
Option2: we pass matrixC as a parameter to sum().
void Sum(double matrixA[][SIZE], double matrixB[][SIZE], double matrixC[][SIZE]); Then all we have to do is remove the delcaration of matrixC from inside of Sum() (i.e. delete the line double matrixC[SIZE][SIZE]; and change the return stament to return; to mark our exit point.

Problem #4
if(rows == columns2 && rows2 == columns)
rows, columns2, rows2, columns <-- none of these are initalized, so the above if statment is capable of doing all kinds of strange things. Basicly you need to either initalize these variables or get rid of that line.

That all being said, Here is an example of another way to handel matrices (this is an example of row operations). In that post I recommend a different stratigy for representing matricies, I think that a class would be a much better solution, but rather than using static multidimentinal arrays, this example uses a structure and a pointer to a dynamic array. This idea might help deal with ensuring that yours rows=collums to do the addition.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month