Thread: filling float array

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7

    filling float array

    Hi all
    trying to fill array with floats.
    #error array subscript is not an integer
    Code:
    #include <stdio.h>
    #define N 25
    
    int main ()
    {  
       int min,sec,total_sec;   
       float s,lapspeed,splits[N];
    
       printf("Enter minute and seconds\n");
       scanf("%d %d", &min, &sec);
       total_sec = min * 60 + sec;
       lapspeed = (total_sec / N);
       for(s==0;s<=N;++s)  
       splits[s] = s * lapspeed;
       for(s==0;s<=N;++s)
       if (splits[s] % 5 == 0)
       putchar('\n');
       printf("%f\n", splits[s]);
       return 0;
    }
    once this is fixed I plan to change array contents to char[],to convert splits to minutes and seconds. I assume I use sprintf.
    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In other words, change s from being a float to say, int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7
    Quote Originally Posted by laserlight View Post
    In other words, change s from being a float to say, int.
    Thanks for the prompt answer Laserlight, but if you do the maths on it s will always be a real number. I tried changing s to int and got
    Code:
    warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He didn't tell you to start randomly changing variables around to different types. He said change this:
    Code:
    float s
    to this:
    Code:
    int s;
    So that this:
    Code:
    plits[s] = s * lapspeed;
    ...
       if (splits[s] % 5 == 0)
    ...
       printf("%f\n", splits[s]);
    Doesn't keep telling you this:
    #error array subscript is not an integer

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

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7
    okay, thanks for your input. What does this mean then?
    Code:
    16:18: error: invalid operands to binary % (have ‘float’ and ‘int’)
    Off to clear my head!

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You probably just changed the keyword "float" to "int" on line 7. That affects s, lapspeed, and splits[N]. You need to make the change so it only affects s, not lapspeed and splits.

    There is a bunch of other things wrong with your code. Particularly the loops.

    Only integral types can be used with the % (modulo) operator.

    Try debugging your code yourself, rather than posting here every time your get an error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    To all:
    Do people ever troll? Have you ever suspected anyone of trolling? I get suspicious when I see something like this, with a bug on almost every line.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7
    Quote Originally Posted by oogabooga View Post
    To all:
    Do people ever troll? Have you ever suspected anyone of trolling? I get suspicious when I see something like this, with a bug on almost every line.
    Do kids fall off their bikes intentionally?

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    if a tree falls in a forest and noone is around to hear it, does it make a sound?

    See, I can make nonsense comments too.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by oxygen_debt View Post
    Do kids fall off their bikes intentionally?
    I don't suppose they do. Sorry about that. I didn't actually think you were trolling. I just wondered if it ever happens. BTW, you're using == in your for statement where you should be using =
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Registered User
    Join Date
    Jan 2012
    Location
    New Zealand, Hastings
    Posts
    7
    Quote Originally Posted by oogabooga View Post
    I don't suppose they do. Sorry about that. I didn't actually think you were trolling. I just wondered if it ever happens. BTW, you're using == in your for statement where you should be using =
    Thanks for the catch, compiles & runs. I'll 'bug' you all some more maybe. Now I'm reading at converting splits[s] to string, minutes:seconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array..
    By kromagnon in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 06:53 PM
  2. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 01:22 PM
  3. Filling an array
    By GCNDoug in forum C Programming
    Replies: 18
    Last Post: 04-25-2007, 02:46 PM
  4. Filling an array
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 02:02 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 03:11 PM