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:
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 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:
//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
 
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
 
Back
Top Bottom