Thread: Counting number from a random file

  1. #31
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok I understand most of the program... and what you did...
    but can you explain to me how these 2 part works??

    Code:
          
            ranges[rand_value / 100]++;

    Code:
        for (i = 0; i < 10; i++) {
            int lower = i * 100;
            int upper = (i + 1) * 100;
            printf("Number Records %03d-%03d: %d\n", lower, upper, ranges[i]);
            fprintf(fp, "Number Records %03d-%03d: %d\n", lower, upper,
                    ranges[i]);

  2. #32
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    I could, but I think you need to spend more than 10 minutes looking at it before giving up and asking for help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #33
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok... I see what you did now...
    it was to set the upper and lower limits...
    Just one final question...
    is it possible to use like a quick sort or a bubble sort for the random list??

    thank you so much by the way....
    I started programming like a little bit more than 2 and half weeks ago...
    So I am still new to this whole programming thing....

  4. #34
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > is it possible to use like a quick sort or a bubble sort for the random list??
    Yes you can.

    In stdlib.h, there is a function called qsort() which can sort any array.
    The really tricky bit is getting the compare function sorted out. Do a search for previous examples on this board to get an idea.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #35
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok I searched the forum... and followed some of the examples... but somehow it only prints out a list of 0....

    heres what I did:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define DATA_FILE "test_data.txt"
    
    void initializeRandom(void);    /* programmer written functions - prototypes */
    int getRandom(int maximum_value);
    int compare( void *a, void *b )
    {
      return ( a, b );
    }
    
    int main(void)
    {
        int ranges[10]    = { 0 };
        int rand_value    = 0;
        int max           = 0;
        int min           = 1000;
        int user_input    = 0;
        int i             = 0, n=0;
        int number_values = 0;
        int array[999]   = {number_values};
    
        char id_string[8];          /* 7 characters for the ID# + 1 for \0 */
    
        FILE *fp;
    
        /* 1.  Get user input */
        printf("Enter your colleague ID # --> ");
        scanf("%d", &user_input);
        fp = fopen(DATA_FILE, "w");
    
        /* Generate a string out of the value inputted by user */
        sprintf(id_string, "%07d", user_input);
        number_values = atoi(&id_string[4]);
        initializeRandom();
    
        fprintf(fp, "This is the number of values: %d\n", number_values);
        printf("This is the number of values: %d\n", number_values);
        fprintf(fp, "This is the list\n");
        printf("This is the list\n");
    
      for (i = 0; i < number_values; i++)
     {
        rand_value = getRandom(999);
        fprintf(fp, "%d\n", rand_value);
        printf("%d\n", rand_value);
    
      if (rand_value > max)
      {
        max = rand_value;
      }
      if (rand_value < min)
      {
        min = rand_value;
      }
        ranges[rand_value / 100]++;
     }
      printf("The sorted list is:\n");
      qsort(rand_value, number_values, number_values, compare);
      for ( i=0; i<number_values; i++ )
    
      printf( "%d\n", array[i] );
    
      if (max > 0)
      {
        printf("this is the max value %d\n", max);
        fprintf(fp, "this is the max value %d\n", max);
      }
      if (min > 0)
      {
        printf("this is the min value %d\n", min);
        fprintf(fp, "this is the min value %d\n", min);
      }
    
      for (i = 0; i < 10; i++)
      {
        int lower = i * 100;
        int upper = (i + 1) * 100;
        printf("Number Records %03d-%03d: %d\n", lower, upper, ranges[i]);
        fprintf(fp, "Number Records %03d-%03d: %d\n", lower, upper,
        ranges[i]);
      }
        fclose(fp);
        system("pause");
        return (0);
    }
    
    
    /* Call this function to start random number generation, */
    /* else every time you run you'll get the same answers - ONLY CALL 1 time */
    void initializeRandom(void)
    {
        srand((unsigned) time(NULL));
    }
    
    /* get a random number between 1 and maximum_value passed in */
    int getRandom(int maximum_value)
    {
        int random = 0;
        random = rand();
        /* Modulus gives you a value between 0 and 1 less */
        /* then number dividing by, so add 1 to shift */
        random = (random % maximum_value) + 1;
        return (random);
    }

  6. #36
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    help please????

  7. #37
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int compare( void *a, void *b )
    {
        return ( a, b );
    }
    Huh??? What are you trying to do with this bit of code?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #38
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
        int array[999]   = {0};
    Code:
    int compare( const void *a, const void *b )
    {
       const int *x = a, *y = b;
       return *x > *y ? 1 : *x < *y ? -1 : 0;
    }
    Code:
       for ( i = 0; i < number_values; i++ )
       {
          rand_value = getRandom(999);
          /* ... */
          array[i] = rand_value;
       }
       printf("The sorted list is:\n");
       qsort(array, number_values, sizeof *array, compare);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #39
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    thank you so much!!!
    Only one question though...
    how does this part works??

    Code:
    int compare( const void *a, const void *b )
    {
       const int *x = a, *y = b;
       return *x > *y ? 1 : *x < *y ? -1 : 0;
    }
    I know we are comparing the two... but I dont understand why it return x> y and so on...

  10. #40
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by kamisama
    thank you so much!!!
    Only one question though...
    how does this part works??
    Code:
    int compare( const void *a, const void *b )
    {
       const int *x = a, *y = b;
       return *x > *y ? 1 : *x < *y ? -1 : 0;
    }
    I know we are comparing the two... but I dont understand why it return x> y and so on...
    I'm glad you asked. It means pretty much the same as this.
    Code:
    int compare( const void *a, const void *b )
    {
       const int *x = a, *y = b;
       if ( *x > *y )
       {
          return 1;
       }
       if ( *x < *y )
       {
          return -1;
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #41
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    oh cool... is that also part of c too???

  12. #42
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by kamisama
    oh cool... is that also part of c too???
    If you mean the conditional operator ?:, then yes.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #43
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    oh ok...
    Thank you all so much... I learned so much just by doing this one program... hehehehe
    Thanks for the help...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  3. Counting Number of Lines of file
    By dapernia in forum C Programming
    Replies: 1
    Last Post: 09-05-2003, 02:22 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. counting the number of words in a file
    By stuartbut in forum C Programming
    Replies: 2
    Last Post: 04-13-2002, 08:19 AM