Problem Solving With C++: The Object of Programming
Problem Solving With C++: The Object of Programming
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
Computer Systems Programming and Problem-Solving Introduction to C++ Testing and Debugging
CPU
High-Level Languages
Ada, C++, Java, BASIC, Lisp, Scheme
Compilers
Source program, object program, Linking
Algorithms
Idea is more general than program Hard part of solving a problem is finding the algorithm
Implementation phase
Translation to C++
Testing Working Program
5
Analysis and specification of task (problem definition). Design of the software (algorithm design) Implementation (coding). Testing Maintenance and evolution of the system Obsolescence
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; }
8
10
cout is the output stream, read See-Out.. It is attached to the monitor screen. << is the insertion operator cin is the input stream, read see-in, and is attached to the keyboard. >> is the extraction operator. Press return after entering a number.\n is called a cstring literal. It is a message for the user. cout << Press return \n sends the message to cout cin >> number_of_pods;
11
cout << Enter the number of peas in a pod.\n; cin >> peas_per_pod; The first of these lines sends a request to the user. The second extracts an integer value (the type of peas_per_pod) into peas_per_pod.
12
total_peas = number_of_pods * peas_per_pod; The asterisk, *, is used for multiplication. 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.
13
You write a C++ program using a text editor exactly as you would write a letter home. Compiling depends on the environment. You may be using an Integrated Development Environment or IDE. Each IDE has its own way to do things. Read you manuals and consult a local expert. You may be using a command line compiler. In that event, you may some thing like write: cc myProgram.cpp Your compiler may require .cxx. .cc or perhaps .C Linking is usually automatic. Again, read your manuals and 15 ask a local expert.
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. Program Errors are classified as
design errors -- if you solved the wrong problem, you have design errors. syntax errors -- violation of languages 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 16 may not do what you want. May be a design error.
Kinds of Errors
Design errors occur when specifications are do not match the problem being solved. The compiler detects errors in syntax Run-time errors result from incorrect assumptions, incomplete understanding of the programming language, or unanticipated user errors.
17
Summary
Hardware physical computing machines. Software programs that run on hardware. Five computer components: input and output devices, CPU, main memory, secondary memory. There are two kinds of memory: main and secondary. Main memory is used when program is running. Secondary memory is usually nonvolatile. Main memory is divided into bytes, usually individually addressable. Byte: 8 bits. A bit is 0 or 1.
18
Summary(continued)
A compiler translates high level language to machine language. Algorithm is a sequence of precise instructions that lead to a solution to a problem. A solution to a problem begins with algorithm design. Errors are of several kinds: syntax, run-time, and logic errors. A variable is used to name a number. cout << is an output statement cin >> is an input statement
19