Problem Understanding this

This is a discussion on Problem Understanding this within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've been working on a program that I need to do for Numerical Analyses. I use memory allocation ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    Problem Understanding this

    Hi everyone,

    I've been working on a program that I need to do for Numerical Analyses. I use memory allocation with a 2D array.

    I'm not really used to this, but my teacher says that the program should not work because I'm not using the 2D array correctly. He tried to explain to me why it should not work, but it works.

    I've learned to use this on a website, but my teacher wants me to explain how my program works. The problem is that I'm passing the 2D array to a function (I pass the memory location to that function) and it receives just like this:

    void resolve_sistema(int eq, float matriz[][eq+1])

    He says that It should not work like this. For example, when I want to use the 2D array I use it like this: matriz[5][5], but my teacher says it should be done like this: *(*(matriz+5)+5).

    Can someone Help?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Post the relevant code.
    How you allocate 2-D array.
    Pass to function. how it is used in function.

    matrix[5][5] and *(*(matriz+5)+5) are the same.
    Last edited by Bayint Naung; 06-09-2010 at 09:05 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    7
    He says to me that I cannot use matriz[5][5] because that doesn't exist.

    BTW, the allocation:

    Code:
    float **criacao_tdd (int pola, int *c) //tdd[c][c+1] c= numero de pontos
    {
        float **tdd;
        int   n;
        char op;
    
        do{
            system("cls");
            printf("\t\t\tCriacao da Tabela Das Diferencas Divididas \n\nQuantos pontos se utilizara na tabela? \n(Numero de pontos máximos: %d)", pola);
            scanf("%d", c);
            if(*c>1 && *c <= pola)
                do{
                    printf("\nDeseja Alterar o valor introduzido? S/N");
                    op = toupper(getch());
                }while(op != 'S' && op != 'N');
        }while(*c<=1 || *c>pola || op == 'S');
    
        tdd = (float **) calloc (*c, sizeof(float *));
    
    
        for ( n = 0; n < *c; n++ )
        {
            tdd[n] = (float*) calloc (*c+1, sizeof(float));
    
            if (tdd[n] == NULL)
                break;
        }
    
        if(tdd == NULL || tdd[n] == NULL)
        {
            do{
                printf("\nErro: não há memória \nDeseja introduzir um novo valor? (S/N) \n\n");
                op = toupper(getch());
            }while(op != 'S' && op != 'N');
    
            if (op == 'N')
                exit(1);
            else
                tdd=criacao_tdd(pola, c);
        }
    
        return (tdd);
    }

    EDIT: When I call the function that allocates this, I do this: tdd = criacao_tdd(pola, c);


    One function that use it:

    Code:
    int px (int c, float tdd[][c+1], float x)
    {
        int coluna = 1, linha = 0, op_erro, aux = 0;
        float px = 0, aux_calculo;
        char op;
    
        do{
            system("cls");
            printf("Deseja Calcular o Erro? ");
            op = toupper(getch());
    
            if (op == 'S')
                op_erro = 1;
            if (op == 'N')
                op_erro = 0;
    
        }while(op_erro != 1 && op_erro != 0);
    
        for(coluna = 1; coluna < c+1-op_erro; coluna++)
        {
            aux_calculo = tdd[0][coluna];
    
            for(linha = 0; linha < aux; linha++)
                aux_calculo = aux_calculo*(x-tdd[linha][0]);
    
            aux++;
            px = px+aux_calculo;
        }
    
        printf("\n\nX = %f ",px);
        return op_erro;
    }
    Last edited by RA-3000; 06-09-2010 at 09:14 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Where is matriz[5][5]?
    Why not english ?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    7
    Sorry for the portuguese. There is no matriz[5][5]. That was an example. There is tdd[][].

    When I use this:
    aux_calculo = tdd[0][coluna];

    My teacher says it should not work and that I have to use this:
    aux_calculo = *(*(tdd+0)+coluna);

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How do you call px() function.
    especially 2 D passing.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    7
    I call it like this (in the main function):
    op_erro = px(colunas, tdd, x);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    Then you can merilly go tell your teacher that he/she is wrong.
    Both of these syntaxes are valid:
    aux_calculo = tdd[0][coluna];
    aux_calculo = *(*(tdd+0)+coluna);
    The former is better because it's more readable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    He tried to explain to me why it should not work, but it works.
    I wonder how he explain?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    7
    Thanks everyone. He keeps telling me that it should not work and I am wrong -_-

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. facing problem in understanding this....
    By enggabhinandan in forum C Programming
    Replies: 10
    Last Post: 10-25-2006, 06:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21