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,938 questions

51,875 answers

573 users

How to calculate the normal and trace of square matrix in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

#define SIZE 5
 
double CalculateNormal(int matrix[][SIZE]) {
    int normal = 0;
     
    for (int i = 0; i < SIZE; i++)
        for (int j = 0; j < SIZE; j++)
            normal += matrix[i][j] * matrix[i][j];
     
    return sqrt(normal);
}
 
int CalculateTrace(int matrix[][SIZE]) {
    int trace = 0;
 
    for (int i = 0; i < SIZE; i++)
        trace += matrix[i][i];
     
    return trace;
}
 
int main(void)
{
    int matrix[][SIZE] = { {1, 1, 1, 1, 1},
                           {2, 2, 2, 2, 2},
                           {3, 3, 3, 3, 3},
                           {4, 4, 4, 4, 4},
                           {5, 5, 5, 5, 5} };
     
    std::cout << CalculateTrace(matrix) << "\n";
    
    std::cout << CalculateNormal(matrix) << "\n";
}
 
 
 
 
/*
run:
 
15
16.5831
 
*/

 



answered Feb 27, 2023 by avibootz
edited Feb 27, 2023 by avibootz

Related questions

1 answer 82 views
1 answer 102 views
1 answer 145 views
1 answer 107 views
1 answer 110 views
...