Thread: Completely new to programming: Having issues with arrays

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    1

    Completely new to programming: Having issues with arrays

    Hi guys, first things first, I am not looking for someone to write all this up for me I am jut really stuck and won't be able to get help from tutors until term starts again in October. I was in hospital in term time but have been allowed to keep studying as long as I do it over the summer. I figured you guys might be able to help me out.

    The first bit of the assignment is as follows:

    Write a program to calculate the distribution of numbers produced by the random number function
    Code:
    rand()
    .

    1) Calculate a random number between 0 and 1. To do this use
    Code:
    rand()
    and divide by
    Code:
    RAND_MAX
    . Remember to use a
    Code:
    CAST
    for
    Code:
    RAND_MAX
    2) Next write some code to ‘bin’ the data. Create an array where each element represents a bin
    (use, say, 10 bins to start but make this variable). Increment (increase by one) the array element
    where the number falls. You need to calculate which bin the random number will fall into. If x is
    the random number between 0 and 1 and there are 10 bins, then the bin number will be the integer
    part of x*10 (assuming your array starts at 0).

    Repeat this for a large number of times (use a
    Code:
    for(){} loop
    ).

    I feel like I'm almost there with this one. I have two seperate programmes I am trying this with, on the suggestions of other people who have given me a hand.

    The first is as follows:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     int main(){
            const int MAXBINS = 10, MAXITERS = 1000; //declares the number of bins and the number of of the numbers.
            int bins[MAXBINS], i;
            srand(time(NULL)); //ensures a different answer is given each time the program is run.
            for (i=0; i<MAXBINS; ++i) bins[i] = 0;
            for (i=0; i<MAXITERS; ++i) bins[(int)(rand()/(double) RAND_MAX)  &MAXBINS]++; 
            for (i=0; i<MAXBINS; ++i) printf("bin %d has count %f\n", i, bins[i]);
            return 0;
    }

    This code gives me the bin numbers and a corresponding 'value' the issue being all the values are 0.

    And the second method:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     int main()
    {
        double x;
        int i;
    
        for(i=0;i<9;i++)
    
         {
        x = rand()/(double) RAND_MAX;
        printf("%f\n",x);
        }
    
         if ((0 <= x) && (x < 0.1)) { /* statements */ }
        else if ((0.1 <= x) && (x < 0.2)) {  /* statements */ }
        else if ((0.2 <= x) && (x < 0.3)) { /* statements */ }
        else if ((0.3 <= x) && (x < 0.4)) { /* statements */ }
        else if ((0.4 <= x) && (x < 0.5)) { /* statements */ }
        else if ((0.5 <= x) && (x < 0.6)) { /* statements */ }
        else if ((0.6 <= x) && (x < 0.7)) { /* statements */ }
        else if ((0.7 <= x) && (x < 0.8)) { /* statements */ }
        else if ((0.8 <= x) && (x < 0.9)) { /* statements */ }
        else ((0.9 <= x) && (x < 1.0)) { /* statements */ }
    
         return 0;
    }
    So the statements will be the bin declarations, but again, not sure how to do that.

    Thanks in advance for your time spent helping me, it means the world.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    In your first example in need to include <time.h>; you must use "%d" to print each bin value (bin is an array of int, not double), and the index generated by your expression is (almost) always 0.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    bins[(int)(rand()/(double) RAND_MAX)

    rand() / (double)RAND_MAX will be a floating point number between 0 and 1. then when you cast it to int it will turn into 0 as the fraction is truncated.
    instead of &'ing it by MAXBINS, try multiplying that value by MAXBINS, BEFORE casting to int, then cast the whole thing to int

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Looks like qny and dmh2000 already gave you the fixes you need to actually make this work. A few extra suggestions for you, since you are a beginner:

    • Programming is not about squeezing as much code onto a single line as possible. Put each statement onto it's own line. This helps your code be organized, which means it's easy to read/understand and harder to make silly mistakes.
    • Indent your code properly. Read this link: SourceForge.net: Indentation - cpwiki.
    • I also suggest always using curly braces for loops, branches, etc, even if there is only one statement. If you decide to add a second statement, you can't forget to add braces, they're already there.
    • The above three suggestions are helped immensely by a good editor that does auto-indent. Also, syntax highlighting is really helpful. Make sure your editor does that.
    • Use more temporary variables, and give all your variables (temp ones too) descriptive names. Keeping your statements short and using good variable names keeps your code and your thoughts clear, so it's easier to keep track of what your code is doing, harder to screw up and easier to print out debugging statements.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Programming - Issues with return
    By Klcastillo in forum C Programming
    Replies: 3
    Last Post: 04-21-2012, 12:10 PM
  2. Having issues with I/O (fprintf and such) new to programming
    By jdouglas_usn in forum C Programming
    Replies: 4
    Last Post: 11-17-2011, 08:00 AM
  3. Newbie issues programming C w/i Xcode
    By marcus314 in forum C Programming
    Replies: 5
    Last Post: 06-09-2010, 11:44 AM
  4. Replies: 3
    Last Post: 08-17-2009, 08:25 AM
  5. C Programming issues
    By Vicky B in forum C Programming
    Replies: 13
    Last Post: 09-24-2001, 01:34 PM