简体   繁体   中英

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

And I have to find the shortest distance between two points of the circle like II; I-II; 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:

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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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