Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.

Similar presentations


Presentation on theme: "CS161 Topic #21 CS161 Introduction to Computer Science Topic #2."— Presentation transcript:

1 CS161 Topic #21 CS161 Introduction to Computer Science Topic #2

2 CS161 Topic #22 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

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

4 CS161 Topic #24 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

5 CS161 Topic #25 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

6 CS161 Topic #26 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

7 CS161 Topic #27 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!

8 CS161 Topic #28 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

9 CS161 Topic #29 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

10 CS161 Topic #210 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

11 CS161 Topic #211 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

12 CS161 Topic #212 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

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

14 CS161 Topic #214 Convert inches to millimeters #include 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() {

15 CS161 Topic #215 (Different Kind of Comment...) #include 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() {

16 CS161 Topic #216 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;

17 CS161 Topic #217 (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)

18 CS161 Topic #218 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;

19 CS161 Topic #219 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; }

20 CS161 Topic #220 CS161 Introduction to Computer Science ASSIGNMENTS!!!

21 CS161 Topic #221 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/~karlafwww.cs.pdx.edu/~karlaf

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

23 CS161 Topic #223 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 Now it is time to start inserting our program. To do so, type i, for insert:

24 CS161 Topic #224 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 To quit the editor, type :q

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

26 CS161 Topic #226 Steps to Enter a Program To fix a program, we must edit it. Type: vi prog1.cpp 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

27 CS161 Topic #227 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!

28 CS161 Topic #228 Steps to Enter a Program Other things to type while in the editor: –h goes left –kgoes up –jgoes down –lgoes right –xdelete the character you are at –dddelete the line that you are at

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

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


Download ppt "CS161 Topic #21 CS161 Introduction to Computer Science Topic #2."

Similar presentations


Ads by Google