Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

C++: How to use a 'for' loop to multiply 2D arrays?

I'm writing a program to populate two multidimensional arrays and multiply their elements using for loops only. The code's pretty simple yet I'm stuck with a host of errors from the multiplication loop. I can't wrap my head around this one.

the log

Is there a problem with my loop syntax? I'll attach the code below. Lines 48 and 49 are the loop code for multiplication. Thanks in advance.

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

int main()
{

cout << "Creating array: " << endl;

cout << " " << endl;

cout << "Please enter the number of rows: "; 

int numRows2 = 0;
cin >> numRows2;

cout << " " << endl;

cout << "Please enter the number of columns: ";
int numCols2 = 0;
cin >> numCols2;

double myNums2[numRows2][numCols2];

for (int counter = 0; counter < numRows2; counter++)
    {
        for (int counter_a = 0; counter_a < numCols2; counter_a++)
           {
            cout << "Enter Element [" << counter << "][" << counter_a << "]: ";
            cin >> myNums2[counter][counter_a];
            }
    }

cout << " " << endl;

cout << "Displaying contents of the array: " << endl;

for (int counter = 0; counter < numRows2; ++counter)
    {
        for (int counter_a = 0; counter_a < numCols2; ++counter_a)

            cout << "Element [" << counter <<"][" << counter_a << "]: " << myNums2[counter][counter_a] << endl;
    }


cout << " " << endl;

cout << "Multiplying each element in myNums1 by each element in myNums2: " << endl;

for (int indexRow1 = 0, int indexCol1 = 0; indexRow1 < numRows, indexCol1 < numCols; ++indexRow1, ++indexCol1)
    for (int indexRow2 = 0, int indexCol2; indexRow2 < numRows2, indexCol2 < numCols2; ++indexRow2, ++indexCol2)
        {
            cout << myNums1[indexRow1][indexRow2] << " x " << myNums2[indexRow2][indexCol2] << " = " << myNums1[indexRow1][indexCol1] * myNums2[indexRow2][indexCol2] << endl;
        }

return 0;
}
question from:https://stackoverflow.com/questions/65949104/c-how-to-use-a-for-loop-to-multiply-2d-arrays

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can not declare two variables in the for loop as you are doing it. You can check this thread for ways to achieve it.

On the snippet you share, certain variables are not declared, numRows, numCols, myNums1.

indexCols2 is not initialized, even though the for loop is not correct as it is.

As @cigien mentioned in the comment, to use an array you will have to allocate memory dynamically if you want to read the size from the user.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...