print an array

This is a discussion on print an array within the C Programming forums, part of the General Programming Boards category; Im working on a probability math problem where I need to print out how many triplets within the array are ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31

    encountered a problem and needs to close

    Im working on a probability math problem where I need to print out how many triplets within the array are divisible by 5. my program compiles but I get get an error when I try to run the program. Not sure if my code is correct. I know its a little primative but I'm working with what little knowledge I have of the language. Any ideas why I cant run the program?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int array[10] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
    
    int main()
    {
        int a, b, x, i, j;
        int tmp;
        int count = 0;
        
        for(j=0; j<120000; ++j)
        { 
             for(x=0; x<50; ++x)
             {
                  srand(time(NULL));
                  a = (rand() % (array[10] - array[0] + 1) + array[0]);
                  b = (rand() % (array[10] - array[0] + 1) + array[0]);
                  tmp = a;
                  a = b;
                  b = tmp;
                  i = array[0] + array[1] + array[2];
                  if(i%5==0) ++count;
             }
        }
        printf("Count = %d\n", count);
        getchar();
        return 0;
    }
    Last edited by Dr Spud; 11-27-2005 at 01:26 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no array[10]. It goes from 0 through 9.


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

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31
    thank you that takes care of my error. now I just need to figure out where I messed up on my shuffling technique because my output is 120000 and should be nowhere near that. This is the first time I use Dev-C++ to generate random numbers.
    Last edited by Dr Spud; 11-27-2005 at 01:58 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31
    Im not sure that is whats wrong I changed a few things that the faq suggests and still get the same output.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int array[10] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
    
    int main()
    {
        int a, b, x, i, j;
        int tmp;
        int count = 0;
        
        static int Init = 0;
        if(Init == 0)
        {
             srand(time(NULL));
             Init = 1;
        }
        
        for(j=0; j<120000; ++j)
        { 
             for(x=0; x<50; ++x)
             {
                  a = (rand() % (array[9] - array[0] + 1) + array[0]);
                  b = (rand() % (array[9] - array[0] + 1) + array[0]);
                  tmp = a;
                  a = b;
                  b = tmp;
             }
             i = array[0] + array[1] + array[2];
             if(i%5==0) ++count;
        }
        printf("Count = %d\n", count);
        getchar();
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2005
    Location
    Alpine, TX
    Posts
    31
    I think I am decaling the max and min wrong. All I want to do is randomly get 2 of the values within my array and have them switch places.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    v1 = rand() % number of items in array;
    v2 = rand() % number of items in array;
    
    temp = array[ v1 ];
    array[ v1 ] = array[ v2 ];
    array[ v2 ] = temp;
    Like that?


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 12:41 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 10
    Last Post: 07-13-2003, 01:48 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21