Thread: Multidimensional arrays

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Multidimensional arrays

    Hi,

    The code which I have posted is messy and may not make much sense to you and I apologise for this. It is supposed to be performing an obscure quantum - mechanical calculation, hence, the reason for all the messy factorials etc.

    However, I have declared a number of arrays globally and among them are two two - dimensional arrays: wigner6j[20][20] and racah[20][20]. The number of the elements is not particularly important and is set randomly at 20 which will allow larger calculations to be made. The arrays are subsequently filled within the function Racah(), which follows the main() body, using 2 large for loops which encompass the complete function. The for loops are performed over integers from 0 to a value specified by the user, thus, giving the arrays the subscripts [m][n].

    Upon compiling (with g++), I get the error messages: "invalid types 'float[int]' for array subscript" which are each referring to the appropriate lines where the two - dimensional arrays are filled at the end of the function Racah().

    My question is simple: why can't I have a float array with integer subscripts? Have I understood the compiler's message? Can anyone help me with this problem?

    Thank you for taking the time to read this.

    dodzy
    Last edited by dodzy; 09-14-2006 at 05:23 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    	if (ans1 = 'y'){
    = for assignment.
    == for equality.
    Code:
    		for (i = 1; i <= states; i++){
    ...
    				scanf("%d %f", &J[i], &E[i]);
    Arrays start at 0 and go through "size - 1".
    Code:
    				scanf("%d %f", &J[i], &E[i]);
    All of those are what your errors / warnings are talking about.

    That'll get you started.


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

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    9
    Thanks for your suggestions! I don't know how I could have been so sloppy.

    Unfortunately, your suggestions have not solved the problem. The same error messages remain.

    dodzy

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try posting your updated code. My psychic powers are resting.


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

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    9
    I already have. I just replaced the old code with the updated code.

    You will find that my original post has been updated.

    Cheers,

    dodzy

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well here
    calc[22] = J[m] + j_p + j_n + 1;
    and here
    intercalc[6] = calc[22] - i;
    you're running off the ends of the arrays.

    arr[N] has indices 0 to N-1, not 0 to N or 1 to N
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That probably wouldn't cause an error message, though . . .

    This computer can't open the attached file for some reason, but I'd guess that you're using a floating-point number for an array index. Something like this:
    Code:
    array[1.2] = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One of the hazards of using globals I suppose.

    You could try a different naming convention, say prefix all globals with g_ it you insist on using them. I notice all your functions have no parameters.

    Anyway
    Code:
    float Wigner6j[20][20], racah[20][20];
    And in the function
    Code:
    	float mincalc, wigner6j[20], Wigner6j, Racah, phase1, phase2;
    That's right, the local variable with the same name isn't an array, so it barfs on the attempt to subscript it.


    For your evening supper, try this
    Code:
    $ gcc -Wshadow -W -Wall -ansi -pedantic -O2 new.c
    new.c:19: warning: return type defaults to `int'
    new.c: In function `Racah':
    new.c:118: warning: declaration of 'Wigner6j' shadows a global declaration
    new.c:15: warning: shadowed declaration is here
    new.c:118: warning: declaration of 'Racah' shadows a global declaration
    new.c:111: warning: shadowed declaration is here
    new.c:249: error: subscripted value is neither array nor pointer
    new.c:262: error: subscripted value is neither array nor pointer
    new.c:118: warning: unused variable `Racah'
    That is, once you've turned all the // comments into proper C comments.

    Having a local variable the same name as your function would cause an even more bizarre error message if you'd tried to call the function recursively.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 10:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM