Thread: array question

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    array question

    Hello there. Does anybody know why doesn't it work?
    Compiler message:invalid types 'float [100][float]' for array subscript at lines 11,12.
    Code:
    #include<stdio.h>
    
    #define max 100
    
    int main()
    {int f[max],a,b,c,x;				/*line 8*/
    
        printf("f(x)=ax^4+bx^2+c dose a,b,c\n");
        scanf("%d %d %d",&a,&b,&c);
       for(x=0;x<max;x++)
        {f[x]=a*x*x*x+b*x*x+c;      		 /*line 11*/                
          if(f[x]==0) printf("%d ",x);}		  /*line 12*/
    
    getchar();
    getchar();
    return 0;
    }
    Why does it work when at line 8 I change float with int?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Arrays are subscripted with int.

    You'd need to do something like:

    float f[max];
    int x;
    <whatever> <your other vars>;

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    getchar();
    getchar();
    Non-portable, see the FAQ.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by manutd
    Code:
    getchar();
    getchar();
    Non-portable, see the FAQ.
    Okay, I'll bite. . . how is this non-portable? getch() is the one that is non-portable, I thought. . . getchar() works on Linux, Windows, DOS, however, I don't know about Mac. Where is it non-portable to?

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Whoops, sorry. My bleary tired eyes saw getch
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Actually, in retrospect manutd is right. The function itself is portable, but the behaviour you want is probably nonportable. To get rid of the trailing input newline nicely, use fgets() and sscanf() instead.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM