Like Tree3Likes
  • 1 Post By quzah
  • 1 Post By quzah
  • 1 Post By quzah

Beginner has problem。Can anyone have a look at my code?

This is a discussion on Beginner has problem。Can anyone have a look at my code? within the C Programming forums, part of the General Programming Boards category; I'm trying to get the coordinates of a 9x9 flat grid. The first point start at the origin. The current ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Beginner has problem。Can anyone have a look at my code?

    I'm trying to get the coordinates of a 9x9 flat grid. The first point start at the origin. The current code has output but is apparently wrong.
    Code:
    #include<math.h>
    #include<stdio.h>
    #define pi 3.1415926
    double initial( int Nbead, double Rold[3][Nbead],int Nx)
    {int i;
    for (i=0;i<Nbead;i++){
        Rold[0][i] = (floor(i/Nx)-1)+1;
        Rold[1][i] = (i%Nx);
        Rold[2][i] = 0.0;
    }
    return Rold[3][Nbead];
    }
    
    int main()
    {
    int Nx=9;
    int Ny=9;
    double aa=1;
    int Nbead=Nx*Ny; /*total number of beads*/
    double Rold[3][Nbead];
    int Nbond=2*Nx*Ny-Nx-Ny;/*total number of bonds*/
    int j,k;
    /*get the position (a flat sheet)*/
    double initial(int Nbead, double Rold[3][Nbead],int Nx);
    for (k=0;k<Nbead;k++)
        {
        for (j=0;j<3;j++)
            printf("the j is %d, the k is %d, Rold is %d\n",j, k,Rold[j][k]);
        printf("\n");
        }
        return 0;
    }
    Last edited by smartfish; 04-24-2011 at 06:26 PM. Reason: code improved

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    Code:
    double * Rold[3][Nbead];
    Why do you have an array of pointers?

    Tim S.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Yes, I know there's should have something to do with the printer. My point is I need to pass the matrix from the initial function to the main function. Pointer should be the way of doing that (I guess?). Any suggestion on how I should change that?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    Get rid of the pointer till you can think of a real reason.
    An array is treated just like a pointer in most cases that a beginner needs to do.

    Tim S.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Thanks for the suggestion. I got rid of the pointer. But still don't get the right result? I think the matrix is not passed from the initial function to the main function?

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    Code:
    /*get the position (a flat sheet)*/
    double initial(int Nbead, double Rold[3][Nbead],int Nx);
    for (k=0;k<Nbead;k++)
    You need to call initial in your code; what you did is another prototype of the function.

    something like below will be needed.
    Code:
    /*get the position (a flat sheet)*/
    initial(Nbead, Rold, Nx);
    for (k=0;k<Nbead;k++)
    Also the following is not right
    printf("the j is %d, the k is %d, Rold is %d\n",j, k,Rold[j][k]);
    Last edited by stahta01; 04-24-2011 at 06:37 PM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    And now I'm getting an error message as follows

    In function 'main':
    |error: incompatible type for argument 2 of 'initial'|
    |note: expected 'double (*)[<Uc630> + 1u]' but argument is of type 'double'|
    ||=== Build finished: 1 errors, 0 warnings ===|

    Does this mean it still need a pointer?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,773
    What is your code; since it works for me.

    Edit: You do know that you are doing things that does NOT work with all C Compilers; some of them are NOT C99 Standard.
    Edit: You should know that an array name is a pointer in C. See the Next post for the exact truth; but, they are much the same in this case.
    http://c-faq.com/aryptr/aryptrequiv.html

    Edit: Getting tired; I suggest indenting your code; you are more likely to get more help for code that is indented.
    http://sourceforge.net/apps/mediawik...le=Indentation

    Tim S.
    Last edited by stahta01; 04-24-2011 at 09:24 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    Edit: You should know that an array name is a pointer in C.
    Except for when it's not (which is always).
    Code:
    char array[ X ];
    array++; /* if it were a pointer, that would be fine */
    Read: arrays and pointers


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    @quzah: The OP started out with an array of Double pointers without ever allocating any doubles for the pointers to point to in real memory.
    It doesn't matter what the array is. An array name is not a pointer. You can't increment the array name, that means it's not a pointer. Array names are not pointers. That was the point I was making.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    OK, my code is working now. Thanks for all the replies!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    You can not increment an constant integer; but it is still an integer.
    Tim S.
    That's a stupid comparison. You can't compare a qualified type with a non-qualified one. And no, it's not a constant integer is not an integer. It's a constant integer. They are not the same thing. That's like trying to say "an lvalue and an rvalue are the same thing because they both are values!"

    Array names are not pointers. You cannot assign a different address to the name of an array. You cannot make an array name point to anything. That means it's not a pointer.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by stahta01 View Post
    You are not following me an array name is the same as a constant pointer in every case that I know about.

    Tim S.
    No, YOU are not following. They aren't the same thing. I already showed you a FAQ that explains how they are different, but here, let's do one more, shall we?
    Code:
    char array[ 10 ];
    char const *p = array;
    
    printf( "sizeof( array ) is %d, and sizeof( p ) is %d\n", sizeof( array ), sizeof( p ) );
    Now run along and read the link the nice man provided for you.


    Quzah.
    Salem likes this.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with beginner code... please help
    By rkrajnov in forum C Programming
    Replies: 2
    Last Post: 09-26-2010, 12:47 AM
  2. Beginner: What is wrong with my code? It seems fine!
    By shivdude in forum C Programming
    Replies: 10
    Last Post: 08-03-2009, 04:36 PM
  3. beginner here need help with an array code
    By Ohshtdude in forum C++ Programming
    Replies: 6
    Last Post: 02-01-2009, 05:42 PM
  4. Complete beginner needs help customising sorting code
    By Ian SH in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2007, 08:09 PM
  5. Code is hanging - Very simple C (Beginner)
    By pc_doctor in forum C Programming
    Replies: 10
    Last Post: 10-29-2007, 06:05 PM

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