Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7197149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:54:59+00:00 2026-05-28T20:54:59+00:00

I was wondering how to go about this. I have a matrix object that

  • 0

I was wondering how to go about this. I have a matrix object that is a 2d array that grabs a matrix. I am trying to build an add method to add my matrices together.I am a second semester student and have more methods but i am stuck on how to go about this. we have done up to overloading operators, dynamic memory and pointers. I just need some guidance to help me learn this stuff effectively instead of trying for 12 hours. Any help would be grateful.

my class function is

Matrix Matrix:: add(const Matrix & m) const
{
    Matrix v; // I know to make a matrix to add

    // I tried this but seems to not work
    v=v[rows][cols]+ m[rows][cols];
    return v
}

I think I have figured it out now. Thanks! But i will still vote best answer so have at it! It might be useful for other students.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:55:00+00:00Added an answer on May 28, 2026 at 8:55 pm

    I’m going to take a stab and assume your matrix class looks something like this:

    class Matrix
    {
        public:
            int width;
            int height;
            int data[width][height];
    }
    

    If you want to add two Matrixes then you need some sort of function that takes two Matrixes and produces a third matrix such that each value in data is the addition of the two corresponding values in the original matrixes. One such function might look like this:

    Matrix add(const Matrix& lhs, const Matrix& rhs)
    {
        Matrix output;
        for(int row = 0; row < lhs.height; ++row)
        {
            for(int column = 0; column < lhs.widtht; ++column)
            {
                output.data[row][column] = lhs.data[row][column] + rhs.data[row][column];
            }
        }
        return output;
    }
    

    We’re going to assume that the matrixes are the same size and ignore the problems different sized matrixes cause. So all we do is loop through both matrixes and add the values to a new matrix. Now we can call code like:

    Matrix a; // Pretend this already has data
    Matrix b; // Ditto
    Matrix c = add(a, b);
    

    You can see where this is going, all that’s left is to make it an operator, generally when you type

    a + b
    

    What the compiler actually see’s is

    operator+(a, b)
    

    So all that’s left is to rename the add function to

    Matrix operator+(const Matrix& lhs, const Matrix& rhs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering how to go about testing this. I have a method that takes
I'm wondering about this: I have a simple facebook-connect app that will only show
Just wondering about this... I have several separate javascript files, that all contain code
Just wondering about some practice about this; I have made a simple visual C#
I am wondering about the performance of this index: I have an Invalid varchar(1)
I have been wondering about this for a while. What if Magento has written
I've been wondering about this one. Most books I've read shows that when you
I just learnt the basics of MySQL and I'm wondering about this: I have
I have been wondering about this, which is why I have put off learning
Have been wondering about this for days now: I have a basic wxpython program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.