1 special programming workshop csit-120 fall 2000 workshop targets solving problems on computer...

27
1 Special Programming Special Programming Workshop Workshop CSIT-120 Fall 2000 CSIT-120 Fall 2000 Workshop Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise

Upload: marianna-harris

Post on 03-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

1

Special Programming WorkshopSpecial Programming WorkshopCSIT-120 Fall 2000CSIT-120 Fall 2000

• Workshop Targets

• Solving problems on computer

• Programming in C++

• Writing and Running Programs

• Programming Exercise

2

TargetsTargets

• We wish to learn how to program

• We know how to edit a program, compile it and run it

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

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

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++

4

Example ProblemExample Problem

• A problem is given as follows:

• “Given several distances in kilometers, we wish to see the same in miles.”

• Given this problem, let us first design a program that will convert only one given distance into miles. We can change it later to let it convert several given distances into miles

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.6km)

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

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 distance in kilometers

• Convert it to miles

• Display the distance in miles

• Now refine this algorithm

8

Refined AlgorithmRefined Algorithm

9

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

• The big question is how to implement this solution in C++?

• First part is to express the distance value 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

10

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

• For whole numbers, we have data type int in C++

• For example, we can declare

int distancekm;• (We have declared a data item called distancekm

that is a whole number)• Now, we need another data item to represent

distance in miles

11

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

• The distance in miles could contain fractional part because conversion factor has a fractional part

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

• C++ provides float and double

• float distancemiles;

12

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 to show constant data items?

13

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)

14

Basic Template to Start a Basic Template to Start a ProgramProgram

• #include <iostream.h>

• void main()

• {

• :::

• :::

• :::

• }

15

Template DescriptionTemplate Description

– #include <iostream.h>

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

16

Template DescriptionTemplate Description

– void main(void)

• 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

17

Program Development Phase-IProgram Development Phase-I

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

• Let us do it now:

18

Basic Template to Start a Basic Template to Start a ProgramProgram

• #include <iostream.h>

• void main()

• {int distancekm;

float distancemiles;const float ConversionFactor=1.6;

• }

• Please note the semicolons after each declaration

19

Phase-II: Action partPhase-II: Action part

• Once we capture our data into data items, we need to perform the actual conversion from kilometers to miles

• First we should read the kilometers from the keyboard

• cout<<“Give the distance in kilometers”;

• cin>>distancekm;

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

21

Q&AQ&A

• Why is <iostream.h> included in the program source code?

• Why do we name the only function in our program as main()?

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

• What is the difference between variables and constants?• What does cout<< do?• Why do we put semicolons at the end of each statement?

22

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

• #include <iostream.h>

• void main()

• {int distancekm;

float distancemiles;const float ConversionFactor=1.6;

cout<<“Give the distance in kilometers”;

cin>>distancekm

• }

23

Phase II continuesPhase II continues

• Now we have read the distance in kilometers.

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

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

24

Phase II continuesPhase II continues

• Here, we are dividing the distance in kilometers by the conversion factor and getting the result as distance in miles

• distance_in_miles = distance_in_km/1.6

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

• distancemiles=distancekm/ConversionFactor;

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

26

Complete ProgramComplete Program

• #include <iostream.h>

• void main()

• {

int distancekm;

float distancemiles;

const float ConversionFactor=1.6;

cout<<“Give the distance in kilometers”;

cin>>distancekm;

distancemiles=distancekm/ConversionFactor;

cout<<“Here is distance in miles”;

cout<<distancemiles;

}

27

Programming ExerciseProgramming Exercise

• Write a program that accepts the age and weight of your cat. Then it shows the age and weight to the user and the meows of the cat are printed as “meow meow” on the screen