Counting number from a random file

This is a discussion on Counting number from a random file within the C Programming forums, part of the General Programming Boards category; Hi!! I am a newbie at C programming, and I need help wih my program... I am suppose to write ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    22

    Counting number from a random file

    Hi!! I am a newbie at C programming, and I need help wih my program...
    I am suppose to write a C program that opens up the generated file and produces a report that looks like:
    Number of records : you determine the value and print it here
    Maximum Value : you determine the value and print it here
    Minimum Value : you determine the value and print it here

    so far I have 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  min = 0;
      int  max =0;
      int  count = 0;
      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];
      int rand_value = 0;
      FILE *fp;
    
      /* 1.  Get user input */
      printf("Enter your colleague ID # --> ");
      scanf("%d", &user_input);
    
      fp = fopen(DATA_FILE, "r");
      /* Generate a string out of the value inputted by user */
      sprintf(id_string, "%07d", user_input);
      strncpy(number_string, &id_string[4], 3);
      number_values = atoi(number_string);  
      initializeRandom();
    //printf("%s results %s is %d \n", id_string, number_string, 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>i)
    
               max = rand_value;
    
         if(min< rand_value)
             min =rand_value;
        if (max >0)
         printf("This is the max %d\n", max);
         printf("This is the min %d\n", min);
    
    
        }
    
    
    
    
      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);
    }
    I can't seem to get the max and min value... and I dont know how to count the total number of values...

    So can someone help me please...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Think about what initial value you have to set min and max to, to make your comparisons succeed on the first pass?

    Or simply read the first record and set min/max to that value, then enter the loop.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    I am still stuck...
    all it did was generate the random list... but the maximum number is totally wrong....

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Posting your latest code usually helps.
    Or at least helps us make a better attempt at helping you.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    ok...
    this is what I have now...

    it generates a random list...
    suppose to count the number of files on that list...

    and print the max and min only once...
    but somehow it prints until the list is ended and the max is totally off...

    Like I said... I just started with C, so I m still new to this...
    I want to write it in an array... and then loop it from there

    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 count = 0;
      int var = 0;
      int max =0;
      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];
      int rand_value = 0;
      FILE *fp;
    
      /* 1.  Get user input */
      printf("Enter your colleague ID # --> ");
      scanf("%d", &user_input);
      fp = fopen(DATA_FILE, "r");
    
     /* Generate a string out of the value inputted by user */
      sprintf(id_string, "%07d", user_input);
      strncpy(number_string, &id_string[4], 3);
      number_values = atoi(number_string);  
      initializeRandom();
      //printf("%s results %s is %d \n", id_string, number_string, number_values);
       for (i=0; i < number_values; i++)
      {
        rand_value = getRandom(999);
        printf("This is the number of values: %d\n", number_values);
        fprintf(fp, "%d\n", rand_value);
        printf("%d\n", rand_value);
    
    
    
      if(rand_value > max)
        max =rand_value;
      if(max> 0)  {
        printf("this is the max value %d\n", max);
                  }
      }
      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);
    }
    Last edited by Salem; 02-18-2005 at 11:42 PM. Reason: comment wrapping

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    still need help....

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Code:
      printf("Enter your colleague ID # --> ");
      scanf("%d", &user_input);
      fp = fopen(DATA_FILE, "r");
    
     /* Generate a string out of the value inputted by user */
      sprintf(id_string, "%07d", user_input);
      strncpy(number_string, &id_string[4], 3);
      number_values = atoi(number_string);
    1. You're opening the file with mode "r", but then you try and write to it?
    Well that isn't going to change your file is it.
    2. What is the point of all that sprintf / strncpy / atoi code?
    You convert the number typed in into a string, chop a bit off and convert back to an integer?
    If you really wanted to do modulo 1000 on it, it would be far simpler to just write
    Besides, you don't \0 terminate the temporary string which atoi() uses (who knows what result you get).

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

    number_values = user_input % 1000;
    Replaces all that code if all you wanted was to trim the user input.

    Or maybe
    number_values = user_input;
    if ( user_input >= 1000 ) number_values = 1000;

    > printf("This is the number of values: %d\n", number_values);
    Put this before the for loop - so you don't clutter the output.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
      if(max> 0)  {
        printf("this is the max value %d\n", max);
                  }
    Put this after the end of the loop.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    Ths string I used is to limit the number of results ....
    the user can input up to 7 numbers... but it will only take 3....
    for Ex. 1234567 is the input number the user, it will only take 567.
    After that it will generate 567 numbers from 0 to 999.
    Quote Originally Posted by Salem
    Code:
      printf("Enter your colleague ID # --> ");
      scanf("%d", &user_input);
      fp = fopen(DATA_FILE, "r");
    
     /* Generate a string out of the value inputted by user */
      sprintf(id_string, "%07d", user_input);
      strncpy(number_string, &id_string[4], 3);
      number_values = atoi(number_string);
    1. You're opening the file with mode "r", but then you try and write to it?
    Well that isn't going to change your file is it.
    2. What is the point of all that sprintf / strncpy / atoi code?
    You convert the number typed in into a string, chop a bit off and convert back to an integer?
    If you really wanted to do modulo 1000 on it, it would be far simpler to just write
    Besides, you don't \0 terminate the temporary string which atoi() uses (who knows what result you get).

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

    number_values = user_input % 1000;
    Replaces all that code if all you wanted was to trim the user input.

    Or maybe
    number_values = user_input;
    if ( user_input >= 1000 ) number_values = 1000;

    > printf("This is the number of values: %d\n", number_values);
    Put this before the for loop - so you don't clutter the output.

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    I did what you say but it ended up printing only the last number from the list...
    this is the ouput file:
    Code:
    763
    173
    228
    544
    761
    4
    15
    888
    171
    753
    507
    567
    163
    428
    130
    343
    913
    296
    896
    553
    288
    139
    725
    652
    702
    166
    400
    623
    395
    710
    7
    575
    126
    210
    264
    861
    141
    229
    566
    24
    615
    27
    982
    714
    7
    799
    226
    697
    335
    531
    3
    73
    631
    163
    3
    30
    355
    221
    321
    470
    34
    413
    954
    24
    325
    140
    993
    368
    942
    415
    934
    797
    862
    549
    337
    948
    831
    112
    247
    419
    21
    484
    28
    803
    31
    749
    195
    656
    275
    627
    312
    730
    975
    988
    339
    604
    394
    71
    301
    15
    917
    320
    647
    637
    424
    383
    782
    425
    763
    180
    150
    844
    762
    375
    518
    908
    198
    968
    689
    972
    539
    561
    648
    571
    911
    962
    779
    862
    489
    732
    793
    974
    535
    129
    152
    596
    140
    216
    461
    329
    658
    46
    214
    331
    81
    45
    345
    176
    965
    318
    997
    645
    346
    620
    133
    588
    859
    487
    418
    574
    291
    679
    403
    53
    592
    649
    422
    555
    171
    958
    434
    665
    493
    574
    642
    466
    619
    564
    903
    220
    494
    918
    354
    965
    24
    31
    891
    601
    958
    141
    521
    735
    407
    493
    589
    72
    213
    316
    755
    18
    805
    549
    692
    466
    572
    254
    168
    61
    745
    824
    113
    677
    67
    316
    920
    652
    219
    441
    578
    55
    874
    126
    481
    570
    568
    380
    734
    685
    642
    986
    576
    313
    857
    148
    550
    590
    722
    801
    643
    747
    155
    544
    21
    414
    95
    682
    583
    445
    310
    126
    367
    619
    197
    869
    17
    725
    31
    369
    147
    366
    453
    146
    834
    595
    662
    543
    646
    917
    99
    955
    49
    589
    665
    68
    196
    717
    712
    812
    554
    311
    395
    783
    562
    788
    245
    624
    170
    75
    376
    118
    459
    19
    143
    503
    201
    43
    84
    945
    745
    357
    777
    282
    690
    470
    23
    162
    734
    100
    979
    729
    767
    241
    500
    741
    This is the number of values: 314
    this is the max value 741
    as you can see 742 is not the maximum number....
    so thats what I m trying to find now...
    Quote Originally Posted by anonytmouse
    Code:
      if(max> 0)  {
        printf("this is the max value %d\n", max);
                  }
    Put this after the end of the loop.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    No idea - works as expected here
    Code:
    Enter your colleague ID # --> 11
    This is the number of values: 11
    284
    794
    54
    668
    707
    190
    476
    143
    602
    94
    111
    this is the max value 794
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    Really???
    What did you do??

    Quote Originally Posted by Salem
    No idea - works as expected here
    Code:
    Enter your colleague ID # --> 11
    This is the number of values: 11
    284
    794
    54
    668
    707
    190
    476
    143
    602
    94
    111
    this is the max value 794

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    I ran your code, modified with my comments.

    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 count = 0;
        int var = 0;
        int max = 0;
        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];
        int rand_value = 0;
    
        /* 1.  Get user input */
        printf("Enter your colleague ID # --> ");
        scanf("%d", &user_input);
    
        number_values = user_input;
        initializeRandom();
        //printf("%s results %s is %d \n", id_string, number_string, number_values);
        printf("This is the number of values: %d\n", number_values);
        for (i = 0; i < number_values; i++) {
            rand_value = getRandom(999);
            printf("%d\n", rand_value);
    
            if (rand_value > max)
                max = rand_value;
        }
        if (max > 0) {
            printf("this is the max value %d\n", max);
        }
        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);
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    22
    oh ok... I see what you did....
    Ok... my program now works after some modification... including the string part...
    this is what I have...

    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  rand_value    = 0;
      int  count         = 0;
      int  var           = 0;
      int  max           = 0;
      int  min           = 0;
      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);
        strncpy(number_string, &id_string[4], 3);
        number_values = atoi(number_string);
        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(max > 0)
      {
        printf("this is the max value %d\n", max);
        fprintf(fp,"this is the max value %d\n", max);
      }
    
    
    
        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);
    }

    I tried doing the min too... but the min print out the last number of the list for some reason...
    the min is something like this....

    Code:
      if(rand_value > max)
      {
        max = rand_value;
      }
      if (rand_value < max)
      {
        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);
      }
    it prints out the last number on the list...
    what am I doing wrong??

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Code:
    if (rand_value < max)
      {
        min = rand_value;
      }
    Now honestly, does this make any sense?

    If I were looking for a min value I would be checking to see if the value I was comparing was less then the min value, not the max value.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Page 1 of 3 123 LastLast
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

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