-
Random numbers
I couldnt run this program. Please help me out.
Question:
generate a random numbers in a given range
count the occurrences of each number and return the total in arry 'a'
find the maximum integer in the arry 'a' of size 's'
find the minimum integer in the arry 'a' of size 's'
and using function to find max and min
Please view attachemnt for my program.
thanks.
-
What are these?
Code:
#include <DBOS\LIB.h>
#include <CLIB.H>
I've never heard of either of these. I also see that you're using the rand() function without time.h, so if one of these has a rand() function, I'd suggest using a more standard library. You're also passing an unitialized variable "s" to your functions. I can't really say more cause I can't compile this without your libraries. I'll make some changes to work with time.h, and I'll assume that date_time_seed() is similar to srand().
-
#include <DBOS\LIB.h>
#include <CLIB.H>
I was told to use those. I am compiling using Salford plato.
-
Please help!!
Please help me check my prgram. I dont seem to run it.
Please view attachment.
Many thanks.
-
Please don't bump your thread with almost the exact same code. As I tried to imply, there isn't much anyone can do without those non-standard libraries, so you could either include them in an attachment or explain what the functions in them do.
-
Why do people insist on attaching their .c files instead of just pasting the code in?
Quzah.
-
your programe logic is not correct, u are checking the array for integers with no values in the array where u get the undefined values.
what this variable S is doing there. u sending the variable S to the function with no value in it again. which again ends up with the undefined result
ssharish2005
-
> Why do people insist on attaching their .c files instead of just pasting the code in?
Dunno, maybe they haven't figured out code tags, or because it's an unsightly mess. People just make it too hard on themselves by writing code in such an undisciplined way.
Here's the code - formatted by muggins here - with some comments as to what is going wrong
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));
/*!! foo.c:37: warning: ‘s’ is used uninitialized in this function */
printf("Minimum count:%d\n", get_min(a, s));
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, num; /*!! foo.c:67: warning: ‘max’ may be used uninitialized in this function */
num == s; /*!! foo.c:68: warning: statement with no effect */
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);
}
I'm guessing date_time_seed(); is one of those magic functions in your schools library code - but in any event you need to only call it once (and never in my_rand).
See the FAQ - reseeding the rand() generator just makes it return a constant.
-
salem,
thanks for ur help. I tried to recompile the program. However, it still couldnt work. Can someone please help me out?
Thanks.
-
Well did you fix the warnings about uninitialised variables?
> it still couldnt work
Be more specific.
Does it compile?
Does it run, or crash
Does it produce the expected output, how does that differ from what you expect?
-
Yea,
I did fix those variables. However it still cannot compile. The first two print parts did what it should do. But not after that. It doesnt produce the expected output. =(
-
You did put these back in right?
/*#include <DBOS\LIB.h>*/
/*#include <CLIB.H>*/
Also, paste actual error messages.
-
Yes i did. What was wrog was that, they said
1) cannot compile.......- array on line 45 which is something inside the function void count-nums,
2) date_time_seed- i removed as u said, but warming regarding that was detected.
3) in int get_max, ive initialised the variables but they still say my varaibles are undefined. Can u please show me how to go about getting the max. ?
Thanks.
-
> 1) cannot compile.......- array on line 45 which is something inside the function void count-nums,
When I said paste, I meant paste directly what the compiler tells you, not what you think it said.
That said, there's another problem I just spotted in count_nums
> for (i = 0; i <= num; i++);
The ; at the end means the loop does nothing, except count.
Then when it exits, you do ONE rand() and assign it to some location past the end of your array.
> 3) in int get_max, ive initialised the variables
It should look exactly like get_min, except for the obvious swap of a < for a >
-
error 313- function date_time_seed has not been declared( perhaps u should #include <DBOS\LIB.h>
error 313_function getch() has not been declared(perhaps u should #include <CLIB.h>)
Please help. Is my function under viod_count_nums correct?
-
I'd get really worried using any compiler which states "perhaps u should..."
Quzah.
-
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);
}
This is my latest program. I still couldnt run it after the second print statement.
Please help.
Thanks.
-
Please help me!!
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);
}
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.
Thanks.
-
Code:
int main(void)
{
int range, num;
/*!! foo.c:14: warning: implicit declaration of function ‘date_time_seed’ */
date_time_seed();
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:
/*!! foo.c:15: warning: ISO C90 forbids mixed declarations and code */
int a[50];
char c;
int s, i;
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:
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();
Your compiler has no 'getch' function, or, if it does, you're not including a header that defines it any place.
Code:
/*run by pressing r and exit by pressing q */
if (c == 'r' || c == 'R') { /*accept both lower and upper case */
Well you don't actually loop or anything here, so while this will do whatever if they press r...
Code:
if (c == 'q' || c == 'Q')
exit(1);
return (0);
}
...it's going to exit here anyway, so the whole 'q' thing is pointless.
Quzah.
-
Please help to check
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.
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);
}
}
Do i make any mistake? I so, please tell me. I am handing it in on this Thursday.
Thanks.
-
THe whole of my program.
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);
}
Am i correct? Please help me to check. Tell me and guide me if i've made any mistakes.
Thanks.