简体   繁体   中英

addition in matrix in c++

following is the code

for (int w=0;w<10;w++)
        {
              for( int y=0;y<8;y++)
            {
                    matrix[y][0] = arr_v1[y];
                    matrix[y][1] = arr_v2[y];
                    matrix[y][2] = arr_v3[y];
                    matrix[y][3] = arr_v4[y];
                    matrix[y][4] = arr_v5[y];
                    matrix[y][5] = arr_v6[y];
                    matrix[y][6] = arr_v7[y];
                    matrix[y][7] = arr_v8[y];
            }
            }

i want to add the values to matrix every time, for loop, for (int w=0;w<10;w++) runs. like, when w=0, it will first put values in the matrix, next time when w=1 runs, it should add values to the same matrix and so on. I am not sure, but probably something like:

int add_val=0;
        for(int c=0;c<8;c++)
        {
            for(int d=0;d<8;d++)
            {
                add_val+=matrix[c][d];
            cout<<matrix[c][d]<<" "; 
            }
            cout<<"\n";
        }

Init your matrix values with zeros when creating or before the loop. Then just add values in your loop

You can initialize all of your matrix cells to zero, then you can write something like -

for(int w=0; w<10; w++)
........
    matrix[index1][index2] += your_value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM