标题:请帮我看看这个错误是为什么呢
只看楼主
lindayanglong
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2008-3-9
 问题点数:0 回复次数:0 
请帮我看看这个错误是为什么呢
我想将对角矩阵[8 0 0 0;
               0 5 0 0;
               0 0 9 0;
               0 0 0 4]
按列重排,按对角线上的数字从大到小排列,也就是要得到矩阵:
[0  8  0  0
 0  0  5  0
 9  0  0  0
 0  0  0  4]
这么写的程序:
#include <stdio.h>

void sort(double *p,int n)
{
    int i,j;
    double temp;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
            if(*(p+j)<*(p+j+1))
            {temp=*(p+j);
             *(p+j)=*(p+j+1);
             *(p+j+1)=temp;}
}

main()
{   int i,j,threevalue[3],temp;
    double linevalue[4],L[4][4]={{8,0,0,0},{0,5,0,0},{0,0,9,0},{0,0,0,4}};
    double threevector[4][3];

           printf("%f,%f,%f,%f\n",L[0][0],L[0][1],L[0][2],L[0][3]);
           printf("%f,%f,%f,%f\n",L[1][0],L[1][1],L[1][2],L[1][3]);
           printf("%f,%f,%f,%f\n",L[2][0],L[2][1],L[2][2],L[2][3]);
           printf("%f,%f,%f,%f\n",L[3][0],L[3][1],L[3][2],L[3][3]);
        printf("******\n");
for(i=0;i<4;i++)
{ linevalue[i]=L[i][i];
}
printf("%f,%f,%f,%f\n",linevalue[0],linevalue[1],linevalue[2],linevalue[3]);
sort(linevalue,4);
for(i=0;i<4;i++)
{if (linevalue[0]==L[i][i])
    threevalue[0]=i;

 else
     if (linevalue[1]==L[i][i])
       threevalue[1]=i;
     else
         if (linevalue[2]==L[i][i])
            threevalue[2]=i;
        else
         continue;
     }
printf("******\n");
printf("%f,%f,%f\n",linevalue[0],linevalue[1],linevalue[2]);
printf("%d,%d,%d\n",threevalue[0],threevalue[1],threevalue[2]);

for(j=0;j<3;j++)
{  temp=threevalue[j];
    for(i=0;i<4;i++)

     {
       threevector[i][j]=L[i][temp];}
}
       printf("******\n");
        printf("%f,%f,%f,%f,\n",threevector[0][0],threevector[0][1],threevector[0][2]);
           printf("%f,%f,%f,%f,\n",threevector[1][0],threevector[1][1],threevector[1][2]);
           printf("%f,%f,%f,%f,\n",threevector[2][0],threevector[2][1],threevector[2][2]);
           printf("%f,%f,%f,%f,\n",threevector[3][0],threevector[3][1],threevector[3][2]);

}
编译没问题,结果有出现,但也出现了不想得到的数字,运行一下就能看到。本人初学C,请大家指点一下,看是什么问题,先谢了!
搜索更多相关主题的帖子: temp double int 矩阵 数字 
2008-03-26 21:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-205582-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.598690 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved