anyone good with C

Soldato
Joined
3 Mar 2008
Posts
3,147
Location
Canterbury, Kent
ive got this assignment and me nbeing shocking at any programming (apart from watch em on tv) cant for the life of me begin to work out the code for this! any help will be highly appreciated :)

Over a large number of trials, a “fair” random number generator will return each of the possible values approximately an equal number of times. Therefore in each set of 60 trials if values are generated in the range of 1 to 6, there should be about 10 occurrences of each number.

1. You are required to write a function that will generate 60 random numbers in the range 1 to 6. Over the 60 iterations you should count how many times each of the individual numbers occur. Your function should print out the number of times each number has been generated in the form:

Number 1 has been generated x times.
Number 2 has been generated y times.

.....etc, where x is the generation frequency of 1 and y is the generation frequency of 2....

2. You should write a main routine which calls your function 10 times.
 
Last edited:
Sounds fairly straight forward to me, but then I havent done any C for years. Get a book out that contains some basic functions and commands and you should have it done in a few hours. Getting people to do it for you is cheating! :p

PK!
 
Well C++ really, ive got this assignment and me nbeing shocking at any programming (apart from watch em on tv) cant for the life of me begin to work out the code for this! any help will be highly appreciated :)

Over a large number of trials, a “fair” random number generator will return each of the possible values approximately an equal number of times. Therefore in each set of 60 trials if values are generated in the range of 1 to 6, there should be about 10 occurrences of each number.

1. You are required to write a function that will generate 60 random numbers in the range 1 to 6. Over the 60 iterations you should count how many times each of the individual numbers occur. Your function should print out the number of times each number has been generated in the form:

Number 1 has been generated x times.
Number 2 has been generated y times.

.....etc, where x is the generation frequency of 1 and y is the generation frequency of 2....

2. You should write a main routine which calls your function 10 times.

Rather than us do your homework for you, why dont you tell us some of your thoughts? What bits of functionality will your program need? Whats the best way to write those bits of functionality? Which parts of the standard c/c++ libraries can you use to help you?
 
tell a lie it isnt c++ its just c phew, i aint asking for the answer! thats why i said any help lol my knowledge is very slim on the topic of programming, i have no resources such as books like you suggest but i do have a nice collection of notes i took during class lol
 
im guessing il need a 'string' containing the numbers one to six then some sort of code to call up each of these numbers randomly 60 times and mprint them out on the screen using the printf function?
 
I'm fairly sure (it's been 5+ years) C's random number generators allow you to specify a range of numbers, that should give you a clue.

Hint: Write it in pseudocode first
 
i dont want to randomly create numbers, i want to randomly pick numbers from a string of numbers, and do this 60 times
 
Last edited:
would i need a math.random class? could i use a for statement such as

for(i=0, i<60, i++)
choice = math.floor(math.Random()*6) //random
choice between 0 and 5
print(string[choice])

the for loop somehow choosing numbers from the string 60 times

very psuedo codey hehe
 
Last edited:
I'm fairly sure (it's been 5+ years) C's random number generators allow you to specify a range of numbers, that should give you a clue.

Hint: Write it in pseudocode first

No your incorrect,

ANSI rand() function

#include <stdlib.h>
i = rand();

i is 0 to RAND_MAX (where RAND_MAX is a manifest defined in <stdlib.h>).

You should use modular arithmetic to limit your rand() range.

Pseudocode;

I would use a map data structure for the frequency count:

Code:
i = 0; 
while (i < 60)
  key = generate number 1-6
  if (key in keys map)
    key.count++;
  else
    create new key
    key.count = 1;   
i++;
end while

Edit: Actually you only have 6 numbers, no point using a map you can just use a array[6]; and increment each value held when you get that number.
 
Last edited:
would i need a math.random class?

then that wouldn't be C :P

in pseudocode:

int array called count of size 6
set entire array to 0;
int temporary

loop through 60 iterations{
temporary= rand()%6
count[temporary]+=1
}

and now you have all the info you need in count[] to display. to loop through the program ten times, you'd just loop through the above loop ten times.

note: this will actually generate numbers between and including 0 and 5, instead of 1 and 6. but you can tweak the output instead of the program.
 
//A Program that generates random numbers in the range LOWER to UPPER, and loops for ITERATIONS times then prints the results. This is looped and executed for TIMES times

#include <stdio.h>

#define LOWER 1 //The lower limit of the random numbers to generate.
#define UPPER 6 //The upper limit of the random numbers to generate.
#define ITERATIONS 60 //The number of iterations of the random number calculator.
#define TIMES 10 //The number of times to call the function.

//Main function. Calls the diceRand() function TIMES times.

main()
{
int i;
for (i=0 ; i<TIMES ; i++)
{
diceRand();
}
}

//Iterates for ITERATIONS loops, producing numbers between LOWER and UPPER, then prints out the results.

diceRand()
{
int i;
int r[UPPER]; //Creates an array 'r' big enough to hold the range of numbers.

for(i=0 ; i<UPPER ; i++) { //Sets every element in the array to 0.
r=0;
}
for(i=0 ; i<ITERATIONS ; i++) { //Loops for ITERATIONS times and puts the numbers in their corresponding positiosn in the 'r' array.
r[getRand()-LOWER]++;
}

for(i=0 ; i<UPPER-LOWER+1 ; i++) {
printf("Number %i has been generated %i times.\n", i+LOWER , r); //Loops through the array of results and prints them out on screen.
}
printf("\n")
}

//Returns single random mumber in the range LOWER to UPPER.

int getRand()
{
return (rand() % UPPER)+LOWER ;
}


Hmmmm whats wrong with this code? 8 errors adn 1 warning? im sure its right though, in fact pretty adament, unless these textie books are worng lol
 
Its because you don't have any function prototypes (or your functions are in the wrong order). You need to make sure your function are declared before they are called. Quick fix is to add:

void diceRand();
int getRand();

At the top before your main function. You also need to change diceRand() to void diceRand() for proper ANSII C. You should also make main, int main() and return 0 on completion.

Your book is old if it does not tell you this.
 
yep quite an old book hehe right well ive got down to 1 error now, thanks for the help very useful, these problems only occur when using visual studio, my other compiler compiles with no problems and runs fine, its just they want it in visual studio, i wrote in notepad++ best tool ever!!!! well in the last line of my code "return (rand() % UPPER)+ LOWER) then it says it " 'rand' identifier not found? any clues as to what this can be????
 
//A Program that generates random numbers in the range LOWER to UPPER,
//and loops for ITERATIONS times then prints the results. This is looped and executed for TIMES times

#include <stdio.h>

#define LOWER 1 //The lower limit of the random numbers to generate.
#define UPPER 6 //The upper limit of the random numbers to generate.
#define ITERATIONS 60 //The number of iterations of the random number calculator.
#define TIMES 10 //The number of times to call the function.
void diceRand();
int getRand();

//Main function. Calls the diceRand() function TIMES times.
int main()
{
int i;
for (i=0 ; i<TIMES ; i++)
{
void diceRand();
}
}

//Iterates for ITERATIONS loops, producing numbers between LOWER and UPPER, then prints out the results.

void diceRand()
{
int i;
int r[UPPER]; //Creates an array 'r' big enough to hold the range of numbers.

for(i=0 ; i<UPPER ; i++) { //Sets every element in the array to 0.
r=0;
}
for(i=0 ; i<ITERATIONS ; i++) { //Loops for ITERATIONS times and puts the numbers in their
r[getRand()-LOWER]++; //corresponding positiosn in the 'r' array.
}

for(i=0 ; i<UPPER-LOWER+1 ; i++) {
printf("Number %i has been generated %i times.\n", i+LOWER , r);
}
printf("\n");
} //Loops through the array of results and prints them out on screen.

//Returns single random mumber in the range LOWER to UPPER.
int getRand()
{
return (rand() % UPPER)+LOWER ;
}

heres the code with just one error please feel free to use
 
yep quite an old book hehe right well ive got down to 1 error now, thanks for the help very useful, these problems only occur when using visual studio, my other compiler compiles with no problems and runs fine, its just they want it in visual studio, i wrote in notepad++ best tool ever!!!! well in the last line of my code "return (rand() % UPPER)+ LOWER) then it says it " 'rand' identifier not found? any clues as to what this can be????

You need to #include <stdlib.h> for rand() function. That is your error fixed.

The bit about main was just the correct way to do it.
Code:
int main(int argc, char *argv[])
{
  /* Your code here */ 


  return 0;
}

return 0 tells the OS that your code exited normally.
 
Back
Top Bottom