CS 210 – Fundamentals of Programming I  Fall 2011 – In­class Exercise for 08/24/2011

advertisement
CS 210 – Fundamentals of Programming I Fall 2011 – In­class Exercise for 08/24/2011
This handout is a tutorial on how to write a program using the C language using the CodeBlocks IDE under Windows 7 at the University of Evansville. It assumes that the user is logged into ACENET in KC­267. A link is provided on the course webpage for downloading CodeBlocks on your own computer.
Setting up CodeBlocks
CodeBlocks is launched by going to the Windows menu, All Programs, 02 Programming, then CodeBlocks. In the Compiler auto­detection dialog, choose 'GNU GCC Compiler' and click OK. Close the Tip of the Day, if any. For File associations, choose whichever you want and click OK. Close the Scripting console. You should maximize the window if it is not full size. (Note that many of the dialogs only happen once or can be turned off.)
You may need (or want) to configure some of the settings under the Settings menu. For this course, you must configure the compiler to "Enable all compiler warnings...[­Wall]". This is done by going to the Settings menu, selecting Compiler and Debugger, checking the appropriate box under the Compiler Flags tab, and clicking OK.
To start writing a new program in CodeBlocks, do the following steps: 1. To create a new project, just click on 'Create a new project' (or go to the File menu and choose New, then Project.) This gives you the dialog box to create a new project. Click on Console Application, then Go. This will bring up the Console application wizard. Click Next. Choose C for the language, then click Next. 2. For today's project, give it the title "tutorial". Browse to a folder to create the project in. It is suggested that you create a folder on your network drive for this course, e.g., "O:\CS 210". Select your UE network drive (O:), then click Make New Folder, give the new folder a name, and click OK. Note the resulting name of your project folder. Then click Next. (After the first time, just browse to this folder.)
3. The Compiler option should be set to GNU GCC Compiler. To speed up compilation, we'll only create a Debug version by unchecking 'Create "Release" configuration'. Then click Finish.
4. CodeBlock creates a "workspace" that will contain all of your CodeBlock projects. This is depicted in the Management window on the left as a hierarchy. The bolded project is the current project ("tutorial" in this case, since it is the only one). Clicking on the + next to Sources shows the main program file called "main.c". Double­click on main.c to bring it up in an edit window. The file contains the simple "Hello World" program. Writing a program Before sitting down to write a program, we should have a good idea of what we want to do, so here is an example of the full treatment from problem statement to execution. 08/22/2011
Page 1 of 4
D. Hwang
Problem Statement Write a program to read in an integer and print out the integer, its square, and its cube. Analysis & Design Analysis ­ what data is input, computed, or output? Objects
Type
Movement
Name
Input from console
int
input
n
Square of input
int
output
n_squared
Cube of input
int
output
n_cubed
Design ­ what are the steps to solve this problem? 1. Read input n from console
2. Compute the square of the input (n * n), and the cube of the input (n * n_squared) 3. Display the results ­ n, n_squared, n_cubed Implementation Finally, here is a C program implementing the above analysis and design. Note that the comments contain the analysis and design of this program.
/* File: main.c
Read in an integer and computes its square and cube.
Input:
An integer
Output: Its square and cube
-----------------------------------------------------------------Class: CS 210
Instructor: Dr. Deborah Hwang
Assignment: CodeBlocks tutorial
Date : August 24, 2011
Programmer(s): <fill in your name(s)>
*/
#include <stdio.h>
/* scanf, printf */
int main ()
{
/* Main Program Analysis */
int n,
/* Input – from console
n_squared,
/* Output - square of input
n_cubed;
/* Output - cube of input
*/
*/
*/
/* Main Program Design */
/* 1. Read input from the console */
printf ("Please enter an integer: ");
scanf ("%d", &n);
08/22/2011
Page 2 of 4
D. Hwang
/* 2. Compute square and cube of input*/
n_squared = n * n;
n_cubed = n * n_squared;
/* 3. Display results */
printf ("\nThe integer is %d\n", n);
printf ("Its square is %d\n", n_squared);
printf ("Its cube is %d\n", n_cubed);
}
return 0;
// end main
Note that the CodeBlocks Console application wizard has already put some of this code into the main.c file. Delete the "Hello World" line of code and type this program into the edit window (you may skip the comments ­ anything starting with /* to the following */). Note that this editor automatically indents the code as you type and uses different colors for some of the constructs in C. E.g., the comments are in green and the reserve words are in blue. When you are done typing in the program, save it by clicking on the Save (diskette) icon in the toolbar. For longer programs, you will want to do this fairly frequently, and always before you try to build your program. Building a program To build the program, click the Build (gear) icon on the toolbar (or type Ctrl+F9 or go to the Build menu and choose Build). This will compile and link all of the files in the project into an executable program. A window at the bottom of the screen should report on the progress being made during the build. If you have any syntax errors, the progress window will contain a list of errors and the line numbers. (You may have to scroll up to see them.) Clicking on an error message should cause the line with the error to be marked in the left column of the source edit window and the insertion cursor to be put at that line. Correct any errors, save the file, and build the project again. If the compilation phase is successful, an object file (with name <sourcefile>.obj, e.g. main.obj
for this tutorial project) will be created in the project folder under obj/Debug, then the system will link your program with the object files that make up the I/O library routines to create an executable program. The executable program file will be named <projectname>.exe, e.g. tutorial.exe for this tutorial project, and will be created in the project folder under bin/Debug. Running a program The integrated environment allows us to run the program from within CodeBlocks. Click the Run (rightarrow) icon on the toolbar (or type Ctrl+F10 or go to the Build menu and select Run). The system will execute your program. An command prompt window (as opposed to a window in the CodeBlocks system) will pop up and will display any output and accept any input for your program. 08/22/2011
Page 3 of 4
D. Hwang
When your program finishes executing, it will ask you to "press any key to continue". When you press a key, the command prompt window will disappear. (The code for this is added automatically by CodeBlocks when running under the IDE. It does not appear if you run the program explicitly in a stand­
alone command prompt window or if you launch the executable directly from Windows.) If you change the source file, you need to build the project again before running it. This can be done by clicking the Build and Run (gear+rightarrow) icon on the toolbar (or type F9 or go to the Build menu and selecting Build and Run). This will automatically save your changes before compiling and running your program. Run this program a few times using different values for input. Make sure it computes the correct results. Assignment
(5 points) Modify this tutorial program in the following ways:
1. Add an integer variable n_fourth
2. Add code to compute the number n raised to the fourth power (n_fourth)
3. Add code to display n_fourth to the screen
Compile and test your program until it runs correctly. Make sure your name is in the comment at the beginning of the file. When you have completed this exercise, print out the file and turn it in to the instructor. 08/22/2011
Page 4 of 4
D. Hwang
Download