1 / 30

CS161 Introduction to Computer Science

CS161 Introduction to Computer Science. Topic #2. Today in CS161. Lecture Handouts you should have Solving Problems with Computers Write a short Algorithm Assignments Walk through Program #1 -- step by step Question Session. CS161 Handouts:.

fidelbrady
Télécharger la présentation

CS161 Introduction to Computer Science

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. CS161 Introduction to Computer Science Topic #2 CS161 Topic #2

  2. Today in CS161 • Lecture • Handouts you should have • Solving Problems with Computers • Write a short Algorithm • Assignments • Walk through Program #1 -- step by step • Question Session CS161 Topic #2

  3. CS161 Handouts: • From the web site, make sure to look at: (www.cs.pdx.edu/~karlaf) • Syllabus • Course Outline • Style Sheet • C++ Style Requirements • Program Cover Sheet CS161 Topic #2

  4. Solving Problems w/ Computers • Programs are... • an expression of a series of instructions that the computer must perform • written in precise languages, called programming languages • Programming languages... • translate your ideas into specific language that the computer will understand • C, C++, Java, Pascal, Visual Basic, Cobol, Fortran CS161 Topic #2

  5. Solving Problems w/ Computers • The most difficult part of programming is figuring out how to design a method to solve a problem • Only then do we translate this into C++! • Therefore, start by writing an algorithm once you understand the problem CS161 Topic #2

  6. Solving Problems w/ Computers • An algorithm is a sequence of step by step instructions for performing some task -- usually for computation • An algorithm must... • accomplish the task • be clear and easy to understand • define the sequence of steps needed to accomplish the task in the order specified CS161 Topic #2

  7. Algorithms • It is best to ignore the details of your programming language (C++) when trying to solve a problem • Instead, figure out the steps you need to go thru to solve the problem • Write these steps down in English • These steps are called the algorithm! CS161 Topic #2

  8. Algorithms • Think of your algorithm as a tool for creating the instructions for solving a problem....as if you were to tell them to another person. • Remember an algorithm is a sequence of step by step instructions for performing some tasks CS161 Topic #2

  9. For Example...solve a problem • Convert inches to millimeters • First understand the problem • where do the inches come from (the user) • what is the math needed for the conversion • mm = 25.4 times inches • how do we want to display the results • 2in convert to 50.8mm CS161 Topic #2

  10. Convert inches to millimeters • Next, write the algorithm • Step 1: Welcome the user • tell them what to expect • tell them the purpose of the program CS161 Topic #2

  11. Convert inches to millimeters • Step 2: • Get the number of inches from the user • display a prompt asking the user to enter • read in the number of inches • display what was read (echo) • ask the user if this is really correct (confirm) • if not, repeat this step until the user is satisfied CS161 Topic #2

  12. Convert inches to millimeters • Continuing with Steps 3 and 4: • Convert the number of inches to mm • mm = 25.4 times inches • Display the results • Provide a sign-off message CS161 Topic #2

  13. Convert inches to millimeters • The next step is to turn this into a C++ program! • All programs have the following “form” #include <iostream> using namespace std; //header comments... int main() { //program body goes here... return 0; } CS161 Topic #2

  14. Convert inches to millimeters #include <iostream> using namespace std; // *********************************** // Karla S. Fant // CS161 Programming Assignment #0 // Purpose of this program is to convert // inches entered in by the user into // millimeters and display the results // ********************************** int main() { CS161 Topic #2

  15. (Different Kind of Comment...) #include <iostream> using namespace std; /* *********************************** Karla S. Fant CS161 Programming Assignment #0 Purpose of this program is to convert inches entered in by the user into millimeters and display the results ********************************* */ int main() { CS161 Topic #2

  16. Convert inches to millimeters //Define variables float inches; //to save # inches float mm; //to save the result //Step #1, welcome the user cout <<“Welcome! We will be converting” <<“ inches to mm today” <<endl; CS161 Topic #2

  17. (A different way to do this...) //Define variables float inches, //to save # inches mm; //to save the result //Step #1, welcome the user cout <<“Welcome! We will be converting”; cout <<“ inches to mm today” <<endl; (NOTE: endl is end followed by a letter l) CS161 Topic #2

  18. Convert inches to millimeters //Step #2, Get the input (prompt, read) cout <<“Please enter the number of inches” <<“ that you wish to convert: “; cin >> inches; //read the # inches //echo what was entered cout <<“You entered: “ <<inches <<“in” <<endl; CS161 Topic #2

  19. Convert inches to millimeters //Step #3 Convert inches to millimeters mm = 25.4 * inches; //Step #4 Display the results cout <<inches <<“in converts to “ <<mm <<“mm” <<endl; //Step #5 Sign off message cout <<“Thank you for using CONVERT” <<endl; return 0; } CS161 Topic #2

  20. CS161 Introduction to Computer Science ASSIGNMENTS!!! CS161 Topic #2

  21. Programming Assignment #1 • First, get an ODIN account • Next, learn how to login • Try sending email! • Learn how to exit the ODIN menu and enter the UNIX shell • Meet with TA’s to get a hand’s on tutorial. Their office hours are posted on the web site: www.cs.pdx.edu/~karlaf CS161 Topic #2

  22. Programming Assignment #1 • Put the #include <iostream> in column #1 • endl is “e n d” followed by lower case L • spaces inside double quotes ARE important CS161 Topic #2

  23. Steps to Enter a Program • Login to ODIN • Exit the Menu • Now you are at the UNIX prompt • It is time to enter your program! vi prog1.cpp <enter> • Now it is time to start inserting our program. To do so, type i, for insert: CS161 Topic #2

  24. Steps to Enter a Program • Now start typing your program. Line by line. When done hit the escape key. • If the program is perfect, it is time to write it to the file. Type :w <enter> • To quit the editor, type :q <enter> CS161 Topic #2

  25. Steps to Enter a Program • We are now back to the UNIX prompt. To compile the program we type the following. g++ prog1.cpp <enter> • At this point, there will either be error messages displayed (with line numbers), or not (if it is correct!). CS161 Topic #2

  26. Steps to Enter a Program • To fix a program, we must edit it. Type: vi prog1.cpp <enter> • Now, if you want to go to the line where the error occurred, type the line number followed by the enter key. For example, to go to line number 10 type: 10 <enter> CS161 Topic #2

  27. Steps to Enter a Program • To add something at the end (like a semicolon), type a for append: • Now you are in append mode. You can type what you want. When you are done typing hit the escape key • When you are done, type the following to write the changes back to the file and quit the editor :wq! <enter> CS161 Topic #2

  28. Steps to Enter a Program • Other things to type while in the editor: • h goes left • k goes up • j goes down • l goes right • x delete the character you are at • dd delete the line that you are at CS161 Topic #2

  29. Steps to Enter a Program • Again, we are at the unix prompt. To compile your program type: g++ prog1.cpp <enter> • If it compiles without any errors, it is time to run the program to see it actually work! Type ./a.out <enter> CS161 Topic #2

  30. CS161 Introduction to Computer Science Are you Ready? CS161 Topic #2

More Related