I'd get really worried using any compiler which states "perhaps u should..."
Quzah.
I'd get really worried using any compiler which states "perhaps u should..."
Quzah.
Hope is the first step on the road to disappointment.
This is my latest program. I still couldnt run it after the second print statement.Code:#include <stdlib.h> #include <stdio.h> #include <DBOS\LIB.h> #include <CLIB.H> void count_nums(int *a, int range, int num); int my_rand(int); int get_max(int *a, int s); int get_min(int *a, int s); int main(void) { int range, num; /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */ date_time_seed(); /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */ int a[50]; char c; int s, i; printf(" * Please press r/R to run or press q/Q to exit *\n"); /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */ c = getch(); /*run by pressing r and exit by pressing q */ if (c == 'r' || c == 'R') { /*accept both lower and upper case */ /*move to next statment only if the input in range 1 to 20 */ do { printf("Enter the range size(max=20)"); scanf("%d", &range); } while (range < 0 || range > 20); printf("How many numbers do you wish to generate?"); scanf("%d", &num); count_nums(a, range, num); for (i = 0; i < range; i++) printf("%d:%d\n", i, a[i]); printf("Maximum count:%d\n", get_max(a, range)); printf("Minimum count:%d\n", get_min(a, range)); printf("Expected: %d\n", (int) (num / range)); } if (c == 'q' || c == 'Q') exit(1); return (0); } int my_rand(int range) { int r; date_time_seed(); r = 1 + rand() % range; return r; } void count_nums(int *a, int range, int num) { int i; for (i = 0; i <= num; i++) { num = my_rand(range); *(a + num) = 0; } } /* function to find the max */ int get_max(int *a, int s) { int i, max; max= *a; for (i = 0; i < s; i++) if (*(a + i) > max) max = *(a + i); return (max); } /*function to find the min */ int get_min(int *a, int s) { int i, min; min = *a; for (i = 0; i < s; i++) if (*(a + i) < min) min = *(a + i); return (min); }
Please help.
Thanks.
NiCoLeHa
This is my latest program. Please help me out. I dont seem to compile it. Please tell me where ive gone wrong and teach me what should i put instead.Code:#include <stdlib.h> #include <stdio.h> #include <DBOS\LIB.h> #include <CLIB.H> void count_nums(int *a, int range, int num); int my_rand(int); int get_max(int *a, int s); int get_min(int *a, int s); int main(void) { int range, num; /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */ date_time_seed(); /*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */ int a[50]; char c; int s, i; printf(" * Please press r/R to run or press q/Q to exit *\n"); /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */ c = getch(); /*run by pressing r and exit by pressing q */ if (c == 'r' || c == 'R') { /*accept both lower and upper case */ /*move to next statment only if the input in range 1 to 20 */ do { printf("Enter the range size(max=20)"); scanf("%d", &range); } while (range < 0 || range > 20); printf("How many numbers do you wish to generate?"); scanf("%d", &num); count_nums(a, range, num); for (i = 0; i < range; i++) printf("%d:%d\n", i, a[i]); printf("Maximum count:%d\n", get_max(a, range)); printf("Minimum count:%d\n", get_min(a, range)); printf("Expected: %d\n", (int) (num / range)); } if (c == 'q' || c == 'Q') exit(1); return (0); } int my_rand(int range) { int r; date_time_seed(); r = 1 + rand() % range; return r; } void count_nums(int *a, int range, int num) { int i; for (i = 0; i <= num; i++) { num = my_rand(range); *(a + num) = 0; } } /* function to find the max */ int get_max(int *a, int s) { int i, max; max= *a; for (i = 0; i < s; i++) if (*(a + i) > max) max = *(a + i); return (max); } /*function to find the min */ int get_min(int *a, int s) { int i, min; min = *a; for (i = 0; i < s; i++) if (*(a + i) < min) min = *(a + i); return (min); }
Thanks.
NiCoLeHa
At this point in your program, you're calling "date_time_seed", and you compiler has no idea where this function is, what it is, or anything about it, so it tells you that.Code:int main(void) { int range, num; /*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */ date_time_seed();
Your compiler is telling you that you're not allowed to call functions, and then after function calls, declare new variables. You need to declare all of your variables at the top of your functions before you call any other functions.Code:/*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */ int a[50]; char c; int s, i;
Your compiler has no 'getch' function, or, if it does, you're not including a header that defines it any place.Code:printf(" * Please press r/R to run or press q/Q to exit *\n"); /*!! foo.c:20: warning: implicit declaration of function ‘getch’ */ c = getch();
Well you don't actually loop or anything here, so while this will do whatever if they press r...Code:/*run by pressing r and exit by pressing q */ if (c == 'r' || c == 'R') { /*accept both lower and upper case */...it's going to exit here anyway, so the whole 'q' thing is pointless.Code:if (c == 'q' || c == 'Q') exit(1); return (0); }
Quzah.
Hope is the first step on the road to disappointment.
Question:
Write a program to show the operation of the random number generator.
The program will generate a specified number of random numbers in a given range.
It will count the number of times each number occurs.
It will display on the screen how many times each number occurred, in the form:
0 occurred 100 times
1 occurred 102 times
2 occurred 98 times
etc.
It will calculate the minimum number of occurrences, the maximum number of occurrences and the number of times each number could be expected.
e.g.
Minimum count =98, maximum count =102, expected count =100.
Use date_time_seed() and rand()
The prpgram will accept the following single keystroke commands from the keyboard ..
• ‘R’ to run the program, prompting for the number range and the number of numbers to generate. After running it prompts for either R or Q again.
• ‘Q’ to quit the program.
Do i make any mistake? I so, please tell me. I am handing it in on this Thursday.Code:#include <stdlib.h> #include <stdio.h> #include <DBOS\LIB.h> #include <CLIB.H> void count_nums(int *a, int range, int num); int my_rand(int); int get_max(int *a, int s); int get_min(int *a, int s); main(void) { int range, num; int s, i; char c; date_time_seed(); int a[50], r[50]; printf(" * Please press r/R to run or press q/Q to exit *\n"); c = getch(); /*run by pressing r and exit by pressing q */ if (c == 'r' || c == 'R') { /*accept both lower and upper case */ /*move to next statment only if the input in range 1 to 20 */ do { printf("Enter the range size(max=20)"); scanf("%d", &range); } while (range < 0 || range > 20); printf("How many numbers do you wish to generate?"); scanf("%d", &num); count_nums(a, range, num); for (i = 0; i < range; i++){ printf("%d:%d\n", i,r[i]); } printf("Maximum count:%d\n", get_max(a, range)); printf("Minimum count:%d\n", get_min(a, range)); printf("Expected: %d\n", (int) (num / range)); } if (c == 'q' || c == 'Q') exit(1); return (0); } int my_rand(int range) { //date_time_seed(); int i; int r[50]; int a[50]; for( i=0; i<50; i++) r[i]=0; r[a[i]]=r[a[i]]+1; //r = 1 + rand() % range; return (0); } void count_nums(int *a, int range, int num) { date_time_seed(); int i; for (i = 0; i <= num; i++){ //num = my_rand(range); a[i]= 0; a[i]= rand()% range; num= my_rand(range); } }
Thanks.
NiCoLeHa
Am i correct? Please help me to check. Tell me and guide me if i've made any mistakes.Code:#include <stdlib.h> #include <stdio.h> #include <DBOS\LIB.h> #include <CLIB.H> void count_nums(int *a, int range, int num); int my_rand(int); int get_max(int *a, int s); int get_min(int *a, int s); main(void) { int range, num; int s, i; char c; date_time_seed(); int a[50], r[50]; printf(" * Please press r/R to run or press q/Q to exit *\n"); c = getch(); /*run by pressing r and exit by pressing q */ if (c == 'r' || c == 'R') { /*accept both lower and upper case */ /*move to next statment only if the input in range 1 to 20 */ do { printf("Enter the range size(max=20)"); scanf("%d", &range); } while (range < 0 || range > 20); printf("How many numbers do you wish to generate?"); scanf("%d", &num); count_nums(a, range, num); for (i = 0; i < range; i++){ printf("%d:%d\n", i,r[i]); } printf("Maximum count:%d\n", get_max(a, range)); printf("Minimum count:%d\n", get_min(a, range)); printf("Expected: %d\n", (int) (num / range)); } if (c == 'q' || c == 'Q') exit(1); return (0); } int my_rand(int range) { int i; int r[50]; int a[50]; for( i=0; i<50; i++) r[i]=0; r[a[i]]=r[a[i]]+1; return (0); } void count_nums(int *a, int range, int num) { date_time_seed(); int i; for (i = 0; i <= num; i++){ a[i]= 0; a[i]= rand()% range; num= my_rand(range); } } /* function to find the max */ int get_max(int *a, int s) { int i, max; max= *a; for (i = 0; i < s; i++) if (*(a + i) > max) max = *(a + i); return (max); } /*function to find the min */ int get_min(int *a, int s) { int i, min; min = *a; for (i = 0; i < s; i++) if (*(a + i) <= min) min = *(a + i); return (min); }
Thanks.
NiCoLeHa