Thread: what is an Illegal floating point ?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    what is an Illegal floating point ?

    Hi all, i made an array and there is just one error left to fix and i really dont understand it the problem is in these lines:


    Code:
    Average+=scores[i];
    Average=Average/N;
    printf("\n Average score = %f",Average);
    
    for(j=0;j<M;j++)
    
    Average+=scores[j];
    Average=Average/M;
    printf("\n Average = %f",Average);
    the error the compiler gives me is "Illegal use of Floating Point" , can anyone help me please ?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    How about showing the types of the variables...?

    I'm guessing you used a floating point for an array index but I'm not really that knowledgeable on floating point operations.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    ok here is the whole code :


    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    #define max_student 50
    #define max_subject 7
    main()
    
    {
    int scores[max_student][max_subject];
    int N,M;
    int i,j;
    
    
    do
    {
    printf("\n Enter No. Of students");
    scanf("%d",&N);
    }
    while(N<1||N>max_student);
    
    do
    {
    printf("\n Enter No. Of Subjects");
    scanf("%d",&M);
    }
    while(M<1 || M>max_subject);
    
    for(i=0;i<=N;i++)
    
    {
    printf("\n enter scores of student No. %d",i+1);
    scanf("%d",&scores[i]);
    }
    
    for(j=0;j<=M;j++)
    
    {
    printf("\n Enter score of subject No. %d",j+1);
    scanf("%d",&scores[j]);
    }
    
    double Average=0.0;
    
    for(i=0;i<N;i++)
    
    Average+=scores[i];
    Average=Average/N;
    printf("\n Average score = %f",Average);
    
    for(j=0;j<M;j++)
    
    Average+=scores[j];
    Average=Average/M;
    printf("\n Average = %f",Average);
    
    getch();
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    It's all there in the errors:
    Code:
    1>------ Build started: Project: CProg4, Configuration: Debug Win32 ------
    1>Compiling...
    1>CProg4.cpp
    1>cprog4.cpp(43) : error C2297: '+=' : illegal, right operand has type 'int [7]'
    1>cprog4.cpp(43) : error C2440: '+=' : cannot convert from 'int [7]' to 'double'
    1>cprog4.cpp(48) : error C2297: '+=' : illegal, right operand has type 'int [7]'
    1>cprog4.cpp(48) : error C2440: '+=' : cannot convert from 'int [7]' to 'double'
    1>Build log was saved at "????"
    1>CProg4 - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Specifically it points to these lines:
    Code:
    int scores[max_student][max_subject];
    
    ...
    
    for(i=0;i<N;i++)
        Average+=scores[i];
    
    ...
    
    for(j=0;j<M;j++)
        Average+=scores[j];
    Based on what "scores" is it should be obvious.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    still having same problem after making it like this :

    Code:
    double scores[max_student][max_subject];
    double N,M;
    double i,j;
    and i also tried to initialize i=0.0 instead of just 0 ,j=0.0 instead of 0
    Last edited by everyone0; 04-27-2010 at 06:30 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The problem isn't that "scores" is two-dimensional array of ints, it's that it's a two-dimensional array. "scores[i]" is a one dimensional array. You can't add an array of objects, no matter the type (int/float/double), to a variable like you are trying to do. You probably want a double/nested loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    If your really having trouble with something take the time to isolate and experiment with the suspects:
    Code:
    #include <stdio.h>
    
    int main(void)
    { 
      int scores[2][2] = { {1, 2}, {3, 4} };
    
      /* scanf("%d", &scores[0]);   warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int (*)[2]’ */
      scanf("%d", &scores[0][0]);  /* ok */
      
    
      /* scores[0] += 1;           error: invalid operands to binary + */
      scores[0][0] += 1;          /* ok */
       
      printf("scores[0][0] += 1 = %d \n", scores[0][0] );
     
      return 0;
    }
    Maybe realizing what was wrong there will help you fix the logic in your for() loops.
    Better indentation would help with that too.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    thank you so much guys , i fixed the problem with ur help , thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why illegal use of floating point?
    By chottachatri in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2008, 07:00 AM
  2. strtol with floating point
    By Laserve in forum C Programming
    Replies: 3
    Last Post: 07-26-2004, 06:02 AM
  3. floating point variables in edittext controls
    By dootickle in forum Windows Programming
    Replies: 3
    Last Post: 04-15-2004, 11:15 AM
  4. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM
  5. Understanding floating point
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-11-2003, 03:58 PM