简体   繁体   English

如何在 C 中获得矩阵对角线的一侧

[英]How do I get one side of a diagonal of matrix in C

I have to input array length and its elements as shown down in the code.我必须输入数组长度及其元素,如代码所示。 Array length represents the number of cities and its elements show the distance between them like shown in the picture down below.数组长度表示城市的数量,其元素显示它们之间的距离,如下图所示。 在此处输入图像描述

So the input should look like this:5 and 2 3 4 1 1所以输入应该是这样的:5 and 2 3 4 1 1

And I have to find the shortest distance between two points of the circle like II;我必须像 II 一样找到圆的两点之间的最短距离; I-II;一-二; I-III... II-I; I-III... II-I; II-II... till I fill a matrix which size is array size x array size(in this case 5x5) and that output needs to look like this: II-II...直到我填充一个大小为数组大小 x 数组大小(在本例中为 5x5)的矩阵,并且 output 需要如下所示:

0 2 5 2 1
2 0 3 4 3
5 3 0 4 5
2 4 4 0 1
1 3 5 1 0

This is my code:这是我的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{

    int* ptr;
    int l,n, i, m, x,y,s,j,t;
    int **p;

    scanf("%d", &l);

    ptr = (int*)malloc(l * sizeof(int));

    if (ptr == NULL) {
        exit(0);
    }
    else {

        for (i = 0; i < l; ++i)
        {
            scanf("%d", &ptr[i]);
            if(ptr[i]<0)
            {
                exit(0);
            }
        }

        for (i = 0; i < l; ++i) {
            printf("%d ", ptr[i]);
        }
    }

    n=l;
    m=l;

    while(m>0 && n>0){
        p= malloc(m*sizeof(int*));
        for (i=0;i<m;i++){
            p[i]=malloc(n*sizeof(int));
            for(j=0;j<n;j++){
                x=0;
                y=0;
                for (t = 0; t < j; t++) {
                    x  += ptr[t];
                }
                for (t = n; t >j; t--) {
                    y += ptr[t];
                }
                if (x > y) {
                    s = y;
                } else {
                    s = x;
                }
                p[i][j]=s;
            }
        }
    }
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            printf("%d", p[i][j]);
        }
        putchar('\n');
    }
    return 0;
}

I think the code for finding distance is good but I can't say that for sure because I don't know how to put it into a matrix.我认为查找距离的代码很好,但我不能肯定地说,因为我不知道如何将它放入矩阵中。 I was also thinking to fill the diagonal of a matrix with zeros and fill the bottom left with numbers and "mirror" it since it is the same, but I'm not quite sure how to do that.我也在考虑用零填充矩阵的对角线并用数字填充左下角并“镜像”它,因为它是相同的,但我不太确定该怎么做。 So if someone can please give me suggestions or fix the code.因此,如果有人可以给我建议或修复代码。 Thank you in advance.先感谢您。

Well, for starters, I think your program will just run an endless loop since you have the line好吧,对于初学者来说,我认为你的程序只会运行一个无限循环,因为你有这条线

 while(m>0 && n>0)

but "m" and "n" do not change inside the loop.但是“m”和“n”在循环内不会改变。 I think this works, at least with your example so take a look:我认为这有效,至少在你的例子中,所以看看:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int* ptr;
    int l,n, i, m, x,y,s,j,t;
    int **p;
    scanf("%d", &l);
    ptr = (int*)malloc(l * sizeof(int));
    if (ptr == NULL) {
        exit(0);
    }
    else {
        for (i = 0; i < l; ++i)
        {
            scanf("%d", &ptr[i]);
            if(ptr[i]<0)
            {
                exit(0);
            }
        }

        for (i = 0; i < l; ++i) {
            printf("%d ", ptr[i]);
        }
        printf("\n");
    }
 
    //Everything up to here is the same, now we initialize the "result" matrix
    p = (int**) malloc (l * sizeof(int*));
    for(i = 0; i < l; i++){
        p[i] = (int*) malloc(l * sizeof(int));
        p[i][i] = 0;//this will just fill the diagonal with 0's
    }
    
    for(i = 0; i < l; i++){//this is the starting point
        for(j = i+1; j < l; j++){//this is the end point. Note we are starting at i+1 because we only need to fill one since the other is just mirrored
        int l1 = 0, l2 = 0; //2 possible lengths, one going forward for index i -> i+1 ->...-> j and one going backwards i -> i-1 -> i-2 ->...->j
            //going forwards
            for(m = i; m <j; m++)
                l1 += ptr[m];
            //going backwards
            n = i-1;
            if(n == -1)//if we are out of bounds->go back
                    n = l-1;
            while(n != j){
                l2 += ptr[n];
                n--;
                if(n == -1)//if we are out of bounds->go back
                    n = l-1;
            }
            l2 += ptr[j];
            //we compare to find the minimum
            if(l1 < l2)
                p[j][i] = l1;
            else
                p[j][i] = l2;
        }
    }
    
    //we now mirror the results for the other half
    
    for(i = 0; i < l; i++)
        for(j = i+1; j < l; j++)
            p[i][j] = p[j][i];
            
    printf("RESULTS: \n");
    //and we print
    for(i = 0; i < l; i++){
        for(j = 0; j < l; j++){
           printf("%d ", p[i][j]);
        }
        printf("\n");
    }
   
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM