Thread: Counting number from a random file

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your strncpy is still bugged

    number_values = atoi(&id_string[4]);
    would at least save you from a strncpy.

    Also, set min to some large value - say 1000 - before you start
    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.

  2. #17
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    oh ok... I see whats wrong... I fixed it and the program works!!
    Thank you all so much!!!

    But now I have another problem....
    I want to find the total number of numbers between so and so...
    how can I do that?

    For ex:
    list shows
    11
    12
    23
    7
    8

    the out put would be:
    the total number of value from 0-9 is 2
    the total number of value from 10-19 is 2
    the total number of value from 20-29 is 1

  3. #18
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Just use a series of conditionals as you cycle through each number. Set up a loop that goes through each number in an array. In each conitional, specify a range, and if that range is matched, increment a counter corresponding to that range.

  4. #19
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok I did something like this...


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define DATA_FILE "c:\\test_data.txt"
    
    void initializeRandom(void);   /* programmer written functions - prototypes */
    int getRandom(int maximum_value);
    
    
    int main (void)
    {
      int one_hun       = 0;  // less than one hundred count
      int two_hun       = 0;  // less than two hundred count
      int three_hun     = 0;  // less than three hundred count
      int four_hun      = 0;  // less than four hundred count
      int five_hun      = 0;  // less than five hundred count
      int six_hun       = 0;  // less than six hundred count
      int seven_hun     = 0;  // less than seven hundred count
      int eight_hun     = 0;  // less than eight hundred count
      int nine_hun      = 0;  // less than nine hundred count
      int thousand      = 0;  // less than thousand count
      int  rand_value    = 0;
      int  total_value   = 0;
      int  count         = 0;
      int  var           = 0;
      int  max           = 0;
      int  min           = 1000;
      int  user_input    = 0;
      int  i             = 0;
      int  number_values = 0;
      char id_string[8];   /* 7 characters for the ID# + 1 for \0 */
      char number_string[4];
    
      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);
    
      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;
      }
     }
      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);
      }
    
      while(fscanf(fp, "%d", &rand_value) != EOF)
      { count++;
    
    
    
        // count where the value is
        if (rand_value < 100)
        {
          one_hun++;
        }
        if (rand_value < 200)
        {
          two_hun++;
        }
        if (rand_value < 300)
        {
          three_hun++;
        }
        if (rand_value < 400)
        {
          four_hun++;
        }
        if (rand_value < 500)
        {
          five_hun++;
        }
        if (rand_value < 600)
        {
          six_hun++;
        }
        if (rand_value < 700)
        {
          seven_hun++;
        }
        if (rand_value < 800)
        {
          eight_hun++;
        }
        if (rand_value < 900)
        {
          nine_hun++;
        }
        else
        {
          thousand++;
        }
      
       
      }
    
      fprintf(fp, "Number Records < 100:    %d\n", one_hun);
      fprintf(fp, "Number Records 100-199:  %d\n", two_hun);
      fprintf(fp, "Number Records 200-299:  %d\n", three_hun);
      fprintf(fp, "Number Records 300-399:  %d\n", four_hun);
      fprintf(fp, "Number Records 400-499:  %d\n", five_hun);
      fprintf(fp, "Number Records 500-599:  %d\n", six_hun);
      fprintf(fp, "Number Records 600-699:  %d\n", seven_hun);
      fprintf(fp, "Number Records 700-799:  %d\n", eight_hun);
      fprintf(fp, "Number Records 800-899:  %d\n", nine_hun);
      fprintf(fp, "Number Records 900-999:  %d\n", thousand);
      printf("Number Records < 100:    %d\n", one_hun);
      printf("Number Records 100-199:  %d\n", two_hun);
      printf("Number Records 200-299:  %d\n", three_hun);
      printf("Number Records 300-399:  %d\n", four_hun);
      printf("Number Records 400-499:  %d\n", five_hun);
      printf("Number Records 500-599:  %d\n", six_hun);
      printf("Number Records 600-699:  %d\n", seven_hun);
      printf("Number Records 700-799:  %d\n", eight_hun);
      printf("Number Records 800-899:  %d\n", nine_hun);
      printf("Number Records 900-999:  %d\n", thousand);
        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 ((time_t *)NULL));
    }
    
    /* get a random number between 1 and maximum_value passed in */
    int getRandom(int maximum_value)
    {
      int random = 0;
      random     = rand();
      random = (random % maximum_value) + 1;  /* Modulus gives you a value between 0 and 1 less then number dividing by, so add 1 to shift */
      return(random);
    }
    but there something wrong with it....

    I am not sure how to put this into an array though...

  5. #20
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    but there something wrong with it....
    What exactly is wrong with it? Getting error messages?

  6. #21
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    In this part it gives me some wierd number for the number of records...

    Code:
    /count where the value is
        if (rand_value < 100)
        {
          one_hun++;
        }
        if (rand_value < 200)
        {
          two_hun++;
        }
        if (rand_value < 300)
        {
          three_hun++;
        }
        if (rand_value < 400)
        {
          four_hun++;
        }
        if (rand_value < 500)
        {
          five_hun++;
        }
        if (rand_value < 600)
        {
          six_hun++;
        }
        if (rand_value < 700)
        {
          seven_hun++;
        }
        if (rand_value < 800)
        {
          eight_hun++;
        }
        if (rand_value < 900)
        {
          nine_hun++;
        }
        else
        {
          thousand++;
        }
      
       
      }

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well if the number is less than 100, every single one of your if statements is going to execute. Add else's before each one.

  8. #23
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    I did that and the output is still the same.
    This is the output:

    This is the number of values: 11
    905
    149
    781
    952
    377
    882
    461
    338
    976
    134
    995
    this is the max value 995
    this is the min value 134
    Number Records < 100: 0
    Number Records 100-199: 0
    Number Records 200-299: 0
    Number Records 300-399: 0
    Number Records 400-499: 0
    Number Records 500-599: 0
    Number Records 600-699: 0
    Number Records 700-799: 0
    Number Records 800-899: 0
    Number Records 900-999: 3967

  9. #24
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Post your entire source code as it is now.

  10. #25
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    Quote Originally Posted by sean_mackrory
    Post your entire source code as it is now.
    this is my code right now...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define DATA_FILE "c:\\test_data.txt"
    
    void initializeRandom(void);   /* programmer written functions - prototypes */
    int getRandom(int maximum_value);
    
    
    int main (void)
    {
      int one_hun       = 0;  // less than one hundred count
      int two_hun       = 0;  // less than two hundred count
      int three_hun     = 0;  // less than three hundred count
      int four_hun      = 0;  // less than four hundred count
      int five_hun      = 0;  // less than five hundred count
      int six_hun       = 0;  // less than six hundred count
      int seven_hun     = 0;  // less than seven hundred count
      int eight_hun     = 0;  // less than eight hundred count
      int nine_hun      = 0;  // less than nine hundred count
      int thousand      = 0;  // less than thousand count
      int  rand_value    = 0;
      int  total_value   = 0;
      int  count         = 0;
      int  var           = 0;
      int  max           = 0;
      int  min           = 1000;
      int  user_input    = 0;
      int  i             = 0;
      int  number_values = 0;
      char id_string[8];   /* 7 characters for the ID# + 1 for \0 */
      char number_string[4];
    
      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);
    
      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;
      }
     }
      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);
      }
    
      while(fscanf(fp, "%d", &rand_value) != EOF)
      { count++;
    
    
    
        // count where the value is
        if (rand_value < 100)
        {
          one_hun++;
        }
       else if (rand_value < 200)
        {
          two_hun++;
        }
       else if (rand_value < 300)
        {
          three_hun++;
        }
       else if (rand_value < 400)
        {
          four_hun++;
        }
       else if (rand_value < 500)
        {
          five_hun++;
        }
       else if (rand_value < 600)
        {
          six_hun++;
        }
       else if (rand_value < 700)
        {
          seven_hun++;
        }
       else if (rand_value < 800)
        {
          eight_hun++;
        }
       else if (rand_value < 900)
        {
          nine_hun++;
        }
        else
        {
          thousand++;
        }
      
       
      }
    
      fprintf(fp, "Number Records < 100:    %d\n", one_hun);
      fprintf(fp, "Number Records 100-199:  %d\n", two_hun);
      fprintf(fp, "Number Records 200-299:  %d\n", three_hun);
      fprintf(fp, "Number Records 300-399:  %d\n", four_hun);
      fprintf(fp, "Number Records 400-499:  %d\n", five_hun);
      fprintf(fp, "Number Records 500-599:  %d\n", six_hun);
      fprintf(fp, "Number Records 600-699:  %d\n", seven_hun);
      fprintf(fp, "Number Records 700-799:  %d\n", eight_hun);
      fprintf(fp, "Number Records 800-899:  %d\n", nine_hun);
      fprintf(fp, "Number Records 900-999:  %d\n", thousand);
      printf("Number Records < 100:    %d\n", one_hun);
      printf("Number Records 100-199:  %d\n", two_hun);
      printf("Number Records 200-299:  %d\n", three_hun);
      printf("Number Records 300-399:  %d\n", four_hun);
      printf("Number Records 400-499:  %d\n", five_hun);
      printf("Number Records 500-599:  %d\n", six_hun);
      printf("Number Records 600-699:  %d\n", seven_hun);
      printf("Number Records 700-799:  %d\n", eight_hun);
      printf("Number Records 800-899:  %d\n", nine_hun);
      printf("Number Records 900-999:  %d\n", thousand);
        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 ((time_t *)NULL));
    }
    
    /* get a random number between 1 and maximum_value passed in */
    int getRandom(int maximum_value)
    {
      int random = 0;
      random     = rand();
      random = (random % maximum_value) + 1;  /* Modulus gives you a value between 0 and 1 less then number dividing by, so add 1 to shift */
      return(random);
    }

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while(fscanf(fp, "%d", &rand_value) != EOF)
    You still have the file open for writing (and this is reading), and you're still outputting information.

    There's no need for this, just move all your if (rand_value < 100) statements alongside the if(rand_value > max) which is inside the while loop generating the numbers.

    Do you know about arrays yet?
    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.

  12. #27
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    yeah... arrays are a whole bunch of thing groups under one name... or something like that....

    But I dont know how to set it up though...
    I was thinking of doing it in an array... because is less tedious....

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well do the first bit and make sure it works, then we can do the arrays
    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.

  14. #29
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok.... I got my program to work with all the if statements and stuff...
    But how do I put it in an array now??
    Here's what I have so far...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define DATA_FILE "c:\\test_data.txt"
    
    void initializeRandom(void);   /* programmer written functions - prototypes */
    int getRandom(int maximum_value);
    
    
    int main (void)
    {
      int one_hun       = 0;  // less than one hundred count
      int two_hun       = 0;  // less than two hundred count
      int three_hun     = 0;  // less than three hundred count
      int four_hun      = 0;  // less than four hundred count
      int five_hun      = 0;  // less than five hundred count
      int six_hun       = 0;  // less than six hundred count
      int seven_hun     = 0;  // less than seven hundred count
      int eight_hun     = 0;  // less than eight hundred count
      int nine_hun      = 0;  // less than nine hundred count
      int thousand      = 0;  // less than thousand count
      int  rand_value    = 0;
      int  total_value   = 0;
      int  count         = 0;
      int  var           = 0;
      int  max           = 0;
      int  min           = 1000;
      int  user_input    = 0;
      int  i             = 0;
      int  number_values = 0;
      char id_string[8];   /* 7 characters for the ID# + 1 for \0 */
      char number_string[4];
    
      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);
    
      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;
      }
        if (rand_value < 100)
        {
          one_hun++;
        }
       else if (rand_value < 200)
        {
          two_hun++;
        }
       else if (rand_value < 300)
        {
          three_hun++;
        }
       else if (rand_value < 400)
        {
          four_hun++;
        }
       else if (rand_value < 500)
        {
          five_hun++;
        }
       else if (rand_value < 600)
        {
          six_hun++;
        }
       else if (rand_value < 700)
        {
          seven_hun++;
        }
       else if (rand_value < 800)
        {
          eight_hun++;
        }
       else if (rand_value < 900)
        {
          nine_hun++;
        }
        else
        {
          thousand++;
        }
    
     }
      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);
      }
    
        // count where the value is
    
      
       
    
    
      fprintf(fp, "Number Records < 100:    %d\n", one_hun);
      fprintf(fp, "Number Records 100-199:  %d\n", two_hun);
      fprintf(fp, "Number Records 200-299:  %d\n", three_hun);
      fprintf(fp, "Number Records 300-399:  %d\n", four_hun);
      fprintf(fp, "Number Records 400-499:  %d\n", five_hun);
      fprintf(fp, "Number Records 500-599:  %d\n", six_hun);
      fprintf(fp, "Number Records 600-699:  %d\n", seven_hun);
      fprintf(fp, "Number Records 700-799:  %d\n", eight_hun);
      fprintf(fp, "Number Records 800-899:  %d\n", nine_hun);
      fprintf(fp, "Number Records 900-999:  %d\n", thousand);
      printf("Number Records < 100:    %d\n", one_hun);
      printf("Number Records 100-199:  %d\n", two_hun);
      printf("Number Records 200-299:  %d\n", three_hun);
      printf("Number Records 300-399:  %d\n", four_hun);
      printf("Number Records 400-499:  %d\n", five_hun);
      printf("Number Records 500-599:  %d\n", six_hun);
      printf("Number Records 600-699:  %d\n", seven_hun);
      printf("Number Records 700-799:  %d\n", eight_hun);
      printf("Number Records 800-899:  %d\n", nine_hun);
      printf("Number Records 900-999:  %d\n", thousand);
        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 ((time_t *)NULL));
    }
    
    /* get a random number between 1 and maximum_value passed in */
    int getRandom(int maximum_value)
    {
      int random = 0;
      random     = rand();
      random = (random % maximum_value) + 1;  /* Modulus gives you a value between 0 and 1 less then number dividing by, so add 1 to shift */
      return(random);
    }

  15. #30
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like so
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define DATA_FILE "test_data.txt"
    
    void initializeRandom(void);    /* programmer written functions - prototypes */
    int getRandom(int maximum_value);
    
    int main(void)
    {
        int ranges[10] = { 0 };
        int rand_value = 0;
        int max = 0;
        int min = 1000;
        int user_input = 0;
        int i = 0;
        int number_values = 0;
        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);
    
        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]++;
        }
        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);
        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);
    }
    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.

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