Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to add 2 matrices in C++

1 Answer

0 votes
#include <iostream>

int main(void) {
    int a[][3] = { {1, 0, 2},  
                   {3, 5, 6},  
                   {7, 4, 1} };  
      
    int b[][3] = { {1, 0, 1},  
                   {2, 2, 1},  
                   {1, 3, 1} };  
    
    static const size_t rows = (sizeof(a) / sizeof(a[0]));  
    static const size_t cols = (sizeof(a) / sizeof(a[0][0])) / rows;  

    int sum[rows][cols];  
      
    for (int i = 0; i < rows; i++) {  
        for(int j = 0; j < cols; j++) {  
            sum[i][j] = a[i][j] + b[i][j];  
        }  
    }  
      
    for (int i = 0; i < rows; i++) {  
        for(int j = 0; j < cols; j++) {  
           std::cout << sum[i][j] << " ";  
        }  
        std::cout << "\n";  
    }  
    
    return 0;
}




/*
run:

2 0 3 
5 7 7 
8 7 2 

*/

 



answered Aug 4, 2021 by avibootz

Related questions

1 answer 91 views
91 views asked Aug 4, 2021 by avibootz
1 answer 95 views
95 views asked Aug 4, 2021 by avibootz
1 answer 159 views
1 answer 111 views
111 views asked Aug 4, 2021 by avibootz
1 answer 160 views
160 views asked May 24, 2017 by avibootz
1 answer 146 views
146 views asked May 23, 2017 by avibootz
1 answer 214 views
...