Tuesday, July 28, 2009

How should I write this program?

My school just gave me an assignment in writing a program using C compiler but I have no idea how to do it...


It goes like this :


Write a number guessing game program which generates the number randomly. When you finish the program, it should look like this:





Number of players:2 %26lt;--(lets say there are 2 players)


Player's Name:__


__





Scores:


(player 1's name): %26lt;--(points are added for every winning game)


(player 2's name):





(Assuming random generated number is 555)


Number starts at:100


Number ends at :1000





What is the number?


(player 1's name):896


Wrong. It is %26lt;896


(player 2’s name):400


Wrong. It is %26gt;400


(player 1’s name):555








Bingo! The answer is 555. (player 1’s name) wins this game.





Continue(Press C) or Exit(Press E)?








Can anyone help me please?

How should I write this program?
This is very simple. The only thing you need is a good random number generators. There have been hundreds of books and articles written about the best way to go about generating random numbers, but based on the type of assignment I have seen so far - the simplest algorithm would be OK.





There are some really slick implementation of random number generators, my favorite are:


http://www.qbrundage.com/michaelb/pubs/e...





But of course if you want to be lazy, you can just use the system rand() function. More info can be found here:


http://members.cox.net/srice1/random/cra...





Next, figure out how to design you IO. Here is a snipplet of a book that will take you to the right direction:


http://www.cs.utah.edu/~zachary/isp/tuto...





Ohh and here are some really useful C snipplets:


http://snippets.dzone.com/tag/c





Explore your possibilities and have fun. This is a really interesting assignment and it makes me want to go back to school.





Ohh and of course, you are going to need a C compiler. Here is a good one:


http://www.crossware.com/trialware/21499...





You can download the trial there or get a free version of another C compiler:


http://www.pitt.edu/~stephenp/misc/downl...





Remember to have fun!
Reply:I suggest you to always solve your programming assignment your self. So I am giving you some hints. First you need a random number generating function, which is provided as library function in the header file math.h. Your algorithm should follow as:





number =(int) random() * 1000;// generates the random number between 0 to 999


Find whose turn is it?


Print the player's name with turn and display the prompt on screen as " Enter you guess",


if the entered number and the generated number are equal, the display the congrats!! message. and add appropriate point to the user.


else, if(the guessed number is less than the generated number)


display appropriate message and display prompt for next user.
Reply:I think you will need to do this to learn but I will give you a few hints.





First I will assume that this is C not C++ the languages are simular but console input/output is different.





Use printf to output the data and scanf to get it


look up random number generation (don't forget to seed the generator with the timer). Set up a while or do until loop. You will need an if to do the comparison remember to use the == operator for the comparison.


No comments:

Post a Comment