CS 210 – Fundamentals of Programming I Spring 2012 – In­class Exercise for 01/11/2012 & 01/12/2012 This handout includes instructions on how to write a program using the C language using the CodeBlocks IDE under Windows 7. The code from this exercise is to be submitted to the automated testing system as demonstrated in class. You must successfully submit your program before leaving class. Setting up CodeBlocks In KC­267, CodeBlocks is launched by going to the Windows menu, then All Programs, then 02 Programming, then CodeBlocks. The first time CodeBlocks is run, several dialogs will appear. 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 (default choice is okay) and click OK. Close the Scripting console. You should maximize the window if it is not full size. (Note that many of the dialogs 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, you 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. Give your project a title, e.g., "inclass­01­11" (or "inclass­01­12"). Browse to a folder to create the project in if not already set. It is suggested that you create a folder on your network for this course, e.g., "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 Debug version by unchecking Create "Release" configuration. Then click Finish. 4. CodeBlock creates a "workspace" that will contain your CodeBlock project. This is depicted in the Management window on the left as a hierarchy. The bolded project is the current project ("inclass­01­11" 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. 5. 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. 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 grey and the reserve words are in blue. 01/10/2012 Page 1 of 3 D. Hwang Assignment (10 points) Write an analysis, design and program implementation for the following problem statement. Successfully submit this program to the automated testing system. Problem Statement Write a program that calculates mileage reimbursement for a salesperson at a rate of 45 cents per mile. Your program should interact with the user in the following manner (user input in bold): MILEAGE REIMBURSEMENT CALCULATOR Enter the beginning odometer reading: 13505.2 Enter the ending odometer reading: 13810.6 The distance traveled is 305.4 miles. At $0.45 per mile, the reimbursement is $137.43. Analysis & Design As demonstrated in last class, we will write the analysis and design for this program directly into the code file. Concepts necessary to complete this task will be covered during lecture. Implementation After developing the analysis, constant and variable declarations will written during the lecture. After developing the design, coding techniques necessary to complete this task will be covered during the lecture. Building a program When you are done typing in the program, save it by clicking on the Save (diskette) icon in the toolbar. For longer programs, you want to do this fairly frequently, and always before you try to build your program. Also make sure you have put your name in a comment at the top of the code file. 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 project) will be created in the project folder 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. inclass-01-11.exe for this project, and will be created in project folder bin/Debug. 01/10/2012 Page 2 of 3 D. Hwang 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. 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, then Run buttons, or 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. Note that using Build and Run will run a program even if there are warnings, so only use it if you're sure the code you changed will not cause warnings. Run this program a few times using different values for input. Make sure it computes the correct results. Submitting an assignment Detailed instructions on how to use the automated testing system can be found in the on­line document Submission Instructions on the course webpage. A link to the submission system (submission.evansville.edu) can be found there as well. We will cover the basics of submitting an assignment at the end of the lecture. Note: The automated testing is based on the output of your program matching the expected outcome specified by the problem statement exactly. If the computed parts appear to be correct, then check for errors in spelling, capitalization, and whitespace. Reminder: Your program must pass the automated tests before you can leave class today. Work with each other and/or ask the instructor for assistance if you need help. Hard copies of your programs will be returned to you. 01/10/2012 Page 3 of 3 D. Hwang