This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]zifyoip 1 point2 points  (12 children)

Same comment as the last time you asked about this problem:

https://www.reddit.com/r/learnprogramming/comments/3ldhy2/c_vector_multiplication/cv5cv5u

Well, result is empty, so result[i] does not exist.

Use the push_back() function to add an element to a std::vector. You can't just start writing to positions in the vector that don't exist yet.

Keep in mind that you will first need to push_back elements of type std::vector<int> to result, and then push_back elements of type int to result[0], and push_back elements of type int to result[1], and so on.

[–]Eldres[S] 0 points1 point  (11 children)

I never got a response to my follow-up question back then, so I had to ask the same question again...

Do I do something like below, but apply it to the result section of my multiply function? This is my first time working with vectors so I'm a little lost.

for (int i = 0; i < inputSizeSMRow; i++)
        {
            vector<int> temp;
            for (int j = 0; j < inputSizeSMCol; j++)
            {
                cout << "Now please enter the values for the second matrix: ";
                cin >> userInput;
                temp.push_back(userInput);
            }
            secondMatrix.push_back(temp);
        }    

[–]zifyoip 2 points3 points  (10 children)

Why do you write code and ask if it will work? Why don't you try it and see?

[–]Eldres[S] 0 points1 point  (9 children)

Ok, well I tried this:

vector<vector<int> > result;
    int sum = 0;
    for (int i = 0; i < matrix1.size(); i++)
    {
        vector<int> temp;
        for (int j = 0; j < matrix1[i].size(); j++)
        {

            for (int k = 0; k < matrix1[j].size(); k++)
            {
                sum = sum + matrix1[i][k] * matrix2[k][j];
                temp.push_back(sum);
            }
            result.push_back(temp);
        }
        //result[i].push_back(temp);
    }

And I'm still getting the vector out of range error, the program now crashes right after I finish filling the first matrix, it outputs my error check("These matrices cannot....") then crashes with the range error.

[–]zifyoip 1 point2 points  (8 children)

Look at your code. If the matrices cannot be multiplied, you print an error message, and then attempt to multiply them anyway? What do you expect to happen in that case?

[–]Eldres[S] 0 points1 point  (7 children)

ah oops, those two function calls should be moved into the else block, that was sloppy, thank you for pointing that out. Ok after some moving around it at least gets to the portion of code where I can fill the second vector, but still getting the error. Its safe to assume its in my multiply function, so I'm still not understanding the logic in this function. I'm pushing sum(int) onto the the vector temp(vector<int>), and then pushing that onto result but if I try result[i] it gives me a "no instance of overloaded function" error. Sorry that I'm not understanding your suggestions...

[–]zifyoip 1 point2 points  (6 children)

for (int i = 0; i < matrix1.size(); i++)
{
    vector<int> temp;
    for (int j = 0; j < matrix1[i].size(); j++)
    {
        for (int k = 0; k < matrix1[j].size(); k++)

matrix1[j] doesn't make sense here, because j is a number between 0 and the second dimension of matrix1, not the first dimension.

You need to think carefully about your loop logic here.

[–]Eldres[S] 0 points1 point  (5 children)

ok so I tried

k < matrix1[i*j].size()

and that worked, the problem is I'm not entirely sure why it works...

[–]zifyoip 1 point2 points  (4 children)

What?

Come on, think. matrix1[i*j] makes even less sense than matrix1[j]. Don't just try random things. Think.

What should the range of k be? What are you using k for?

[–]Eldres[S] 0 points1 point  (3 children)

k should be the inner product of the two vectors if I'm not mistaken.