Help with code

B

bb91915

I am taking a beginners class in C and am attempting to write code for
extra problems my instructor gave us. These are not for grade and are
only used solely for practice. I have a hard time taking pseudocode
and turning it into code. Since I am in a beginners class, we have
only learned so much to date, so what I am presenting will be really
rough, so please pardon my ignorance.

Here are my instructions, with my code to follow:

Using the Borland IDE, design, enter, compile, link, debug, and run a
program called flip.c. Flip.c is a fair coin tossing simulator. You
must use a while loop, a do while loop, a for loop, an array, a
#define, and a user defined function in this program.

Flip.c will do the following 20 times and record the results:
- Use a random number generator to simulate tossing a fair coin 50
times.
- Record the number of heads and tails in two arrays.
- Print the results of the 20 trials in an eye pleasing format

use four #inlcude to include stdio.h, conio.h, time.h, stdlib.h
use #define to create a literal NTOSSES whose value is 50
use #define to create a literal NTRIALS whose value is 20

define int arrays head[NTRIALS] and tail[NTRIALS]

create a prototype for DoTrial(int trial)
create a prototype for PrintTrials()

main()
{
use clrscr() to clear the screen
use randomize() to initialize the random number generator

within a do while loop call the function DoTrial() to toss the coin
NTOSSES times

call PrintTrials() to display the results

return 0

}

void DoTrial(int trial)
{
within a for loop, toss a coin 50 times and record the results in the
trial element of the
head and tail arrays
}

void PrintTrials()
{
within a while use printf to print the head and tails arrays, one
head and one tail per line
}

end of program

*********************************************************************************************************
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>

#define NTOSSES 50
#define NTRIALS 20

int head[NTRIALS];
int tail[NTRIALS];

void DoTrial(int trial);
void PrintTrials();

main()

{
clrscr();
randomize();

do
{
void DoTrial(int trial);

} while (NTOSSES < 50);

PrintTrials();
return 0;
}

void DoTrial(int trial)
{

int toss = 0;
for (toss = 0; toss < 50; toss++)


}

void PrintTrials()
{

printf(" Number of Heads Number of Tails");
printf("\t%d\t%d", head[NTRIALS],tails[NTRIALS]);
printf("\n");

}
 
P

pete

bb91915 said:
I am taking a beginners class in C and am attempting to write code for
extra problems my instructor gave us. These are not for grade and are
only used solely for practice. I have a hard time taking pseudocode
and turning it into code. Since I am in a beginners class, we have
only learned so much to date, so what I am presenting will be really
rough, so please pardon my ignorance.

Here are my instructions, with my code to follow:

Using the Borland IDE, design, enter, compile, link, debug, and run a
program called flip.c. Flip.c is a fair coin tossing simulator. You
must use a while loop, a do while loop, a for loop, an array, a
#define, and a user defined function in this program.

Flip.c will do the following 20 times and record the results:
- Use a random number generator to simulate tossing a fair coin 50
times.
- Record the number of heads and tails in two arrays.
- Print the results of the 20 trials in an eye pleasing format

use four #inlcude to include stdio.h, conio.h, time.h, stdlib.h
use #define to create a literal NTOSSES whose value is 50
use #define to create a literal NTRIALS whose value is 20

define int arrays head[NTRIALS] and tail[NTRIALS]

create a prototype for DoTrial(int trial)
create a prototype for PrintTrials()

/* BEGIN flip.c */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NTOSSES 50
#define NTRIALS 20

void DoTrial(int trial, int *heads, int *tails, int tosses);
void PrintTrials(int trials, int *heads, int *tails);

int main(void)
{
int head[NTRIALS];
int tail[NTRIALS];
int trial;

srand((unsigned)time(NULL));
for (trial = 0; NTRIALS > trial; ++trial) {
head[trial] = tail[trial] = 0;
DoTrial(trial, head, tail, NTOSSES);
}
PrintTrials(NTRIALS, head, tail);
return 0;
}

void DoTrial(int trial, int *heads, int *tails, int tosses)
{
int toss;

for (toss = 0; tosses > toss; ++toss) {
if ((rand() & 64) != 0) {
++heads[trial];
} else {
++tails[trial];
}
}
}

void PrintTrials(int trials, int *heads, int *tails)
{
int n;

puts("Trial Number of Heads Number of Tails");
for (n = 0; trials > n; ++n) {
printf("%2d %d %d\n",
n + 1, heads[n], tails[n]);
}
}

/* END flip.c */
 
B

bb91915

Pete,

Thanks, it does exactly what it was supposed to do, however, we are not
at this level yet in class, but I will dissect it so that I know what
every line of code is doing.

Thanks again,

Bryon
 
P

pete

bb91915 said:
Pete,

Thanks, it does exactly what it was supposed to do,
however, we are not
at this level yet in class, but I will dissect it so that I know what
every line of code is doing.

I hope it's not too difficult.
If you have further questions, feel free to ask.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,473
Latest member
pioneertraining

Latest Threads

Top