0

I am trying to implement a multidimensional array. Below, you will see for one example in jArray[2][2] that I assign 20.0, clearly. However, both printf statements don't yield the same result. Thanks for your help!

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

#define M_PI 3.14159265358979323846

int main(){ 

    float x1 = 0.1;
    float x2 = 0.1;
    float x3 = -0.1;

    float jArray [3][3] = {
        {3.0, x3*sin(x2*x3), x2*sin(x2*x3)}, 
        {2*x1, -162*(x2+0.1), cos(x3)}, 
        {-x2*exp(-x1*x2), -x1*exp(x1*x2), 20.0}
    };

    float matrix0 [3][3] = {
        {jArray[0][0], jArray[0][1], jArray[0][2]},
        {jArray[1][0], jArray[1][1], jArray[1][2]},
        {jArray[2][0], jArray[2][1], jArray[2][2]},     
    };      

    printf("%f\n\n", jArray[2][2]);

    printf("[%f\t%f\t%f]\n[%f\t%f\t%f]\n[%f\t%f\t%f]\n\n",
        matrix0[0][0], matrix0[0][1], matrix0[0,2],
        matrix0[1][0], matrix0[1][1], matrix0[1,2],
        matrix0[2][0], matrix0[2][1], matrix0[2,2]);    

    return 1;

}

Output:

20.000000

[3.000000   0.001000    0.200000]
[-32.400002 -0.099005   -0.101005]
[0.000000   0.010000    0.000000]
2
  • 4
    matrix0[0,2]? If your compiler isn't giving you warnings, then you should enable more warnings. Commented Mar 17, 2015 at 6:37
  • @JoachimPileborg Wow ... just, wow. I need to sleep. Thanks. Commented Mar 17, 2015 at 6:39

1 Answer 1

3

Replace

    printf("[%f\t%f\t%f]\n[%f\t%f\t%f]\n[%f\t%f\t%f]\n\n",
    matrix0[0][0], matrix0[0][1], matrix0[0,2],
    matrix0[1][0], matrix0[1][1], matrix0[1,2],
    matrix0[2][0], matrix0[2][1], matrix0[2,2]); 

with

    printf("[%f\t%f\t%f]\n[%f\t%f\t%f]\n[%f\t%f\t%f]\n\n",
    matrix0[0][0], matrix0[0][1], matrix0[0][2],
    matrix0[1][0], matrix0[1][1], matrix0[1][2],
    matrix0[2][0], matrix0[2][1], matrix0[2][2]); 

Your compiler would've emitted a warning for that because %f expects a float(or a double), not a float*.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.