Top Banner
1 Lab-1 Lab-1 CSIT-121 Spring 2005 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise
27

1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

1

Lab-1Lab-1CSIT-121 Spring 2005CSIT-121 Spring 2005

• Lab Targets

• Solving problems on computer

• Programming in C++

• Writing and Running Programs

• Programming Exercise

Page 2: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

2

TargetsTargets

• We wish to learn how to program in VC++

• We should know how to launch Visual C++ integrated environment

• We should know how to compile a program

• Let us work on most fundamental aspects of programming in C++

Page 3: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

3

Programming FundamentalsProgramming Fundamentals

• We have a real life problem that is to be solved on the computer

• In order to solve it, we need to write a program

• The program must be written using the syntax rules of Visual C++

Page 4: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

4

Example ProblemExample Problem

• A problem is given as follows:

• “If a car has MPG rating of 25 miles, what is its kilometer per liter rating?”

• Given this problem, let us first design a program that will convert miles into kilometers. Thus we can compare the MPG and KPL.

Page 5: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

5

Strategy to solve the problemStrategy to solve the problem

• How would you solve this problem with paper and pencil?

• (Conversion Factor 1 Mile = 1.6 KM)

Page 6: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

6

Solving through programmingSolving through programming

• We will use C++ syntax to solve this problem on the computer

• We first need to know the total number of data items in this problem and their type

• TOTAL DATA ITEMS

Page 7: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

7

Solution on paperSolution on paper

• Next we should solve it on paper. The solution on paper is called an algorithm

• Initial Algorithm

• Read the MPG

• Convert it to Kilometers per gallon

• Convert gallon into liters

• Compute Kilometers per liter

Page 8: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

8

Refined AlgorithmRefined Algorithm

Page 9: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

9

How to implement in C++?How to implement in C++?

• How should we implement this solution in C++?

• First part is to express the data in C++

• C++ provides data types to capture our real life data into programs

• For numbers, we can have whole numbers such as 19 or FP numbers such as 19.63

Page 10: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

10

How to express numbers in C++How to express numbers in C++

• The Kilometers could contain fractional part because of the 1.6 conversion factor

• We need a data format that can accept a FP number into it

• C++ provides float and double• double dist_kpl, dist_mpg;

Page 11: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

11

Variables and ConstantsVariables and Constants

• If you can change the value of a data item in your program, it is known as a variable.

• If you cannot change the value of a data item in your program, it is a constant.

• Can you change the value of the conversion factor between Miles and Kilometers?

• How can we show constant data items?

Page 12: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

12

Constant Data ItemsConstant Data Items

• For constant data items, just add the

keyword const before their declaration

• For example,

• const float ConversionFactor=1.6;• (Please notice the “initialization” of the data

item with a specific value)

Page 13: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

13

Microsoft Visual StudioMicrosoft Visual Studio

• Microsoft Visual Studio is a suite of development tools

• Launching VC++ (StartProgramsMicrosoft Visual Studio 6.0Microsoft Visual C++ 6.0)

• Learn about the IDE interface of Visual C++

• FileNewFilesC++ Source File

Page 14: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

14

Basic Template to Start a Basic Template to Start a ProgramProgram

• #include <iostream>• using namespace std;• void main()• {• :::• :::• :::• }

Page 15: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

15

Template DescriptionTemplate Description

– #include <iostream>

• This line tells the system to include pre-defined I/O capability so that we can use the keyboard and screen

Page 16: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

16

Template DescriptionTemplate Description

– void main()

• This line gives the name of the function that you are developing. main() is the default name used for the main function of any program

• Function is a block of code that performs a given task. A function carries a name and opening and closing braces

Page 17: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

17

Program Development Phase-IProgram Development Phase-I

• In phase-I, we should input our declarations of data items into the template

• Let us do it now:

Page 18: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

18

Basic Template to Start a Basic Template to Start a ProgramProgram

• #include <iostream>• using namespace std;• void main()• {• double dist_kpl,dist_mpg;• const float ConversionFactor=1.6;

• }• Please note the semicolons after each declaration

Page 19: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

19

Phase-II: Action partPhase-II: Action part

• Once we store our data into data items, we are ready to perform the actual conversion from miles to kilometers

• First we should read the miles from the keyboard• cout<<“How many miles does your car cover in

one gallon (MPG rating from the sticker)?”;• cin>>dist_mpg;

Page 20: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

20

PhaseII: Action partPhaseII: Action part

• cout<< is the way to display your data on the screen

• cin>>variable_name is the way to read data from the keyboard and assign it to one variable in the program

Page 21: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

21

Q&AQ&A

• How are fractional numbers (e.g. 3/4 or 1 1/2 are represented in C++?

• What is the use of opening braces and closing braces in the program?

• What is the difference between variables and constants?• What keyword is added to make a value constant?• What does cin>> do?• Why do we put semicolons at the end of each

statement?

Page 22: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

22

Our Program so far….Our Program so far….

• #include <iostream>• using namespace std;• void main()• {• double dist_kpl,dist_mpg;• const float ConversionFactor=1.6;• cout<<“How many miles does your car cover in one

gallon (MPG rating from the sticker)?”;• cin>>dist_mpg;• }

Page 23: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

23

Phase II continuesPhase II continues

• Now we have read the distance in miles

• Next, our program should convert it into kilometers using the conversion factor

• It is here that we should design an “assignment statement”

Page 24: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

24

Phase II continuesPhase II continues

• Here, we are multiplying the mileage by the conversion factor and getting the result as distance in km

• distance in km = distance in miles*1.6

• This arithmetic expression can be written in C++ using an assignment statement

• dist_kpl = dist_mpg*1.6

Page 25: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

25

Rules of Assignment StatementRules of Assignment Statement

• In C++, you will use the destination variable on left of the equal sign

• You cannot use a constant data item on left of the equal sign

• You should not assign a FP value to an integer variable

• Doing so will cause the loss of fractional part

Page 26: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

26

Programming Exercise Demo Programming Exercise Demo Due Feb 2Due Feb 2• Extend this program by adding the trip cost

calculation feature. This is an additional feature and does not replace the original program. The user will enter the distance traveled in miles from the trip meter. Convert this distance into kilometers and divide by KPL (kilometers per liter ratio) to calculate the total amount of fuel consumed in liters. Multiply this quantity by 0.70 to get the total amount of money spent in fuel purchase. Add tolls and coffee as desired. Display the result.

Page 27: 1 Lab-1 CSIT-121 Spring 2005 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.

27

Sample RunSample Run

• How many miles does your car cover in one gallon (MPG rating from the sticker)?– 25

• Your KPL rating is 10.58

• Now enter the distance (in miles) traveled from the trip meter– 400

• Enter amount spent on tolls and coffee– 12

• You spent a total of $54.34 on your trip