C++ Lab1
C++ Lab1
Our laboratories are installed with the Turbo-C compiler. To start from scratch, you should login your NUSNET account when you go to the computer lab. Click DOS Prompt and you will go to drive H. Now you can invoke the Turbo-C compiler stored in drive C from your current drive H using this command: H:\> c:\tcpp30\bin\tc and pressing the Enter key, and you can do your work now.
Everything you need to write, edit, compile, link, and debug your programs is at your fingertips when you are in the Turbo C++ Integrated Development Environment (IDE). There are three components in the IDE: the menu bar at the top provides primary access to all the menu commands the window area in the middle is a screen area that you can move, resize, overlap, open, close, zoom and tile the status line at the bottom offers on-line hints, tells you what the program is doing, etc. To choose a menu command, you can either use the keyboard or a mouse. As an illustration, to choose the menu item Open contained in the menu File (the meaning of File|Open): Using Keyboard: press Alt and the highlighted letter F to display the menu (observe the changes on the status line); then press the highlighted letter O to choose the command once the menu is displayed. From the keyboard, you press ESC to cancel an action. You can use a number of keyboard shortcuts or hot keys to access menu bar and choose commands. If hot key(s) for a command is available, it (they) will appear on the right-hand side of that command on the menu or listed on the status line below. To open a file, you can simply press F3. A list of Turbo C++ IDE hot keys is attached at the end of this part. You should memorize the most-used ones and use them where applicable. Programming speed is important during tests. Using Mouse: move the mouse pointer to File on the menu bar and click the left mouse button; then move the mouse pointer to Open and click. From this point on, the word click means press and release the left mouse button. Click anywhere outside the menu to cancel an action.
1
An Exercise
In this section, you will learn to create, edit, save, load, compile, and run C programs from the keyboard and mouse. A list of commonly used keyboard editor commands is attached at the end of the part. Go through the following warming-up exercise before creating your first program. 2.1 Creating a New Program [File|New] Click File on the Menu bar to get a pull-down menu. Click New. You will see a cursor at the upper left corner of the active window. You will now edit your first C program. Key in the following program that contains some errors. Ignore the line numbers. (C is case-sensitive so do not change include to Include or main to MAIN and so on. However, the number of spaces need not be exactly the same as that in the given program.) /* first.c */ #include <stdio.h> #include <conio.h> void main(void) { float a, b, sum, diff; scanf("%f%f", &a, &b); printf("/n/n Enter two numbers: "); sum = a + b; diff = a - b; printf("The sum is %f \n", SUM); printf("The difference is %f \n", diff); } 2.2 Saving a Program [F2]
If this is your very first time using the Turbo C++ 3.0 Compiler, you need to do the followings: Select Options from the menu bar. Click the Directories to open a dialogue box. Check that the Include directories and the Library directories are c:\TCPP30\INCLUDE and c:\TCPP30\LIB respectively. If not, make the necessary changes and click OK. Select Options from the menu bar. Click Environment and select Editor. Change the Default Extension from CPP to C and click OK. Select Options from the menu bar. Click Save. Mark all the Save Options and click OK. Once done, you do not have to repeat the above steps when you login some other time. A program can be saved in a disk for future use. If the edit window is opened using File|New, it will have the default name NONAMExx.C (xx stands for a number from 00 to 99). Press F2. Turbo C++ prompts you for a filename. You may specify the location where you want your file to be stored by typing the path name before the filename (eg. A:first.c). The default directory is the current directory and the default file extension is .cpp. Remember to save your program after each reasonable amount of editing and before execution in case of power failure or system crash. Save your file as first.c
2.3
Editing a Program
Take the above sample program first.c for example: in line 8, /n/n should be changed to \n\n. exchange line 7 and line 8. in line 11, SUM should be sum. Press F2 to save the program. 2.4 Getting Helps [F1]
There is help information on virtually all aspects of the IDE and Turbo C++ in the Help menu which can be opened by F1. To get help on things like function names (eg. printf, scanf), header files (eg. stdio.h), reserved words (eg. include), and so on, position the cursor on the item in an Edit window and press Ctrl-F1. Alternatively you can simply put the mouse cursor on the word and click the right mouse button. 2.5 Compiling a Program [Alt-F9] A compiler is a program that translates a high-level language (e.g., C or C++) program into machine language to be understood by the computer. Each completed C program can be compiled by loading it into the Turbo C++ IDE and press Alt-F9. If syntax errors are detected during compilation, a list of messages will appear in the Message window telling you where and what the errors are. Press ENTER to return to the Edit window and make corrections before re-compiling the program. If compilation is successful, an object code (with extension .obj) of the program is generated.
2.6 Running a Program [Ctrl-F9] To run your program, press Ctrl-F9. It will also compile and link the program so you do not have to compile it using Alt-F9. To stop a program while it is running, press Ctrl-Break. When your program first.C is running, you will see on the User/Output screen:
Enter two numbers:
Type any two integers, separated by one space (e,g., 3 2 ), and press ENTER. The following sentences will flash past and you are placed in the Edit window:
The sum is 5 The difference is 1
You may encounter run-time errors as you go along developing programs. A common one is division by zero. You will be taught how to debug programs in Laboratory Exercise 2. 2.7 Viewing Screen Output [Alt-F5]
To review your program output on the screen, you should press Alt-F5. Return to the editor by pressing any key.
To go to the DOS command prompt temporarily, select File|DOS Shell. You can now copy, delete, or rename files or view directory contents. Note that you have not quitted the IDE, rather you have just left the IDE a while to do some DOS work. Type EXIT and press ENTER to return to the IDE. 2.9 Quit Turbo C++ To quit Turbo C++, press Alt-X or select the menu File|Exit.
CUA: Common User Access command set (industrial standard user interface) Alt: Alternate command set (default for all Borlands products) 3.1 General IDE Hot Keys CUA Alt Menu item F1 F1 Help F2 File|Save F3 File|Open F4 Run|Go to Cursor F5 Window|Zoom Ctrl+F F6 Window|Next 6 F7 F7 Run|Trace Into F8 F9 F10 F8 F9 none Run|Step Over Compile|Make none
Function Displays a help screen. Saves the file that's in the active edit window. Brings up a dialog box so you can open a file. Runs your program to the line where the cursor is positioned. Zooms the active window. Cycles through all open windows. Runs your program in debug mode, tracing into functions. Runs your program in debug mode, stepping over function calls. Invokes the Project Manager to make an .EXE file. Takes you to the menu bar.
3.2 Menu Bar Hot Keys CUA Alt+Spaceba r Alt+C Alt+D Alt+E Alt+F Alt+H Alt+O Alt+P Alt Alt+Spaceb ar Alt+C Alt+D Alt+E Alt+F Alt+H Alt+O Alt+P Menu item _ menu Compile menu Debug menu Edit menu File menu Help menu Options menu Project menu Function Takes you to the _ (System) menu Takes you to the Compile menu Takes you to the Debug menu Takes you to the Edit menu Takes you to the File menu Takes you to the Help menu Takes you to the Options menu Takes you to the Project menu
4
Takes you to the Run menu Takes you to the Search menu Takes you to the Window menu Exits Borland C++
3.3 Editing Hot Keys CUA Alt Menu item Ctrl+Del Ctrl+Del Edit|Clear Ctrl+Ins Ctrl+Ins Edit|Copy Shift+Del Shift+Del Edit|Cut Shift+Ins Alt+Bksp c F3 Shift+Ins Edit|Paste
Function Removes selected text from window; doesn't put it in Clipboard Copies selected text to Clipboard Places selected text in Clipboard, deletes selection Pastes text from Clipboard into the active window Restores text in active window to previous state Repeats last Find or Replace command Saves file in active edit window Opens file
3.4 Window Management Hot Keys CUA Alt+# Alt+0 Ctrl+F4 Shift+F 5 Alt+F5 Shift+F 5 Ctrl+F6 Alt Alt+# Alt+0 Alt+F3 Menu item Function Displays a window, where # is the number of the window you want to view Displays a list of open windows Closes the active window Tiles all open windows Opens an Inspector window Displays User Screen Zooms/unzooms the active window Switches the active window Changes size or position of active window
3.5 Online Help Hot Keys CUA Alt Menu Item F1 F1 Help|Contents F1 F1 F1 F1 Help|Using Help Shift+F 1 Alt+F1 Ctrl+F1 Shift+F1 Help|Index Alt+F1 Ctrl+F1 Help| Previous Topic Help|Topic Search
Function Opens a context-sensitive help screen Brings up Help on Help. (Just press F1 when you're already in the help system.) Brings up Help index Displays previous Help screen calls up language-specific help in the active edit window
3.6 Debugging/Running Hot Keys CUA Alt+F5 Alt+F7 Alt+F8 Alt+F9 Ctrl+F2 Alt Alt+F4 Alt+F7 Alt+F8 Alt+F9 Ctrl+F2 Ctrl+F3 Ctrl+F4 Ctrl+F7 Ctrl+F8 Ctrl+F9 F4 F7 F8 F9 Menu Item Debug|Inspect Search|Previous Error Search|Next Error Compile|Compile Run|Program Reset Debug|Call Stack Debug|Evaluate/Modify Debug|Add Watch Debug|Toggle Breakpoint Run|Run Run|Go To Cursor Run|Trace Into Run|Step Over Compile|Make Function Opens an Inspector window Takes you to previous error Takes you to next error Compiles to .OBJ Resets running program Brings up call stack Evaluates an expression Adds a watch expression Sets or clears conditional breakpoint Runs program Runs program to cursor position Executes tracing into functions Executes skipping function calls Makes (compiles/links) program
Ctrl+F5 F5 Ctrl+F9 F7 F8 F9
Assignment
Write a program named as sums.c to ask the user to enter eight integers, and print the following contents on the screen: sum of the square of each odd term (1st, 3rd, 5th and 7th) sum of the square of each even term (2nd, 4th, 6th and 8th) Difference of the above sums The screen output is shown below:
Enter Eight Integers: 8 3 6 2 4 5 9 1 Sum of the square of each odd term: 197 Sum of the square of each even term: 39 Difference of two sums: 158
Help
The following instructions will help you in writing the program. print on the screen the message asking the user to enter 8 integers (use printf) scan in 8 integers from the keyboard (use scanf) compute sum of the square of each odd term, sum of the square of each even term, and the difference of the two sums print the complete output on the screen
Assignment Write a program named as compute.c that accepts an arbitrary number of integers from the command line and displays their sum, mean, maximum and minimum on the screen as shown below. The number of arguments entered from the command line is assumed to be unknown. These are a few test cases: C:\> compute 28 Sum = 28 Mean = 28.00 Maximum = 28 Minimum = 28 C:\> compute Error !! No Data.
Sum = 3692 Mean = 738.40 Maximum = 3987 Minimum = -389 The following logic is suggested. If you have alternatives, please go ahead to code every thing in your own way. if the number of arguments is less than 2 print error messages and exit; convert the first integer to a numerical value and initialize max, min, sum to the value; if the number of arguments is greater than 2 for the remaining integers { convert a remaining integer to a numerical value; accumulate the numerical value to sum; change max to the numerical value if necessary; change min to the numerical value if necessary; } compute mean; print sum, mean, max, min;