Returns the sum and product of two symmetric matrices.

Source: Internet
Author: User

/* Exp5-3.cpp */
# Include <stdio. h>
# Define N 4
# Define M 10
Int value (int A [], int I, Int J)/* returns the value of a [I] [J] In Compressed Storage */
{
If (I> = J)
Return a [I * (I-1)/2 + J];
Else
Return a [J * (J-1)/2 + I];
}
Void MADD (int A [], int B [], int C [N] [N])/* compress the sum of A and B */
{
Int I, J;
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
C [I] [J] = value (A, I, j) + value (B, I, j );
}
Void mult (int A [], int B [], int C [N] [N])/* calculate the product of Compressed Storage A and B */
{
Int I, J, K, S;
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
{
S = 0;
For (k = 0; k <n; k ++)
S = S + value (A, I, K) * value (B, K, J );
C [I] [J] = s;
}
}
Void disp1 (int A [])/* output Compressed Storage */
{
Int I, J;
For (I = 0; I <n; I ++)
For (j = 0; j <n; j ++)
Printf ("% 4D", value (A, I, j ));
Printf ("\ n ");
}
Void disp2 (int c [N] [N])/* output symmetric matrix C */
{
Int I, J;
For (I = 0; I <n; I ++)
{
For (j = 0; j <n; j ++)
Printf ("% 4D", C [I] [J]);
Printf ("\ n ");
}
}
Void main ()
{
Int A [m] = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Int B [m] = {1, 1, 1, 1, 1, 1, 1 };
Int c1 [N] [N], C2 [N] [N];
Madd (a, B, C1 );
Mult (a, B, C2 );
Printf ("\ n ");
Printf ("A matrix:"); disp1 ();
Printf ("B Matrix:"); disp1 (B );
Printf ("A + B: \ n"); disp2 (C1 );
Printf ("a * B: \ n"); disp2 (C2 );
Printf ("\ n ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.