C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-17-2005, 10:48 PM   #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...
kamisama is offline   Reply With Quote
Old 02-18-2005, 03:51 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 02-18-2005, 11:09 AM   #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....
kamisama is offline   Reply With Quote
Old 02-18-2005, 12:48 PM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 02-18-2005, 01:12 PM   #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-19-2005 at 12:42 AM. Reason: comment wrapping
kamisama is offline   Reply With Quote
Old 02-18-2005, 11:26 PM   #6
Registered User
 
Join Date: Feb 2005
Posts: 22
still need help....
kamisama is offline   Reply With Quote
Old 02-19-2005, 12:56 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 02-19-2005, 01:08 AM   #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.
anonytmouse is offline   Reply With Quote
Old 02-19-2005, 10:31 AM   #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.
kamisama is offline   Reply With Quote
Old 02-19-2005, 10:37 AM   #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.
kamisama is offline   Reply With Quote
Old 02-19-2005, 10:43 AM   #11
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 02-19-2005, 11:07 AM   #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
kamisama is offline   Reply With Quote
Old 02-19-2005, 11:18 AM   #13
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 02-19-2005, 12:52 PM   #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??
kamisama is offline   Reply With Quote
Old 02-19-2005, 12:56 PM   #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
andyhunter is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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