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
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.
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
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.
ok I did something like this...
but there something wrong with it....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); }
I am not sure how to put this into an array though...
What exactly is wrong with it? Getting error messages?but there something wrong with it....
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++; } }
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.
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
Post your entire source code as it is now.
this is my code right now...Originally Posted by sean_mackrory
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); }
> 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.
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....
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.
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); }
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.