Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why does this code not do what it should

  1. Nov 17, 2007 #1
    I can't see what is wrong with this code, but for some reason it doesn't work. It compiles, just doesn't do what it should.

    Code (Text):

    for(i=0; i<n; i++)
        {
                 delx[i] = i/n;
        }
    I have defined delx as an array, with n elements, and just want to fill the array with evenly spaced values from 0 to n. When I try running it, it says that all elements of the array are 0. Any chance someone could explain to me where the problem is.
     
  2. jcsd
  3. Nov 17, 2007 #2
    If i is declared type int, and n is declared type int, that would explain the behavior.

    try this:

    delx = float(i)/float(n);

    this is called casting, when you force the the type of a variable to change for a particular calculation (I am not sure if float() is the correct syntax for C).

    Alternatively I think you can declare n as a float, but I am no C expert. The problem though is that when you divide a small integer by a large one and get an integer result, the result has to be zero.
     
  4. Nov 17, 2007 #3

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Presumably i and n are integers? Integer division will round down.
    Use delx[x] = (float)i/(float)n;
     
  5. Nov 17, 2007 #4

    ranger

    User Avatar
    Gold Member

    The problem seems to be that you're not casting correctly (assuming array type int). The way I would do it is to make my array a float and then do a integer division and perform casting:

    Code (Text):

    #include <stdio.h>

    #define n 10

    int main()
    {
        float delx[n];
        int i=0;
       
        for(i=0; i<n; i++)
        {
                 delx[i] = (float) i/n;
                 printf("%f\n", delx[i]);
        }
    return 0;
    }
     
    Well if his array has n elements, then array size cannot be specified by a non-integer type. So I guess integer division is sort of implicit.
     
    Last edited: Nov 17, 2007
  6. Nov 17, 2007 #5

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    The code in the op doesn't define i or n, or say if n is the array size;
    Some (naughty) compilers will cast a float index to an array to int.
     
  7. Nov 18, 2007 #6

    KTC

    User Avatar

    Only if both operands are positive, otherwise the behaviour is implementation defined.
     
  8. Nov 18, 2007 #7

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Yes, I mean't that they wouldn't round using the normal arithmetic rules which is what the OP expedcted. The word I was looking for was truncate.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Why does this code not do what it should
  1. What code is this? (Replies: 6)

Loading...