473,788 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1926
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(NTR IALS, 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
2757
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 storing products in
6
3026
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 an html authoring tool to help you accomplish? 2. What do you expect from online help for a html authoring tool? 3. What audience do you think a freeware html authoring tool is directed towards?
6
391
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 hides all of them including hiding the database window, disabling menu changes. What I am after is the same effect as disabling all the check boxes in startup which still leaves 'File', 'Edit', 'Insert','Records','Window' &'Help'. I want to do this...
4
2636
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 type in a command line that runs the help in the correct Context from a button on each form. It all worked fine - HERE. The problem is that when I sent it out to a site, the help file was not able to be accessed because it was my path in the...
7
3306
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
23
3287
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 to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
16
2013
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 learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
3
3740
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 lot different from what the pros around here use. I really need help with debugging some program errors more than anything, even though my coding style might not be perfect. Anyway here is my code. About the only things that work right are...
1
2291
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 button the list is populated with a list of that genre. I have got the interface done to satisfaction, my problem is that when i press the change genre button nothing happens and when i select a track to play from the list which is setvisible and...
10
3366
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 the worst I ever seen. I almost cannot find anything I need, including things I
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.