COS1512 - Introduction To Programming II (COS1512)
Institution
University Of South Africa (Unisa)
Book
Problem Solving with C++, Global Edition
This summary has Chapters 1, 4-6, 8-12, 14-15 and 17. This reduced the pages from 606 to 52.
These notes are made for me to study for my exam so you can trust that this is for people who are getting this upcoming exam.
COS1512 - Introduction To Programming II (COS1512)
All documents for this subject (51)
2
reviews
By: dianeletcher • 1 month ago
By: serufemotloung29 • 1 year ago
Seller
Follow
LinkCroft
Reviews received
Content preview
Introduction to Programming II
COS1512
Chapter 1 - Introduction to Computers and C++ Programming
Summary
• The five main components of a computer are the input device(s), the output device(s), the processor
(CPU), the main memory, and the secondary memory.
• A computer has two kinds of memory: main memory and secondary memory. Main memory is used only
while the program is running. Secondary memory is used to hold data that will stay in the computer
before and/or after the program is run.
• A computer’s main memory is divided into a series of numbered locations called bytes. The number
associated with one of these bytes is called the address of the byte. Often, several of these bytes are
grouped together to form a larger memory location. In that case, the address of the first byte is used as
the address of this larger memory location.
• A byte consists of eight binary digits, each either zero or one. A digit that can only be zero or one is called
a bit.
• A compiler is a program that translates a program written in a high-level language like C++ into a program
written in the machine language that the computer can directly understand and execute.
• A sequence of precise instructions that leads to a solution is called an algorithm. Algorithms can be
written in English or in a programming language, like C++. However, the word algorithm is usually used to
mean a sequence of instructions written in English (or some other human language, such as Spanish or
Arabic).
• Before writing a C++ program, you should design the algorithm (method of solution) that the program will
use.
• Programming errors can be classified into three groups: syntax errors, runtime errors, and logic errors.
The computer will usually tell you about errors in the first two categories. You must discover logic errors
yourself.
• The individual instructions in a C++ program are called statements.
• A variable in a C++ program can be used to name a number. (Variables are explained more fully in the
next chapter.)
• A statement in a C++ program that begins with cout << is an output statement, which tells the computer
to output to the screen whatever follows the <<.
• A statement in a C++ program that begins with cin >> is an input statement.
,Chapter 4 - Procedural Abstraction and Functions That Returna Value
Overloading
If you have two or more function definitions for the same function name, that is called overloading. When you
overload a function name, the function definitions must have different numbers of formal parameters or some
formal parameters of different types. When there is a function call, the compiler uses the function definition
whose number of formal parameters and types of formal parameters match the arguments in the function call.
Example
4 double unitPrice(int diameter, double price);
5 //Returns the price per square inch of a round pizza.
6 //The formal parameter named diameter is the diameter of the pizza
7 //in inches. The formal parameter named price is the price of the
pizza.
9 double unitPrice(int length, int width, double price);
10 //Returns the price per square inch of a rectangular pizza
11 //with dimensions length by width inches.
12 //The formal parameter price is the price of the pizza.
,Chapter 5 - Functions for All Subtasks
GENERAL DEBUGGING TECHNIQUES
Keep an Open Mind
Examine the system as a whole and don’t assume that the bug occurs in on particular place. If the program is giving
incorrect output values, then you should examine the source code, different test cases for the input and output
values, and the logic behind the algorithm itself. For example, consider the code to determine price for the
supermarket example in Display 5.9. If the wrong price is displayed, the error might simply be that the input values
were different from those you were expecting in the test case, leading to an apparently incorrect program.
Some novice programmers will “randomly” change portions of the code hoping that it will fix the error. Avoid this
technique at all costs! Sometimes this approach will work for the first few simple programs that you write.
However, it will almost certainly fail for larger programs and will often introduce new errors to the program. Make
sure that you understand what logical impact a change to the code will make before committing the modification.
Finally, if allowed by your instructor, you could show the program to someone else. A fresh set of eyes can
sometimes quickly pinpoint an error that you have been missing. Taking a break and returning to the problem a
few hours later or the next day can also sometimes help in discovering an error.
Check Common Errors
One of the first mistakes you should look for are common errors that are easy
to make, as described throughout the textbook in the Pitfall and Programming
Tip sections. Examples of sources for common errors include (1) uninitialized
variables, (2) off-by-one errors, (3) exceeding a data boundary, (4) automatic
type conversion, and (5) using = instead of ==.
Localize the Error
Determining the precise cause and location of a bug is one of the first steps to fixing the error. Examining the input
and output behavior for different test cases is one way to localize the error. A related technique is to add cout
statements to strategic locations in the program that print out the values for critical variables. The cout statements
also serve to show what code the program is executing. This is the strategy of tracing variables that was described
in Chapter 3 for loops, but it can be used even when there are no loops present in the code.
The assert Macro
The assert macro is a tool to ensure that the expected conditions are true at the location of the assert statement. If
the condition is not met, then the program will display an error message and abort. To use assert, first include the
definition of assert in your program with the following include statement:
#include <cassert>
To use assert, add the following line of code at the location where you would like to enforce the assertion with a
boolean expression that should evaluate to true:
assert(boolean_expression);
, Chapter 6 - I/O Streams as an Introduction to Objects and Classes
Streams & Basic Files
Stream:
• Flow of characters
• Has Input and Output characters
Cin & Cout = streams
Example
int theNumber;
inStream >> theNumber;
outStream << "theNumber is" << theNumber << endl;
Declaring
you can declare inStream to be an input stream for a file and outStream to be an output stream for another file as
follows:
ifstream inStream;
ofstream outStream;
Path include is
#include <fstream>
Opening file would be
inStream.open("infile.dat");
and to connect it
ifstream inStream("infile.dat");
the following reads two input numbers from the file connected to inStream and places them in the variables
oneNumber and anotherNumber:
int oneNumber, anotherNumber;
inStream >> oneNumber >> anotherNumber;
An output stream is opened (that is, connected to a file) in the same way as just described for input streams. For
example, the following declares the output stream outStream and connects it to the file named outfile.dat:
ofstream outStream;
outStream.open("outfile.dat");
After a file is connected to the stream outStream with a call to open, the program can send output to that file
using the insertion operator <<.
outStream << "oneNumber = " << oneNumber
<< " anotherNumber = " << anotherNumber;
Every file should be closed when your program is finished getting input from the file or sending output to the file.
Closing a file disconnects the stream from the file. A file is closed with a call to the function close. The following
lines from the program in Display 6.1 illustrate how to use the function close:
inStream.close( );
outStream.close( );
Calling a Member Function
SYNTAX
Calling_Object.Member_Function_Name(Argument_List);
EXAMPLES
inStream.open("infile.dat");
outStream.open("outfile.dat");
outStream.precision(2);
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller LinkCroft. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $2.78. You're not tied to anything after your purchase.