Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,

Similar presentations


Presentation on theme: "1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,"— Presentation transcript:

1 1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague, Western Carolina University. A Constituent Institution of The University of North Carolina

2 2 Introduction to Computers and C++ Programming l Computer Systems l Programming and Problem-Solving l Introduction to C++ l Testing and Debugging

3 3 1.1 Computer Systems l Hardware -- Parts of a computer you can touch »PC »Workstation »Mainframe »Network »input/output »memory –primary, secondary –fixed, removable »CPU

4 4 1.1 Computer Systems l Software -- Parts of a computer you cannot touch »Operating Systems –Macintosh –Windows –Linux »High-Level Languages –Ada, C++, Java, BASIC, Lisp, Scheme »Compilers –Source program, object program, Linking »Editor: Integrated Development Environments (IDE) –IDEs combine editor, compiler and the execution environment (usually including a debugger)

5 5 1.2 Programming and Problem Solving l Algorithms »Idea is more general than ‘program’ »Hard part of solving a problem is finding the algorithm l Program Design Process »Problem Solving Implementation phase phase P roblem definition –Algorithm Design Translation to C++ –Desk Testing Testing – – Working Program

6 6 Software Life Cycle l Analysis and specification of task (problem definition). l Design of the software (algorithm design) l Implementation (coding). l Testing l Maintenance and evolution of the system l Obsolescence

7 7 1.3 Introduction to C++ l Origins of the C++ Language »Bjarne Stroustrup designed C++ for modeling. »C++ is an OOP extension of the C language. »C was developed as a systems programming language from the B language in the Unix environment. It grew into a general purpose programming language as its libraries were developed.

8 8 Display 1.8 A Sample C++Program #include using namespace std; int main( ) { int number_of_pods, peas_per_pod, total_peas; cout << "Press return after entering a number.\n"; cout << "Enter the number of pods:\n"; cin >> number_of_pods; cout << "Enter the number of peas in a pod:\n"; cin >> peas_per_pod; total_peas = number_of_pods * peas_per_pod; cout << "If you have "; cout << number_of_pods; cout << " pea pods\n"; cout << "and "; cout << peas_per_pod; cout << " peas in each pod, then\n"; cout << "you have "; cout << total_peas; cout << " peas in all the pods.\n"; return 0; }

9 9 Some details of a C++ Program #include using namespace std; int main() { l These lines are a complicated way to say “the program starts here” return 0; } l The last two lines say “the program ends here.”

10 10 Some Details of a C++ Program int number_of_pods, peas_per_pod, total_peas; l This is a variable declaration. (We explain variable later.) l Variable must be declared before they can be used. l Variables are used to store values. l The int in the declaration says the variables can hold integer values. l Other lines are (executable) statements that tell the compute to do some task.

11 11 Some Details of a C++ Program l cout is the output stream, read See-Out.. It is attached to the monitor screen. << is the insertion operator l cin is the input stream, read see-in, and is attached to the keyboard. >> is the extraction operator. l “Press return after entering a number.\n” is called a cstring literal. It is a message for the user. l cout << “Press return …\n” sends the message to cout l cin >> number_of_pods;

12 12 Some Details of a C++ Program cout << “Enter the number of peas in a pod.\n”; cin >> peas_per_pod; l The first of these lines sends a request to the user. l The second extracts an integer value (the type of peas_per_pod) into peas_per_pod.

13 13 Some Details of a C++ Program total_peas = number_of_pods * peas_per_pod; l The asterisk, *, is used for multiplication. l This line multiplies the already entered values of number_of_pods and peas_per_pod to give a value which is stored (assigned to) total_peas.

14 14 Display 1.9 Layout of a Simple C++ Program #include An include directive using namespace std; More later, for now “do it” int main()Declares main function {Start of main’s block variable _declarations Statement1; Statement2;... Statement_last; return 0;Says “end program here” }End of main’s block l (a

15 15 Compiling and Running a C++ Program l You write a C++ program using a text editor exactly as you would write a letter home. l Compiling depends on the environment. l You may be using an Integrated Development Environment or IDE. Each IDE has its own way to do things. l Read you manuals and consult a local expert. l You may be using a command line compiler. In that event, you may some thing like write: cc myProgram.cpp l Your compiler may require.cxx..cc or perhaps.C l Linking is usually automatic. Again, read your manuals and ask a local expert.

16 16 1.4 Testing and Debugging l An error in a program, whether due to design errors or coding errors, are known as bugs. Rear Admiral Grace Hopper is credited with this term. l Program Errors are classified as »design errors -- if you solved the wrong problem, you have design errors. »syntax errors -- violation of language’s grammar rules, usually caught by the compiler, and reported by compiler error messages. »run-time errors -- a program that compiles may die while running with a run-time error message that may or may not be useful. »logic error -- a program that compiles and runs to normal completion may not do what you want. May be a design error.

17 17 Kinds of Errors l Design errors occur when specifications are do not match the problem being solved. l The compiler detects errors in syntax l Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.

18 18 Summary l Hardware physical computing machines. l Software programs that run on hardware. l Five computer components: input and output devices, CPU, main memory, secondary memory. l There are two kinds of memory: main and secondary. Main memory is used when program is running. Secondary memory is usually nonvolatile. l Main memory is divided into bytes, usually individually addressable. l Byte: 8 bits. A bit is 0 or 1.

19 19 Summary(continued) l A compiler translates high level language to machine language. l Algorithm is a sequence of precise instructions that lead to a solution to a problem. l A solution to a problem begins with algorithm design. l Errors are of several kinds: syntax, run-time, and logic errors. l A variable is used to name a number. l cout << is an output statement l cin >> is an input statement


Download ppt "1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,"

Similar presentations


Ads by Google