Você está na página 1de 6

Computer Programming

Fall 2011

Lab-01,02
(Due Monday, October 31, 2011)

Objective
The objective of this lab is to Provide understanding about fundamental concepts. Provide understanding of basic computer architecture. Provide practice for flow charts. Get familiar with Turbo C IDE. To explore its debugging capabilities. Understanding its customization (& relevant settings).

Important
This lab is to be done alone. Absolutely no cooperation is allowed. NOT Consulting your lecture notes is not required though highly recommended.

Happy Coding!

Institute of Management Sciences (formerly Pak-AIMS)

Computer Programming

Fall 2011

Part 1
Task 1
Why do we prefer software based solutions over hardware based solutions? Write your answer in points.

Task 2
Draw the diagram of basic computer organization and explain each component in a single line or two.

Task 3
Draw a flow chart of the following problems

Problem A
Convert a decimal number to binary. For example, if user inputs 132, you should display 1000 0100.

Problem B
Accept a number from user and prints its table. For example, if user enters 10, you should display 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100

Problem C
Draw a flowchart of the logical steps needed to produce a printed listing of all students over the age of 20 in a class. The input record contains the age of students. Assume a sentinel value of 99 for the age field of the trailer record.

Problem D
A set of examination papers which have been graded with scores from 0 to 100 is to be searched to find how many of them are above 90. The total has to be printed. Assume a suitable sentinel value for the trailer record.

Institute of Management Sciences (formerly Pak-AIMS)

Computer Programming

Fall 2011

Part 2
First Program
Download Turbo C IDE from the wiki (Download Link). Extract it and go to TurboC\BIN directory and launch tc.exe. This file (tc.exe) contains the IDE in which we will be writing our C++ programs. Go to File menu and click New (Shortcut: Alt + F, N). A new file will be opened where you can write code. Type in the following program in that newly opened file

#include <iostream.h> int main() { cout << Hello World!; return 0; }

Make sure you save your typed program periodically by clicking File menu and clicking Save (Shortcut: F2). Name the file as First.cpp o When saving for the first time, Turbo C IDE will ask you a location where it will save the file for you. By default it saves the file to the current directory (usually the BIN directory that contains tc.exe file). Make sure you remember the path. Go to Compile menu and click Compile (Shortcut: Alt + F9). It will successfully compile the code. If it reports any errors, double check the program, specially the inverted commas around Hello World! They are double inverted commas (located near Enter key) and not two single commas. After removing the errors (if any) compile again by pressing (Alt + F9 or by Compile menu). Once you have compiled the code successfully, you can now link it by going to Compile menu and clicking Link menu item. If you typed everything correctly, you will get a success here as well. Now, all the steps have been completed that we discussed from Algorithm to Executable lecture and Turbo C has made an executable file for us. We can now execute the program. Turbo C will name the executable after our source code file (the one that we gave while saving the file). In this case, it will generate the executable under First.exe. Compilation + Linking + Execution can be done in a single step by going to Run menu and selecting Run menu item (shortcut: Ctrl + F9). Go to Window menu and select User Screen option (shortcut: Alt + F5) to view the output.

Institute of Management Sciences (formerly Pak-AIMS)

Computer Programming

Fall 2011

Second Program
Go to TurboC\BIN directory and launch tc.exe. This file (tc.exe) contains the IDE in which we will be writing our C++ programs. Go to File menu and click New (Shortcut: Alt + F, N). A new file will be opened where you can write code. Type in the following program in that newly opened file

#include <iostream.h> int main() { int num1 = 10; int num2 = 20; int sum = num1 + num2; cout << sum; return 0; }

Make sure you save your typed program periodically by clicking File menu and clicking Save (Shortcut: F2). Name the file as Sum.cpp o When saving for the first time, Turbo C IDE will ask you a location where it will save the file for you. By default it saves the file to the current directory (usually the BIN directory that contains tc.exe file). Make sure you remember the path. Go to cout << sum; line and press (Ctrl + F8). It will turn the background color of that line to red. We just inserted a breakpoint. Now whenever this line will be going to execute, execution will be halted and you will be brought back to the IDE window so that you can inspect the values in the variables and make sure everything is according to the expectations. o Breakpoints provide great flexibility to track bugs. Go to Run menu and selecting Run menu item (shortcut: Ctrl + F9). You will see that before executing the line (where we inserted a break point), youre brought back to the IDE. (Note the changed color of the line background. It shows which line is currently being executed). Move your cursor to num1 variable and press Alt+F4 (shortcut for inspect). A window will open telling you the data type of the variable and its current value (which will be 10). Press escape to close the window. Now move your cursor to num2 variable and press Alt+F4 (shortcut for inspect). A window will open telling you the data type of the variable and its current value (which will be 20). Press escape to close the window. Finally move your cursor to sum variable and press Alt+F4 (shortcut for inspect). A window will open telling you the data type of the variable and its current value (which will be a garbage value). This is because sum is not assigned a value yet.

Institute of Management Sciences (formerly Pak-AIMS)

Computer Programming

Fall 2011

Press F8 (shortcut for step over) and the current line will be executed in a single step. Now move your cursor again to sum and press Alt+F4. Now it will show you the value 30 instead of the garbage value. This is because the line has been executed so now sum has the value which was assigned to it. Press Ctrl+F9 to execute the remaining program in a single go. (You can stop debugging anytime by pressing Ctrl+F2. You can also use watch windows in order to watch multiple variables simultaneously. Re-run the program by pressing Ctrl+F9 and you will be again brought back to the IDE before the execution of int sum = num1 + num2; Go to Window menu and click Watch menu item. Make sure your windows are cascaded. (Go to Window menu and click Cascade Windows) so that we can see multiple windows at a time. In the watch window you can press Enter key and write variables num1, num2 and sum. Now execute the program in the same way as you did before (by pressing F8 to execute one line at a time). You will see that the value in the sum variable changes the moment int sum = num1 + num2; is executed. Keep exploring your IDE. It will greatly help you in writing good quality programs and will save lots of precious time as well.

Institute of Management Sciences (formerly Pak-AIMS)

Computer Programming

Fall 2011

Submission Instructions
1. For this particular lab, you are required to submit handwritten (no printouts are not allowed) solution of the problems in Part 1. 2. Clearly mention your subject and roll number.

End

Institute of Management Sciences (formerly Pak-AIMS)

Você também pode gostar