#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    A Array with random numbers


    Hello Im trying to create an Array of 100 doubles

    in a for loop, assigning each of the doubles with a random number between .50 and 50.00


    Here is my code so far

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
    double array[100];
    int x;

    srand(time(NULL));

    for(x=0;x<100;x++)
    {
    array[x] = (double) (rand() % 100 + 1) / 2.0;
    }

    for(x=0;x<100;x++)
    {
    printf("%d ", array[x]);
    }
    system("pause");
    return 0;

    }

    for some reason its not selecting random numbers
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    First, use code tags to preserve your code's indenting. HTML strips that out, but code tags prevent that from happening.

    [code] insert your formatted code here [/code]

    Your code (indenting retrieved via Reply button) with code tags:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        double array[100];
        int x;
    
        srand(time(NULL));
    
        for(x=0;x<100;x++)
        {
            array[x] = (double) (rand() % 100 + 1) / 2.0;
        }
    
        for(x=0;x<100;x++)
        {
            printf("%d ", array[x]);
        }
    	system("pause"); 
        return 0;
    
    }
    printf("%d ", array[x]);
    %d displays an integer. You want to display a double, which is a floating-point format. Try "%f" instead.

    And while you're at it, read the documentation on printf to read what the different conversion specifiers mean. For example, you have a few options with floating-point that you should make yourself aware of. Google on man page printf.

    Comments on this post

    • eramit2010 agrees : :)
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    u r using %d in printf so its printing 0 and u r thinking that its not random.
    use %f and it will work.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    First, use code tags to preserve your code's indenting. HTML strips that out, but code tags prevent that from happening.

    [code] insert your formatted code here [/code]

    Your code (indenting retrieved via Reply button) with code tags:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        double array[100];
        int x;
    
        srand(time(NULL));
    
        for(x=0;x<100;x++)
        {
            array[x] = (double) (rand() % 100 + 1) / 2.0;
        }
    
        for(x=0;x<100;x++)
        {
            printf("%d ", array[x]);
        }
    	system("pause"); 
        return 0;
    
    }
    printf("%d ", array[x]);
    %d displays an integer. You want to display a double, which is a floating-point format. Try "%f" instead.

    And while you're at it, read the documentation on printf to read what the different conversion specifiers mean. For example, you have a few options with floating-point that you should make yourself aware of. Google on man page printf.

    ok thank you I will make sure I follow the proper way to post next time

IMN logo majestic logo threadwatch logo seochat tools logo