/* 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 ");
}