
- •Vasyl Yatskiv,
- •Content
- •Introduction
- •1. Theory
- •1.1. Arithmetic Operators
- •1.2. Relational Operators
- •1.3. Logical Operators
- •1.4. Increment/Decrement Operators
- •1.5. Assignment Operator
- •1.6. Conditional Operator
- •1.7. Comma Operator
- •1.8. The sizeof Operator
- •1.9. Operator Precedence
- •1.10. Simple Type Conversion
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #2. Control structures
- •1. Theory
- •1.1. Simple and Compound Statements
- •1.2. The if Statement
- •1.3. The switch Statement
- •1.4. The while Statement
- •1.5. The do Statement
- •1.6. The for Statement
- •1.7. The continue Statement
- •1.8. The break Statement
- •1.9. The goto Statement
- •1.10. The return Statement
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •1. Theory
- •1.1. A Simple Function
- •1.2. Parameters and Arguments
- •1.3. Global and Local Scope
- •1.4. Scope Operator
- •1.5. Symbolic Constants
- •1.6. Inline Functions
- •1.7. Recursion
- •1.8. Default Arguments
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #4. Arrays, pointers, references and dynamic variables
- •1. Theory
- •1.1 Arrays
- •1.2 Multidimensional Arrays
- •1.3 Pointers
- •1.4 Dynamic Memory
- •1.5 Pointer Arithmetic
- •1.6 References
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #5. Structures
- •1. Theory
- •1.1 Introducing Structures
- •1.2 Using a Structure in a Program
- •1.3 Program Notes
- •1.4 Other Structure Properties
- •1.5 Arrays of Structures
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #6. Strings
- •1. Theory
- •1.1. Introduction to Strings
- •1.2. Concatenating String Constants
- •1.3. Using Strings in an Array
- •1.4. Reading String Input a Line at a Time
- •1.5. Line-Oriented Input with gets()
- •1.6. Introducing the string Class
- •1.7. Assignment, Concatenation, and Appending
- •1.8. More string Class Operations
- •1.9. More on string Class I/o
- •2. Lab Overview
- •3. Report Structure
- •4. Control Questions
- •5. References
- •Annex a Report’s Title Page
- •From discipline “Algorithmization and Programming” Topic: _______________________________________________
MINISTRY OF EDUCATION AND SCIENCE, YOUTH AND SPORT OF UKRAINE
TERNOPIL NATIONAL ECONOMIC UNIVERSITY (TNEU)
FACULTY OF COMPUTER INFORMATION TECHNOLOGIES
AMERICAN-UKRAINIAN SCHOOL OF COMPUTER SCIENCE
LAB MANUAL
of discipline “Algorithmization and Programming”
for students of specialty 6.050101 – “Computer Science”
Ternopil
2012
Lab manual of discipline “Algorithmization and Programming” / I.Paliy, M.Komar. – Ternopil. – 2012. – 68 p.
Approved by
Department of Information Computing Systems and Control Meeting,
Protocol # 11 of 15 May 2012
Author: Ihor Paliy,
PhD, Assistance Professor of the Department for Information Computing Systems and Control, TNEU
Myroslav Komar,
Lecturer of the Department for Information Computing Systems and Control, TNEU
Reviewers: Anatoly Sachenko,
DSc, Professor, Head of the Department for Information Computing Systems and Control, TNEU
Vasyl Yatskiv,
PhD, Associate Professor, Department of Specialized Computer Systems, TNEU
Content
INTRODUCTION |
4 |
Lab #1. C++ OPERATORS |
5 |
Lab #2. CONTROL STRUCTURES |
15 |
Lab #3. C++ USER-DEFINED FUNCTIONS |
27 |
Lab #4. ARRAYS, POINTERS, REFERENCES AND DYNAMIC VARIABLES |
37 |
Lab #5. STRUCTURES |
47 |
Lab #6. STRINGS |
55 |
|
|
Introduction
Since its introduction less than a decade ago, C++ has experienced growing acceptance as a practical object-oriented programming language suitable for teaching, research, and commercial software development. The language has also rapidly evolved during this period and acquired a number of new features (e.g., templates and exception handling) which have added to its richness. It is the basis for Java, JavaScript and C#.
This course is designed to present the basics of the language in a straight forward, easy to understand manner. It studies students the approaches for solving different problems using algorithms development and following computer programming. It teaches how to program in C++ and how to properly use its features. It does not attempt to teach object-oriented design to any depth.
At the completion of this course, the student will be able to
Design algorithmic solutions to problems;
Understand the structure of a C/C++ language program including the use of variable definitions, data types, functions, scope and operators;
Translate a specific algorithm into correct, good commented C++ code using generally accepted programming style using:
various forms of Input/Output including files;
assignments;
if-else logic;
while, do, and for loops;
functions;
arrays;
strings and string functions, etc.
Be able to test a program, find and correct compile-time, run-time and logic errors.
Lab #1. C++ OPERATORS
Goal: Learn how to program solutions for simple computing problems using C++ operators.
1. Theory
1.1. Arithmetic Operators
C++ provides operators for composing arithmetic, relational, logical, bitwise, and conditional expressions. It also provides operators which produce useful side-effects, such as assignment, increment, and decrement. We will look at each category of operators in turn. We will also discuss the precedence rules which govern the order of operator evaluation in a multi-operator expression.
C++ provides five basic arithmetic operators. These are summarized in the Table below.
Operator |
Name |
Example |
+ |
Addition |
12 + 4.9 // gives 16.9 |
- |
Subtraction |
3.98 - 4 // gives -0.02 |
* |
Multiplication |
2 * 3.4 // gives 6.8 |
/ |
Division |
9 / 2.0 // gives 4.5 |
% |
Remainder |
13 % 3 // gives 1 |
Except for remainder (%) all other arithmetic operators can accept a mix of integer and real operands. Generally, if both operands are integers then the result will be an integer. However, if one or both of the operands are reals then the result will be a real (or double to be exact).
When both operands of the division operator (/) are integers then the division is performed as an integer division and not the normal division we are used to. Integer division always results in an integer outcome (i.e., the result is always rounded down). For example:
9 / 2 // gives 4, not 4.5!
-9 / 2 // gives -5, not -4!
Unintended integer divisions are a common source of programming errors. To obtain a real division when both operands are integers, you should cast one of the operands to be real:
int cost = 100;
int volume = 80;
double unitPrice = cost / (double) volume; // gives 1.25
The remainder operator (%) expects integers for both of its operands. It returns the remainder of integer-dividing the operands. For example 13%3 is calculated by integer dividing 13 by 3 to give an outcome of 4 and a remainder of 1; the result is therefore 1.
It is possible for the outcome of an arithmetic operation to be too large for storing in a designated variable. This situation is called an overflow. The outcome of an overflow is machine-dependent and therefore undefined. For example:
unsigned char k = 10 * 92; // overflow: 920 > 255
It is illegal to divide a number by zero. This results in a run-time division-by-zero failure which typically causes the program to terminate.