Thread: Multi-dimensional Array Issues

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    11

    Multi-dimensional Array Issues

    I'm working on an assignment for a C programming class. The goal is to analyze components of a circuit, assemble voltage and resistor values in a matrix, then use Cramer's rule to find current values. The issue I'm running into is that my output values are all coming up 0. I'm not sure if it's a math failure or if my use of arrays is faulty.

    Code:
    #include <stdio.h>
    int main(void)
    {
    
        float r1, r2, r3, r4, r5, e1, e2, e3;
    
        printf("Enter resistor values in kili-ohms and battery values in volts\n");
        printf("r1: ");
        scanf_s("%f", &r1);
        printf("r2: ");
        scanf_s("%f", &r2);
        printf("r3: ");
        scanf_s("%f", &r3);
        printf("r4: ");
        scanf_s("%f", &r4);
        printf("r5: ");
        scanf_s("%f", &r5);
    
        printf("e1: ");
        scanf_s("%f", &e1);
        printf("e2: ");
        scanf_s("%f", &e2);
        printf("e3: ");
        scanf_s("%f", &e3);
    
        float A[3][3] = { { -1, 1, 1 }, { r1 + r2 + r3, 0, r5 }, { 0, r4, -1 * r5 } },
    
            Ax[3][3] = { { 0, A[0][1], A[0][2] }, { e1 + e3, A[1][1], A[1][2] }, { (-1 * e2) + (-1 * e3), A[2][1], A[2][2] } },
    
            Ay[3][3] = { { A[0][0], 0, A[0][2] }, { A[1][0], e1 + e3, A[1][2] }, { A[2][0], (-1 * e2) + (-1 * e3), A[2][2] } },
    
            Az[3][3] = { { A[0][0], A[0][1], 0 }, { A[1][0], A[1][1], e1 + e3 }, { A[2][0], A[2][1], (-1 * e2) + (-1 * e3) } };
    
        float detA = (A[0][0] * (A[1][1] * A[2][2] - A[2][1] * A[1][2])) - (A[0][1] * (A[1][0] * A[2][2] - A[2][0] * A[1][2])) + (A[0][2] * (A[1][0] * A[2][1] - A[2][0] * A[1][1])),
    
            detAx = (Ax[0][0] * (Ax[1][1] * Ax[2][2] - Ax[2][1] * Ax[1][2])) - (Ax[0][1] * (Ax[1][0] * Ax[2][2] - Ax[2][0] * Ax[1][2])) + (Ax[0][2] * (Ax[1][0] * Ax[2][1] - Ax[2][0] * Ax[1][1])),
    
            detAy = (Ay[0][0] * (Ay[1][1] * Ay[2][2] - Ay[2][1] * Ay[1][2])) - (Ay[0][1] * (Ay[1][0] * Ay[2][2] - Ay[2][0] * Ay[1][2])) + (Ay[0][2] * (Ay[1][0] * Ay[2][1] - Ay[2][0] * Ay[1][1])),
    
            detAz = (Az[0][0] * (Az[1][1] * Az[2][2] - Az[2][1] * Az[1][2])) - (Az[0][1] * (Az[1][0] * Az[2][2] - Az[2][0] * Az[1][2])) + (Az[0][2] * (Az[1][0] * Az[2][1] - Az[2][0] * Az[1][1]));
        float i1 = detAx / detA, i2 = detAy / detA, i3 = detAz / detA;
    
        printf("i1, i2 and i3 are %f, %f and %f", &i1, &i2, &i3);
        
        
        getch();
        return(0);
    }
    A classmate who used to code also mentioned that for loops might be an alternative option for populating arrays. I'm not sure if that applies here but extra tips are always helpful.

    Edit: I found my obvious mistake in the final printf function. Removed the "&'s". All that's left is to apply some multiplication to make sure the prefixes of my units are taken into account. I'd still take any input to make sure the current values seem right.
    Last edited by rklein; 02-23-2017 at 12:33 PM. Reason: Found an obvious mistake

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You can make your program easier to read, which may help in finding any errors.
    Code:
    #include <stdio.h>
    
    float getf(const char *prompt) {
        float f;
        printf("%s: ", prompt);
        scanf_s("%f", &f);
        return f;
    }
    
    int main(void) {
        float r1, r2, r3, r4, r5, e1, e2, e3;
     
        printf("Enter resistor values in kili-ohms and battery values in volts\n");
        r1 = getf("r1");
        r2 = getf("r2");
        r3 = getf("r3");
        r4 = getf("r4");
        r5 = getf("r5");
        e1 = getf("e1");
        e2 = getf("e2");
        e3 = getf("e3");
     
        float A[3][3]  = { {           -1,  1,   1 },
                           { r1 + r2 + r3,  0,  r5 },
                           {            0, r4, -r5 } };
    
        float Ax[3][3] = { {         0,   A[0][1],   A[0][2] },
                           {  e1 +  e3,   A[1][1],   A[1][2] },
                           { -e2 + -e3,   A[2][1],   A[2][2] } };
    
        float Ay[3][3] = { {   A[0][0],         0,   A[0][2] },
                           {   A[1][0],  e1 +  e3,   A[1][2] },
                           {   A[2][0], -e2 + -e3,   A[2][2] } };
    
        float Az[3][3] = { {   A[0][0],   A[0][1],         0 },
                           {   A[1][0],   A[1][1],  e1 +  e3 },
                           {   A[2][0],   A[2][1], -e2 + -e3 } };
     
        float detA  = (A[0][0]  * (A[1][1]  * A[2][2]  - A[2][1]  * A[1][2]))
                    - (A[0][1]  * (A[1][0]  * A[2][2]  - A[2][0]  * A[1][2]))
                    + (A[0][2]  * (A[1][0]  * A[2][1]  - A[2][0]  * A[1][1]));
    
        float detAx = (Ax[0][0] * (Ax[1][1] * Ax[2][2] - Ax[2][1] * Ax[1][2]))
                    - (Ax[0][1] * (Ax[1][0] * Ax[2][2] - Ax[2][0] * Ax[1][2]))
                    + (Ax[0][2] * (Ax[1][0] * Ax[2][1] - Ax[2][0] * Ax[1][1]));
    
        float detAy = (Ay[0][0] * (Ay[1][1] * Ay[2][2] - Ay[2][1] * Ay[1][2]))
                    - (Ay[0][1] * (Ay[1][0] * Ay[2][2] - Ay[2][0] * Ay[1][2]))
                    + (Ay[0][2] * (Ay[1][0] * Ay[2][1] - Ay[2][0] * Ay[1][1]));
    
        float detAz = (Az[0][0] * (Az[1][1] * Az[2][2] - Az[2][1] * Az[1][2]))
                    - (Az[0][1] * (Az[1][0] * Az[2][2] - Az[2][0] * Az[1][2]))
                    + (Az[0][2] * (Az[1][0] * Az[2][1] - Az[2][0] * Az[1][1]));
    
        float i1 = detAx / detA;
        float i2 = detAy / detA;
        float i3 = detAz / detA;
     
        printf("i1, i2 and i3 are %f, %f and %f\n", i1, i2, i3);
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    Excellent. I ended up checking the output values via pencil and paper.

    A couple follw up questions:

    1. Could you explain the getf() function and what you did in lines 3-7?

    2. The second part of the problem is to print the different matrices used to find the determinants. I ended up using four nested for loops because I couldn't get multiple to print in one while arranged properly. Anything you'd recommend for this?

    Code:
    printf("A =\n");
        int i, j;
        for (i = 0; i < 3; i++)
        {for (j = 0; j < 3; j++) 
            {printf("%7.2f", A[i][j]);}
            printf("\n");}
    
        printf("\nAx =\n");
        int t, f;
        for (t = 0; t < 3; t++)
        {
            for (f = 0; f < 3; f++)
            {
                printf("%7.2f", Ax[t][f]);
            }
            printf("\n");
        }
        
        printf("\nAy =\n");
        int u, h;
        for (u = 0; u < 3; u++)
        {
            for (h = 0; h < 3; h++)
            {
                printf("%7.2f", Ay[u][h]);
            }
            printf("\n");
        }
    
        printf("\nAz =\n");
        int o, k;
        for (o = 0; o < 3; o++)
        {
            for (k = 0; k < 3; k++)
            {
                printf("%7.2f", Az[o][k]);
            }
            printf("\n");
        }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Maybe something like the following.
    As for the getf function, what don't you understand about it?
    Code:
    #include <stdio.h>
    
    #define SIZE 3
    
    float getf(const char *prompt) {
        float f;
        printf("%s: ", prompt);
        scanf_s("%f", &f);
        return f;
    }
    
    float calc_det(float m[][SIZE]) {
        return m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])
             - m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])
             + m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
    }
    
    void print_matrix(const char *name, float m[][SIZE]) {
        printf("%s =\n", name);
        int i, j;
        for (i = 0; i < SIZE; i++) {
            for (j = 0; j < SIZE; j++) 
                printf("%7.2f ", m[i][j]);
            printf("\n");
        }
    }
    
    int main(void) {
        printf("Enter resistor values in kili-ohms and battery values in volts\n");
    
        float r1 = getf("r1");
        float r2 = getf("r2");
        float r3 = getf("r3");
        float r4 = getf("r4");
        float r5 = getf("r5");
        float e1 = getf("e1");
        float e2 = getf("e2");
        float e3 = getf("e3");
    
        float A[SIZE][SIZE]  = {{           -1,  1,   1 },
                                { r1 + r2 + r3,  0,  r5 },
                                {            0, r4, -r5 }};
    
        float Ax[SIZE][SIZE] = {{         0,   A[0][1],   A[0][2] },
                                {  e1 +  e3,   A[1][1],   A[1][2] },
                                { -e2 + -e3,   A[2][1],   A[2][2] }};
    
        float Ay[SIZE][SIZE] = {{   A[0][0],         0,   A[0][2] },
                                {   A[1][0],  e1 +  e3,   A[1][2] },
                                {   A[2][0], -e2 + -e3,   A[2][2] }};
    
        float Az[SIZE][SIZE] = {{   A[0][0],   A[0][1],         0 },
                                {   A[1][0],   A[1][1],  e1 +  e3 },
                                {   A[2][0],   A[2][1], -e2 + -e3 }};
     
        float detA  = calc_det(A);
        float detAx = calc_det(Ax);
        float detAy = calc_det(Ay);
        float detAz = calc_det(Az);
    
        float i1 = detAx / detA;
        float i2 = detAy / detA;
        float i3 = detAz / detA;
     
        printf("i1, i2 and i3 are %f, %f and %f\n", i1, i2, i3);
    
        print_matrix("A",  A);
        print_matrix("Ax", Ax);
        print_matrix("Ay", Ay);
        print_matrix("Az", Az);
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    I guess my question would be whether it behaves just like scanf() which is what it looks like. Also didn't realize there was a shortcut for calculating the determinant of a matrix in C. The instructor likely neglected to share that information with us on purpose.

    Anyways, thanks again. Insightful posts just as before.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I guess you just aren't used to functions.

    I wouldn't say that getf is just like scanf_s, although it does call the scanf_s function. The point is that you were printf'ing and scanf_s'ing over and over again. That's the kind of thing you can move into a function to avoid the repetition.

    The determinant calculation isn't really a shortcut so much as just another removal of repetition. It takes advantage of the fact that the calculations are exactly the same except that they use a different matrix.

    print_matrix also creates a function out of repetitive code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is Multi Dimensional Array?
    By loserone+_+ in forum C Programming
    Replies: 4
    Last Post: 01-29-2013, 08:29 AM
  2. Multi Dimensional Array
    By johnmackin in forum C Programming
    Replies: 0
    Last Post: 06-13-2012, 06:45 PM
  3. multi-dimensional array
    By shuo in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2008, 01:03 AM
  4. Multi dimensional array
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 05:03 PM
  5. multi-dimensional array
    By mcorn in forum C Programming
    Replies: 2
    Last Post: 08-04-2002, 09:14 AM

Tags for this Thread