BEC10102_Lab_01_V1

Embed Size (px)

Citation preview

  • 8/2/2019 BEC10102_Lab_01_V1

    1/11

    FACULTY OF ELECTRICAL AND ELECTRONIC ENGINEERING

    COURSE:

    COMPUTER PROGRAMMING (LABORATORY)

    LABORATORY INSTRUCTION SHEET

    Code : BEC 10102

    Program : 01 / 02 BEC/BEF/BEH/BEU/BEE

    Experiment : 01

    Title : Introduction to C++ Programming

  • 8/2/2019 BEC10102_Lab_01_V1

    2/11

    Faculty of Electrical and Electronic Engineering Page 2

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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; andc) 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 orinterpreter, 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, dividingan integer by zero is a runtime error. Runtime errors are harder to find than syntax

  • 8/2/2019 BEC10102_Lab_01_V1

    3/11

    Faculty of Electrical and Electronic Engineering Page 3

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 orinterpreter. 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.

  • 8/2/2019 BEC10102_Lab_01_V1

    4/11

    Faculty of Electrical and Electronic Engineering Page 4

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 willbe 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 applicationthat runs at the command prompt like DOS.

    4. Make sure the option button ofCreate new workspace and the check box ofWin32 arechosen.

    5. Then click OKbutton. Win32 Console Application window will appear. Choose an emptyproject. Next click Finish button and finally click OKbutton. You will see a window like in

    Fig. 1.3.

    Fig. 1.1: Initial Window

  • 8/2/2019 BEC10102_Lab_01_V1

    5/11

    Faculty of Electrical and Electronic Engineering Page 5

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 Newto display the dialog box with content ofFiles tab. The window might be

    look like Fig. 1.4. Choose C++ Source File. Checked the Add to project checkbox. By

  • 8/2/2019 BEC10102_Lab_01_V1

    6/11

    Faculty of Electrical and Electronic Engineering Page 6

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 theprogram. 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 OKbutton. Fig. 1.5 is

    the result of these steps.

    Fig. 1.4: Newdialog box

  • 8/2/2019 BEC10102_Lab_01_V1

    7/11

    Faculty of Electrical and Electronic Engineering Page 7

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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

  • 8/2/2019 BEC10102_Lab_01_V1

    8/11

    Faculty of Electrical and Electronic Engineering Page 8

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    /*

    Program Description: Find a voltage across a 200 ohm resistor

    carrying 3A current using Ohms Law

    */

    #include

    using namespace std;

    void main()

    {

    int V, I, R;I=3;

    R=200;

    V= I * R;

    cout

  • 8/2/2019 BEC10102_Lab_01_V1

    9/11

    Faculty of Electrical and Electronic Engineering Page 9

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 Executeexercise1.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

  • 8/2/2019 BEC10102_Lab_01_V1

    10/11

    Faculty of Electrical and Electronic Engineering Page 10

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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

    #define KMS_PER_MILE 1.609

    using namespace std;

    int main (void)

    {

    double kms

    cout miles;

    kms = KMS_PER_MILES + miles;

    cout

  • 8/2/2019 BEC10102_Lab_01_V1

    11/11

    Faculty of Electrical and Electronic Engineering Page 11

    Department of Computer Engineering Session 2011/2012

    Title: Introduction to C++ Programming Semester II

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

    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 examination2) Short report