MC140.01 / MC140.02
Fall 2000
FAQ
last updated: Thu Oct 12 08:45:34 EDT 2000
PROJECT!!!
Assignment #5
Assignment #4
Assignment #3
Drawing a circle
Keeping the output window open
Submitting electronically
Handling infinite loops
Assignment #2
Assignment #1
--> Reading a line of input with spaces in it.
This program contains a function
called readInput()
that you can paste into your
project.
This function reads characters from the keyboard and stores
them (including spaces) into a single string.
When a newline character is read (i.e, you press the
<Enter> key), it stops reading input and returns the
whole string (including spaces, but not including the
newline character).
You may find this function useful for doing projects 2, 3
and 4.
--> Printing integers with leading zeros.
This program demonstrates how to print integers with leading zeros. Refer to page 363 in the textbook for further information.
(1) Initializing the random number generator.
You should insert the following code in your program to initialize
the random number seed, so that every time you run your program,
you will get a different sequence of "random" numbers.
#include <stdlib.h> #include <time.h> . . . void main() { // declare variables local to main time_t *t = NULL; // initialize random number seed srand( (unsigned int) time( t )); . . . } /* end of main() */Now you can make calls to the
rand()
function inside
your program.
(2) Start here!
(3) CodeWarrior seems to be doing strange things with the modulo operator (%), as well as our old problems with integer division. So, when determining the suit and rank of each card in the "hand" argument to evaluate_hand(), try something like this:
suit_index = (int)( (float)hand[i] / 13 ); rank_index = hand[i] - suit_index*13;
Submission.
As usual, submit your program electronically. Not as usual, you will need to submit TWO programs, one for question 1 and one for question 2. So you should deposit TWO files in the professor's folder, named as follows:
e.g. sklarel-ass4-1.c sklarel-ass4-2.cBring a hardcopy to class of both programs. Please staple loose sheets together. Thank you.
In general, I highly encourage you to do the math first to get each point (x,y) that
you want to plot, in graphics coordinates. Then convert the (x,y) to (xw,yw),
in windows coordinates and mark the grid:
xw = x + (MAX-1)/2; yw = (MAX-1)/2 - y; grid[yw][xw] = '*';Once you determine the (x,y) for each point, these three steps (above) WILL BE THE SAME FOR EVERY POINT!
For the hypothenuse of the triangle, you may have problems where the division
calculation for the slope is being converted to an integer -- especially when
the width of your triangle is larger than the height, then the slope becomes 0.
Here's how to fix this, using the coercion operators we talked about in class:
y = (int) ( ( (float)(-height)/(float)width ) * x + height );This way, the result of your division will be a real number.
In lecture for section 1 today (Friday, 22 Sep), we tried to draw a circle and experienced a strange problem with formatting the output. The problem actually had nothing to do with the programming! Here's the story.
When using ASCII graphics, you need to use a fixed-width font. Computer screens are essentially grids, or matrices, of points of light called "pixels". If you look at the settings on your Display control panel of your windows computer (Start - Control Panel - Display - Settings), you'll see that the "Desktop Area" is defined in pixels. Mine says "800 by 600 pixels". This means that the screen is 800 pixels wide and 600 pixels high.
Fonts are defined in pixels, where each character fills up a rectangular space that is some number of pixels wide and some number of pixels high. Some fonts have fixed width, which means that every character is drawn in a rectangle of the same width. Other fonts have variable width, which means that every character is drawn in a rectangle whose width varies with the shape of each character. For example, the character "i" is narrower than the character "w".
Today in class, I had the CodeWarrior editor set to use the font called "Georgia". This is a variable width font. In this font, the star character ("*") is drawn in a rectangle that is actuallytwice as wide as the rectangle used to contain the space character. So the fact that the rows of stars were right-justified was purely coincidental, due to the fact that I'd chosen to make a circle where each successive row contained two more (or less) stars than the previous row.
Take a look at circle.c. Download it to CodeWarrior and put it in a package. In CodeWarrior, select "Edit - Preferences", then click on "Editor - Fonts & Tabs". Set the font to "Georgia", and you'll see that the first circle in the source file looks right, while the second circle looks funny. Set the font to "Courier", and you'll see that the second circle looks off, while the first circle looks funny.
When you run the program, your output window should use a fixed-width font, so the fixed-width circle will look correct.
We will grade these programs on a Linux terminal, using a fixed-width font. So I strongly recommend that you use "Courier" as the font in your editor and only work with a fixed-width font.
Feel free to use the second example in circle.c as the basis for drawing a circle for assignment #2.
As mentioned in class today, you MUST do the following (or you will lose points):
(1) Comment your code. This includes a header that contains your name, the date, your section (of MC140) and the assignment number.
(2) Use the specified file name: <your-user-name>-ass2.c
Substitute your BC user name for <your-user-name>.
For example, my file name would be: sklarel-ass2.c
(3) Submit your file electronically, into the professor's folder on the OCF federation server.
(4) Bring a hardcopy (printout) of your source file (e.g. sklarel-ass2.c) to class on Monday (25 Sep).
In section 1 on Wednesday (20 Sep), we created a program that contained
an infinite loop.
Remember, your program will repeatedly execute the code inside the body of a
for loop and a while loop until the end condition for the loop has been met.
For example, when we say:
while ( option != 'Q' ) { // body of while loop } /* end of while */This while loop will execute the code in its body repeatedly, until the condition ( option != 'Q' ) evaluates to false. If, inside the body of your while loop, you don't make any provision to change the value of option, then it will never become equal to 'Q', and hence the loop will repeat for an infinite number of times.
If something like this happens, you will have to kill your program. This means you have to use Windows to stop your program running. You can do this by closing your program's output window. You can also do this by pressing the "Ctrl - Alt - Delete" keys simultaneously, at which point you will be able to look at the Windows Task Manager and tell it to end the CodeWarrior task.
However, after you do this, you may experience subsequent problems compiling your program because CodeWarrior will not want to over-write your application file because it thinks it is still in use (even though it isn't because you have already stopped the program running). In order to reset this condition, you will probably have to log off of Windows and log on again.
When you submit your program, be sure to submit only your source file!!!
(i.e. the file whose name ends in ".c", not the CodeWarrior project file,
whose name ends in ".mcp").
This file should be named <your-user-name>-ass<number>.c,
where you substitute your BC user name for <your-user-name>
and the current assignment number for <number>.
For example, for assignment 1, my source code file would be called
sklarel-ass1.c
Please use only lower case characters!
If you are in the OCF, open the Professor's folders (from the Start Menu on a PC or from the Apple Menu from a MAC). Double click the folder with my user name (sklarel). Then drag <your-user-name>-ass1.c into the appropriate MC140 folder (MC140.01 for MWF 1pm or MC140.02 for MWF 2pm).
If you are in a dorm, select "Start - Find - Computer". Specify "federation" as the name of the computer to find. When it appears, click on the computer icon next to federation to open it. Find the ocf_prof folder and double click on that to open it. Then find my folder, called sklarel and double click on that to open it. Then drag <your-user-name>-ass1.c into the appropriate MC140 folder (MC140.01 for MWF 1pm or MC140.02 for MWF 2pm).
When running my program, how do I keep the application window open so I can see what the program did?
There are two ways to do this.
(1)
When creating a new project, after selecting
"File - New - Win32 C/C++ Application Stationery" and entering values in the
"Project name" and "Location" boxes and clicking on "OK", a window will pop up
with some choices.
Instead of selecting "C Console App",
select "WinSIOUX C App" and click on "OK".
This will use a library that writes its output to a text window, and you will
have control over when that window closes. It's not elegant, but it works.
(2)
At the end of your main program, before the statement
return 0;put in the following statement:
getchar();Here's an example:
#include <stdio.h> int main( void ) { printf( "hello world!\n" ); getchar(); return( 0 ); }
(1) In step 1 (Create the source code), the empty "#include" statement should really read as follows:
#include <stdio.h>
(2) In step 3 (Modify it), the phrase "echo the user's output to the screen" should say "echo the user's input to the screen."