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

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

Program Design. Objectives Students should understand the basic steps in the programming process. Students should understand the need for good design.
Computer and Programming
True or false A variable of type char can hold the value 301. ( F )
1 Lab Session-VI CSIT-120 Fall 2000 Let us look at C++ syntax rules in brief Next, we complete the lab session-V Lab session-VI deals with functions (OPTIONAL)
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Lab 2 CSIT-120 Spring 2001 Session II-A (Feb 13th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (Feb 20th)
1 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 Lab Session-VI CSIT-120 Spring 2001 Let us look at C++ syntax rules in brief Exercise VI-A (Demo Required) Lab Assignment#4 Due May 1st, 2001 (No Lab.
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
1 Lab Session-I CSIT120 Spring2001 Using Windows Using An Editor Using Visual C++ Using Compiler Writing and Running Programs Lab-1 continues (Session.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Computer Science 1620 Programming & Problem Solving.
1 Lab Assignment#5 Due 12/7 A car can hold 12 gallons of gasoline and it can travel 360 miles without refuelling. Write a program the displays the trip.
1 Lab-1 CSIT-121 Fall 2004 Lab Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise.
1 Session-4 CSIT 121 Spring 2006 Debugging Tips Lab Work.
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
1 Lab 2 CSIT-120 Fall 2000 Session II-A (September 14th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (September.
Software Engineering 1 (Chap. 1) Object-Centered Design.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Chapter 2: Using Data.
1 Special Programming Workshop CSIT-120 Fall 2000 Workshop Targets Solving problems on computer Programming in C++ Writing and Running Programs Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Software Engineering Object-Centered Design. Problem Solving Does anyone scuba dive? Let’s solve this scuba-diving problem: Write a program that, given.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
CS1001 Programming Fundamentals 3(3-0) Lecture 2
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Programming Funamental slides
Programming Funamental slides
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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

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

3 Programming 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 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.

5 Strategy to solve the problem How would you solve this problem with paper and pencil? (Conversion Factor 1 Mile = 1.6 KM)

6 Solving 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 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

8 Refined Algorithm

9 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

10 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;

11 Variables 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?

12 Constant 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)

13 Microsoft Visual Studio Microsoft Visual Studio is a suite of development tools Launching VC++ (Start  Programs  Microsoft Visual Studio 6.0  Microsoft Visual C++ 6.0) Learn about the IDE interface of Visual C++ File  New  Files  C++ Source File

14 Basic Template to Start a Program #include using namespace std; void main() { ::: }

15 Template Description –#include This line tells the system to include pre- defined I/O capability so that we can use the keyboard and screen

16 Template 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

17 Program Development Phase-I In phase-I, we should input our declarations of data items into the template Let us do it now:

18 Basic Template to Start a Program #include using namespace std; void main() { double dist_kpl,dist_mpg; const float ConversionFactor=1.6; } Please note the semicolons after each declaration

19 Phase-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;

20 PhaseII: 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&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?

22 Our Program so far…. #include 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; }

23 Phase 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”

24 Phase 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

25 Rules 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 Programming Exercise Demo Due 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.

27 Sample Run How many miles does your car cover in one gallon (MPG rating from the sticker)? –25 Your KPL rating is 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