1 / 27

Session-I & II CSIT-121 Spring 2006

Session-I & II CSIT-121 Spring 2006. Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs Programming Exercise. Session Targets. Introducing the course and the syllabus Introducing the textbook and its bundles

muniya
Télécharger la présentation

Session-I & II CSIT-121 Spring 2006

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session-I & IICSIT-121 Spring 2006 • Session Targets • Introducing the VC++.NET • Solving problems on computer • Programming in C++ • Writing and Running Programs • Programming Exercise

  2. Session Targets • Introducing the course and the syllabus • Introducing the textbook and its bundles • 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 • Finally, we should know how to execute a compiled program

  3. VC++ in the VS.NET Environment • Introducing the Visual C++ .net environment • Suggested Steps to build a program quickly in Visual Studio.Net Environment: • Launch visual studio.net • Launch file-->new-->project • Click on the plus sign to the left of visual c++ to expand choices • Click on Win32 and choose Win32 console project • On the new dialog window, click on “Application Settings” • Under “additional options”, check the item “Empty Project” then click “Finish” • On the right, see the components of the project, right click on “c++ source files” • Choose “Add”, followed by “Add new item” • Choose c++ source file as the new item to be added and assign it a name • Type in the c++ code • Use Build"Build Solution" • Choose Debug Start without debugging to run it • Choose close solution to start fresh for each new program • Attempt Lesson 1-1, 1-2, 1-3, 1-4, 1-5, • (Source code is available on the course homepage)

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

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

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

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

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

  9. Refined Algorithm

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

  11. 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 kpl, mpg, kpg;

  12. 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?

  13. Constant Data Items • For constant data items, just add the keyword const before their declaration • For example, • const double CF=1.6; • (Please notice the “initialization” of the data item with a specific value)

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

  15. Template Description • #include <iostream> • 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 <iostream> • using namespace std; • void main() • { • double kpl,mpg, kpg; • const float CF=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>>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 <iostream> • using namespace std; • void main() • { • double kpl,mpg, kpg; • const double CF=1.6; • cout<<“How many miles does your car cover in one gallon (MPG rating from the sticker)?”; • cin>>mpg; • }

  23. Phase II continues • Now we have read the distance in miles • Next, our program should convert it into kilometers per gallon 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*CF • This arithmetic expression can be written in C++ using an assignment statement • kpg = mpg*CF

  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 Jan 26 • Compile and run the program with sample data on the next slide • Extend the program to find the price of gas if the distance traveled is given by the user in miles and the price of gas is as below (per liter): • const double PPL = 0.95; • HINT: Algorithm Steps: • Convert the distance into kilometers (km) • Divide the distance by KPL rating (liters) • Multiply the result by PPL to obtain total price of gas

  27. Sample Run • How many miles does your car cover in one gallon (MPG rating from the sticker)? • 25 • Your KPG rating is 40 • Your KPL rating is 10.58 • Now enter the distance (in miles) traveled from the trip meter • 400 • You spent a total of $57.46 on your trip

More Related