Vous êtes sur la page 1sur 11

FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING

COURSE: COMPUTER PROGRAMMING (LABORATORY)

LABORATORY INSTRUCTION SHEET

Code Program Experiment Title

: : : :

BEC 10102 01 / 02 BEC/BEF/BEH/BEU/BEE 01 Introduction to C++ Programming

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

2 2011/2012 II

Experiment 01: Introduction to C++ Programming Outcomes: After completing this experiment, students should be able to: a) identify suitable steps to overcome any syntax, logic or run-time error; b) propose a solution using any C++ basic programming in solving problems; and c) construct a simple C++ program for the given problem statement. Equipment(s): You need the following items to execute this experiment: a) A personal computer with Microsoft Visual C++ b) Operating System: Windows

Introduction to C++ Programming Program Debugging Tips: In writing programs, you will come across with some frightening moments when you cannot run your program successfully and please do not afraid! When you know the difference type of error, then you are able to handle your fear, detect the error and provide correct code for it. Mind you that you should regard writing program is just like writing an essay. Any programming language has its own grammar, specifications and regulations as similar to what you have known in learning any other spoken language. There are three kinds of errors in a program as follows: 1) Syntax/compilation/compile-time error it can be detected by a compiler or interpreter, when the code is processed. Multiple error messages may be generated for one syntax error. In this case, the first error message is the most relevant for fixing the error; 2) Run-time error occurs during the execution of a program. For example, dividing an integer by zero is a runtime error. Runtime errors are harder to find than syntax

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

3 2011/2012 II

errors because they occur only under certain conditions. A runtime error may also crash the running program and destroy any information about the cause of the error; and 3) Algorithmic error or logic error which cannot be detected by a compiler or interpreter. For example, you might, by mistake, have typed in a wrong value or formula in an equation for calculation. As a result, the output from the program execution is incorrect or is not what you would expect.

If a program has either a runtime or logic error, the program has a bug. The process of finding and fixing bugs is called debugging. Debugging is more difficult, especially for a large program.

Activity One Instruction: Follow step-by-step guideline in order to experience on how to construct a simple C++ program using MS Visual C++. Step-by-step Guideline: Getting Started with MS Visual C++ The following step-by-step is guidance to getting started with C++ programming software, MS Visual C++. In this experiment, you will construct a simple C++ program using MS Visual C++ that able to calculate the voltage across a 200 ohm resistor carrying 3 Ampere using Ohms law.

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

4 2011/2012 II

Develop a Simple C++ Program to Find a Voltage using Ohms Law 1. Load software Microsoft Visual C++ 6.0. The following window as shown in Fig. 1.1 will be appeared. 2. Click File menu and select New. Then a dialog as in Fig. 1.2 will be shown. 3. Click Project tab and select Win32 Console Application. This will create an application that runs at the command prompt like DOS. 4. Make sure the option button of Create new workspace and the check box of Win32 are chosen. 5. Then click OK button. Win32 Console Application window will appear. Choose an empty project. Next click Finish button and finally click OK button. You will see a window like in Fig. 1.3.

Fig. 1.1: Initial Window

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

5 2011/2012 II

Fig. 1.2: New Window

Fig. 1.3: Initial project environment 6. Step 6 to step 8 are configuration steps to create a file for construct a C++ program. Click File New to display the dialog box with content of Files tab. The window might be look like Fig. 1.4. Choose C++ Source File. Checked the Add to project checkbox. By

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

6 2011/2012 II

default, it will select the current project, for this exercise, Lab1 to include the file into the project. However, use pull down menu to choose other project. 7. Fill in File name: exercise1. The name can be anything reflected to the purpose of the program. Ensure the file is saved in the right path and the right directory. This can be done by selecting the right location on Location. 8. These steps create a project named Lab1. This project has a file named exercise1.cpp. The project and the file are saved in the stated location. Then click OK button. Fig. 1.5 is the result of these steps.

Fig. 1.4: New dialog box

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

7 2011/2012 II

Fig. 1.5: Added a source file named exercise1.cpp in the Lab1 project 9. Enter the code in editor window. The result is shown in Fig. 1.6.

Fig. 1.6: C++ code is entered to calculate voltage using Ohms law

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

8 2011/2012 II

/* Program Description: Find a voltage across a 200 ohm resistor carrying 3A current using Ohms Law */ #include <iostream> using namespace std; void main() { int V, I, R; I=3; R=200; V= I * R; cout<< Voltage across 200 ohm resistor carrying 3A is << V << V; }

Fig. 1.7: Zoom in the code of Fig. 1.6

10. Click Build Compile exercise1.cpp. The compilation result is show in Debug window (yellow box) as shown in Fig. 1.8.

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

9 2011/2012 II

Fig. 1.8: Zoom in the code of Fig. 1.6 11. The source code can be executed if no error occurs. To execute, click Build Execute exercise1.exe. This action displays output window. To do Fig. 1.9 depicted the expected output for this experiment. List any important issues to be considered throughout this activity.

Fig 1.9: Output window

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

10 2011/2012 II

Activity Two Instruction: Fig. 1.10 shows the source code named distance.cpp. The source code will convert distances entered by user in miles to kilometers. Write the source code in Microsoft Visual C++ editor window. Execute the source code and answer the following questions.

/* File name: distance.cpp File description: Converts distances from miles to kilometers */ #include <iostream> #define KMS_PER_MILE 1.609 using namespace std; int main (void) { double kms cout<<"Enter the distance in miles: "; cin >> miles; kms = KMS_PER_MILES + miles; cout << "That equals " << kms << "kilometers.\n"; return 0;

Fig. 1.10: Source code distance.cpp

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Faculty of Electrical and Electronic Engineering Department of Computer Engineering Title: Introduction to C++ Programming

Page Session Semester

11 2011/2012 II

Questions 1) What type of error occurs during compilation? 2) What are the changes you should do to achieve successful compilation? Justify for each modification that you have made. 3) What are the important steps required in debugging?

Assessment 1) Oral examination 2) Short report

Lecturers: Mdm. Munirah Ab.Rahman & Dr Afandi Ahmad

Vous aimerez peut-être aussi