3.3.3.1对称矩阵

// DataStructTest.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

void ShowArray2(int * p,int row,int col)
{
    
int index=0;
    
for(int i=0;i<row;i++)
    
{
        
for(int j=0;j<col;j++)
        
{
            
if (j==0)
                cout
<<p[index++];
            
else
                cout
<<" "<<p[index++];
        }

        cout
<<endl;
    }

    cout
<<endl;
}


void ShowArray1(int * p,int count)
{
    
for(int k=0;k<count;k++)
    
{
        
if (k==0)
            cout
<<p[k];
        
else
            cout
<<" "<<p[k];
    }

    cout
<<endl;
}


int main(int argc, char* argv[])
{
    
int Array2[4][4]=
    
{
        
{1,2,4,7},
        
{2,3,5,8},
        
{4,5,6,9},
        
{7,8,9,10}
    }
;

    
int Array1[4*(1+4)/2];
    
//显示二维数组
    ShowArray2(&Array2[0][0],4,4);
    
//将二维数组(对称矩阵)压缩存储到一维数组中
    int index=0;
    
for(int i=0;i<4;i++)
    
{
        
for(int j=0;j<=i;j++)
        
{
            Array1[index
++]=Array2[i][j];
        }

    }

    
//显示一维数组的内容
    ShowArray1(Array1,sizeof(Array1)/sizeof(int));
    cout
<<endl<<endl;

    
int tempArray2[4][4];
    
//通过一维数组得到二维数组(对称矩阵)
    for(int s=1;s<=4;s++)
    
{
        
for(int d=1;d<=4;d++)
        
{
            
int k=0;
            
if (s>=d)
            
{
                k
=s*(s-1)/2+d;
            }

            
else
            
{
                k
=d*(d-1)/2+s;
            }

            tempArray2[s
-1][d-1]=Array1[k-1];
        }

    }

    ShowArray2(
&tempArray2[0][0],4,4);
    
return 0;
}

posted @ 2007-06-30 15:28  吴东雷  阅读(307)  评论(0编辑  收藏  举报