473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with code

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");

}

Nov 16 '05 #1
3 1913
bb91915 wrote:

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 */

--
pete
Nov 17 '05 #2
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

Nov 17 '05 #3
bb91915 wrote:

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.

--
pete
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
3
by: inkexit | last post by:
I need help figuring out what is wrong with my code. I posted here a few weeks ago with some code about creating self similar melodies in music. The coding style I'm being taught is apparently a...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.