Thread: Simple code

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    Simple code

    Hello! So i started programming only 2 days ago and i have this problem. When i run this program it says "array subscript is not an integer". I cannot find the error.

    Code:
    
       float notes[5] = {0};
       float total = 0;
       float average;
       float i=0;
    
    
       printf(" Type the scores :\n");
    
    
    
    
       for (i=0; i<5; ++i){
           scanf("%f", &notes[i]);
       }
        for (i=0; i<5; ++i){
           total +=  notes[i];
        }
        average = total /5;
    
    
        printf(" %f", average);
    
    
    return 0;
    
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your counter, "i", is a float. You can't use a float as array index. Change it to int instead.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Thank you !

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Also, you should provide a full program, not chopping off the #includes and the "int main(void) {", so we can compile and test your code.

    You also should insure that each call to scanf() does return a valid value. In your code scanf() should return 1 if successful. What if the user types in "XYZ", instead of "123"?

    We don't know what source you are using to learn C, but if it did not discuss this, then you should buy yourself a good book on the C Programming Language. And hopefully you are using a modern compiler, like gcc, or Clang, not Turbo-C!

    Good luck!

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Well, I'm in college and I should know this by now, but i've been slacking all day. I have my final exam for C programming in a week and I am not really confident about it (since i only started studing this 2 days ago). Nevertheless, I am watching youtube tutorials and in case it doesn't go well i have in about a 2 weeks an extra exam to boost my grade. It should not be difficult since it's the first semester of the first year of college but one does never know. By the way by a "modern compiler" I think you are refering to the program i use in C and it's codeblocks. The original code was :
    Code:
    
    
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    int main(void) {
    
    
    float notes[5] = {0};
       float total = 0;
       float average;
       float i=0;
     
     
       printf(" Type the scores :\n");
     
       for (i=0; i<5; ++i){
           scanf("%f", &notes[i]);
       }
        for (i=0; i<5; ++i){
           total +=  notes[i];
        }
        average = total /5;
     
        printf(" %f", average);
     
    return 0;
    }

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Code::Blocks is an IDE, not a compiler. It calls the compiler, which is a modern compiler that adheres to C99 or C11 as you are using stdbool.h.

    If you are taking a course in C, why haven't you been studying the language all along, doing the exercises in the book or assigned by the instructor???

    As for YouTube videos, they are painfully inadequate, and/or downright wrong. A good book on C would be a better choice!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Probably simple code help.
    By Sanit in forum C Programming
    Replies: 7
    Last Post: 03-25-2009, 01:00 PM
  2. A Very Simple C++ Code
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2007, 12:15 PM
  3. Help with simple code
    By shin in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2005, 05:02 PM
  4. help with simple code (i think!)
    By JimJamJovi in forum C Programming
    Replies: 2
    Last Post: 01-10-2002, 10:53 AM

Tags for this Thread