Programming Using C++ - With Cover
Programming Using C++ - With Cover
Mahlet Teklay
January 2023
)
Mahlet Teklay
January 2023
i
Week 4:- Practice: ............................................................................................................................... 70
Assessments ......................................................................................................................................... 83
Week 5: ................................................................................................................................................. 84
2.5 Strings representation and manipulation ................................................................................... 84
2.5.1 Initialization of Strings.............................................................................................................. 85
2.5.2 Assigning Values to Strings ...................................................................................................... 86
2.5.3 String Output............................................................................................................................. 89
2.5.4 String Input ............................................................................................................................... 90
2.5.5 Reading multiple lines .............................................................................................................. 91
2.5.6 String constants ......................................................................................................................... 91
Week 5: Practice ................................................................................................................................... 93
Assessment ........................................................................................................................................... 99
Reference ............................................................................................................................................ 101
Chapter 3: Pointers ........................................................................................................................... 103
Week 6: ............................................................................................................................................... 104
3.1 Pointers Introduction ............................................................................................................... 104
3.2 Declaring Pointers ................................................................................................................... 109
3.3 Assigning values to pointers ................................................................................................... 109
3.4 Pointer to void ......................................................................................................................... 110
Week 6:- Practice ................................................................................................................................ 111
Assessment ......................................................................................................................................... 114
Week 7: ............................................................................................................................................... 115
3.5 Arrays of Pointers ................................................................................................................... 115
3.6 Pointer and arrays ................................................................................................................... 115
3.7 Pointer Advantage ................................................................................................................... 116
Week 7:- Practice ................................................................................................................................ 122
Assessments ....................................................................................................................................... 127
Reference ............................................................................................................................................ 128
Chapter 4: Function (Built in, User defined) .................................................................................. 129
Week 9: ............................................................................................................................................... 130
4.1 Modular programming and Modules ...................................................................................... 130
4.2 Declaring and Defining Functions .......................................................................................... 130
4.3 Defining the Function ............................................................................................................. 132
4.4 Scope of variables ................................................................................................................... 134
Week 9:- Practice ................................................................................................................................ 138
Assessments ....................................................................................................................................... 145
ii
Week 10: ............................................................................................................................................. 146
4.5 Function Arguments................................................................................................................ 146
4.6 Passing arguments ................................................................................................................... 147
4.7 Return Values.......................................................................................................................... 150
4.8 Default Parameters .................................................................................................................. 150
Week 10:- Practice .............................................................................................................................. 152
Assessment ......................................................................................................................................... 159
Week 11: ............................................................................................................................................. 162
4.9 Overloaded Functions ............................................................................................................. 162
4.10 Inline Functions ...................................................................................................................... 163
4.11 Recursive Functions ................................................................................................................ 165
Week 11 :- Practice ............................................................................................................................. 167
Assessment ......................................................................................................................................... 175
Reference ............................................................................................................................................ 179
Chapter 5: Structure......................................................................................................................... 181
Week 12 : ............................................................................................................................................ 182
5.1 Record: - Structure .................................................................................................................. 182
5.1.1 Structure .................................................................................................................................. 182
5.1.2 struct Specification: Defining Structures ................................................................................ 183
5.1.3 Declaring and using sturct data types ..................................................................................... 184
5.1.4 Initializing Structure Variables ............................................................................................... 185
5.1.5 Accessing members of a structure variable ............................................................................. 186
Week 12: Practice ............................................................................................................................... 187
Assessment ......................................................................................................................................... 194
Week 13: ............................................................................................................................................. 196
5.2 Array of structs ....................................................................................................................... 196
Week 13:- Practice .............................................................................................................................. 207
5.3 C++ Pointers to Structure........................................................................................................ 207
5.4 Pointers and Arrays ................................................................................................................. 208
Week 14: ............................................................................................................................................. 215
5.5 Passing Structure to Function ................................................................................................. 215
5.6 Returning Structures from Functions in C++ .......................................................................... 218
Week 14:- Practice: ............................................................................................................................. 221
Reference ............................................................................................................................................ 229
Chapter 6: File Management ........................................................................................................... 231
Week 15: ............................................................................................................................................. 232
iii
6.1 File Management Introduction ................................................................................................ 232
6.2 Streams and Files .................................................................................................................... 232
6.3 The standard streams............................................................................................................... 233
6.4 C++ File I/O Classes and Functions ....................................................................................... 233
6.5 Text and Binary Files .............................................................................................................. 234
6.6 Text File processing ................................................................................................................ 234
6.7 Reading from a File ................................................................................................................ 238
6.8 File Pointer and their Manipulators ........................................................................................ 240
6.9 Binary File processing ............................................................................................................ 243
6.10 Random Access Files .............................................................................................................. 248
Week 15 :- Practice: ............................................................................................................................ 251
Assesement ......................................................................................................................................... 254
Reference ............................................................................................................................................ 255
iv
Course Profile
Department Information Technology
Course title Programming Using C++
1
Detailed Course Outline
Topics to be covered Topics to be covered Required materials
Week Outcomes
(2 lecture hours) (3 lab hours) /equipment/devices
Chapter 1: Overview of programming basics
Basics of C++ • Installing a C++ IDE 5.1.1 PC, C++ compiler
Structure of C++ Program; • Visualize the structure LaserJet Printers
Keywords, Identifiers, Inputs, of a C++ program LCD Projectors
Outputs, Comments, environment
Parts of a program; Data Types, • Lab experiment to see
Variables, and Constants all the operators in
Operators; Assignment action inside a C++
1 program
Operators; Compound
Assignment Operators; • Declaring a variable
Arithmetic Operators; with the correct choice
Relational Operators; of data types
Increment and Decrement
Operators; Infix and postfix
types; Precedence of Operators
• if statements 5.1.1 PC, C++ compiler
implementation syntax 5.22 LaserJet Printers
Control Statements Looping • switch Statements LCD Projectors
2 Statements: for, while, do, break, implementation syntax
continue; nested loops • for loop, while loop
and do while loop
implementation syntax
Chapter 2: Arrays and String Manipulation
• one dimensional array 5.2.3 PC, C++ compiler
declaration LaserJet Printers
Array Definition: • Accessing content of LCD Projectors
3 one dimensional and one-dimensional array
multidimensional arrays cycle • Displaying the content
of one-dimensional
array using loops.
• Displaying the content 5.2.3 PC, C++ compiler
of two-dimensional LaserJet Printers
Application of array (array array using loops.
4 LCD Projectors
sorting, matrix) • Array traversing
• Array sorting
• string declaration in C 5.2.3 PC, C++ compiler
style LaserJet Printers
• string declaration in LCD Projectors
Strings:
C++ style
5 Accessing strings
• Accessing and
manipulating strings
• Apply string built in
functions
Chapter 3: Pointers
2
6 Address-of operator • Pointer declaration 5.1.4 PC, C++ compiler
Dereference operator • Pointer initialization LaserJet Printers
Declaration of pointers LCD Projectors
Arrays and pointers
7 Pointers arithmetic, variable • Dynamic memory 5.2.3 PC, C++ compiler
access, strings, pointer to allocation and other LaserJet Printers
pointers, invalid pointer and null concepts related with LCD Projectors
Pointers and pointer to function pointers
8 Review of the above topics Midterm Exam
Chapter 4: Function (Built in, User defined)
9 Introduction to Modular • Use different built in 5.2.4 PC, C++ compiler
programming (Functions) functions in C++ 5.1.3 LaserJet Printers
Scope of Variables • Declaration of LCD Projectors
function
• Definition of functions
• Calling function
10 Function Arguments • How to pass 5.2.4 PC, C++ compiler
Return Values Parameters by value 5.1.2 LaserJet Printers
Default Parameters • How to pass 5.1.3 LCD Projectors
Parameters passing Parameters by
reference
11 Function Overloading • How to use recursive 5.2.4 PC, C++ compiler
Recursive function functions LaserJet Printers
Inline function • Declaring inline LCD Projectors
functions
Chapter 5: Structures
12 Introduction • Declaring structure 5.2.4 PC, C++ compiler
Overview of Structure • Defining structure in LaserJet Printers
Array of structure structure LCD Projectors
Nested structure
13 Structure, Reference and Pointer • Initializing structure 5.2.4 PC, C++ compiler
• Manipulating structure LaserJet Printers
LCD Projectors
14 Passing structure to function • Passing value of 5.2.4 PC, C++ compiler
structure to a function LaserJet Printers
• Passing address of a LCD Projectors
structure to a function
Chapter 6: File Management
15 Streams and Files Text and • Creating a file in PC, C++ compiler
Binary Files different modes 5.1.6 LaserJet Printers
Binary File processing • Reading from file 5.2.5 LCD Projectors
Random Access Files • Writing on existed
Buffers and Synchronization files
• Text File processing
16 Final Examination period
3
4
Chapter 1: Overview of Programming Basics
Lesson Plan
1. Learning objectives
• Be able to apply the concept and application of iteration and recursion in program
development.
2. Motivation
• Outputs through Question and Answer; repetitive discussion and instruction of the C++
programming basics; individual internet search.
5
6. Assessments
• How to declare variables?
• How to apply flow control in programming?
• How can you install DEV C++ software?
7. Clean-up
After finishing practice, computers should be properly shut down.
8. Independent practice/Follow-up activities
• Review the outcome of the practice, improvement measure and previous reflected
opinions.
6
Week 1:
1.1 Structure of C++ Program
A C++ program has the following structure
[Comments]
[Preprocessor directives]
[Global variable declarations]
[Prototypes of functions]
[Definitions of functions]
Notice that main is not a reserved word. However, this is a fairly technical distinction, and for
practical purposes you are advised to treat main, cin, and cout as if they were reserved as well.
7
Identifiers
An identifier is name associated with a function or data object and used to refer to that function
or data object. An identifier must:
▪ Start with a letter or underscore
▪ Consist only of letters, the digits 0-9, or the underscore symbol _
▪ Not be a reserved word
Syntax of an identifier
Letter
Letter
- Digit
-
For the purposes of C++ identifiers, the underscore symbol, _, is considered to be a letter. Its
use as the first character in an identifier is not recommended though, because many library
functions in C++ use such identifiers. Similarly, the use of two consecutive underscore symbols,
_ _, is forbidden.
8
Literals are constant values which can be a number, a character of a string. For example the
number 129.005, the character ‘A’ and the string “hello world” are all literals. There is no
identifier that identifies them.
Comments
A comment is a piece of descriptive text which explains some aspect of a program. Program
comments are totally ignored by the compiler and are only intended for human readers. C++
provides two types of comment delimiters:
• Anything after // (until the end of the line on which it appears) is considered a comment.
• Anything enclosed by the pair /* and */ is considered a comment.
A point worth mentioning again here is that C++ is case-sensitive. In other words, uppercase
and lowercase letters are considered to be different. A variable named age is different from
Age, which is different from AGE.
Creating More Than One Variable at a Time
9
You can create more than one variable of the same type in one statement by writing the type
and then the variable names, separated by commas. For example:
int myAge, myWeight; // two int variables
As you can see, myAge and myWeight are each declared as integer variables. The second line
declares three individual long variables named area, width, and length. However keep in mind
that you cannot mix types in one definition statement.
Assigning Values to Your Variables
You assign a value to a variable by using the assignment operator (=). Thus, you would assign
5 to Width by writing
int Width;
Width = 5;
You can combine these steps and initialize Width when you define it by writing
int Width = 5;
Initialization looks very much like assignment, and with integer variables, the difference is
minor. The essential difference is that initialization takes place at the moment you create the
variable.
Just as you can define more than one variable at a time, you can initialize more than one
variable at creation.
For example:
This example initializes the integer variable width to the value 5 and the length variable to the
value 7. It is possible to even mix definitions and initializations:
This example creates three type int variables, and it initializes the first and third.
10
Several data types are built into C++. The varieties of data types allow programmers to select
the type appropriate to the needs of the applications being developed. The data types supported
by C++ can be classified as basic (fundamental) data types, user defined data types, derived
data types and empty data types. However, the discussion here will focus only on the basic data
types.
Basic (fundamental) data types in C++ can be conveniently divided into numeric and character
types. Numeric variables can further be divided into integer variables and floating-point
variables. Integer variables will hold only integers whereas floating number variables can
accommodate real numbers.
Both the numeric data types offer modifiers that are used to vary the nature of the data to be
stored. The modifiers used can be short, long, signed and unsigned.
The data types used in C++ programs are described in Table 1.4. This table shows the variable
type, how much room it takes in memory, and what kinds of values can be stored in these
variables. The values that can be stored are determined by the size of the variable types.
Width:5
Length: 10
Area: 50
Line 2 includes the required include statement for the iostream's library so that cout will work.
Line 4 begins the program.
On line 6, Width is defined as an unsigned short integer, and its value is initialized to 5. Another
unsigned short integer, Length, is also defined, but it is not initialized. On line 7, the value 10
is assigned to Length.
On line 11, an unsigned short integer, Area, is defined, and it is initialized with the value
obtained by multiplying Width times Length. On lines 13-15, the values of the variables are
printed to the screen. Note that the special word endl creates a new line.
Wrapping around integer values
The fact that unsigned long integers have a limit to the values they can hold is only rarely a
problem, but what happens if you do run out of room? When an unsigned integer reaches its
maximum value, it wraps around and starts over, much as a car odometer might. The following
example shows what happens if you try to put too large a value into a short integer.
12
4: unsigned short int smallNumber;
5: smallNumber = 65535;
6: cout<< "small number:" <<smallNumber<<endl;
7: smallNumber++;
8: cout<< "small number:" <<smallNumber<<endl;
9: smallNumber++;
10: cout<< "small number:" <<smallNumber<<endl;
11: return 0;
12: }
Output:
small number: 65535
small number:0
small number:1
A signed integer is different from an unsigned integer, in that half of the values you can
represent are negative. Instead of picturing a traditional car odometer, you might picture one
that rotates up for positive numbers and down for negative numbers. One mile from 0 is either
1 or -1. When you run out of positive numbers, you run right into the largest negative numbers
and then count back down to 0. The whole idea here is putting a number that is above the range
of the variable can create unpredictable problem.
Example: A demonstration of adding too large a number to a signed integer.
1: #include <iostream>
2. using namespace std
3: int main()
4: {
5: short int smallNumber;
6: smallNumber = 32767;
7: cout<< "small number:" <<smallNumber<<endl;
8: smallNumber++;
9: cout<< "small number:" <<smallNumber<<endl;
10: smallNumber++;
11 cout<< "small number:" <<smallNumber<<endl;
12: return 0;
13`: }
Output:
small number:32767
small number:-32768
small number:-32767
IMPORTANT – To any variable, do not assign a value that is beyond its range!
13
Characters
Character variables (type char) are typically 1 byte, enough to hold 256 values. A char can be
interpreted as a small number (0-255) or as a member of the ASCII set. ASCII stands for the
American Standard Code for Information Interchange. The ASCII character set and its ISO
(International Standards Organization) equivalent are a way to encode all the letters, numerals,
and punctuation marks.
In the ASCII code, the lowercase letter "a" is assigned the value 97. All the lower- and
uppercase letters, all the numerals, and all the punctuation marks are assigned values between
0 and 127. Another 128 marks and symbols are reserved for use by the computer maker,
although the IBM extended character set has become something of a standard.
Characters and Numbers
When you put a character, for example, `a', into a char variable, what is really there is just a
number between 0 and 255. The compiler knows, however, how to translate back and forth
between characters (represented by a single quotation mark and then a letter, numeral, or
punctuation mark, followed by a closing single quotation mark) and one of the ASCII values.
The value/letter relationship is arbitrary; there is no particular reason that the lowercase ‘a’ is
assigned the value 97. As long as everyone (your keyboard, compiler, and screen) agrees, there
is no problem. It is important to realize, however, that there is a big difference between the
value 5 and the character `5'. The latter is actually valued at 53, much as the letter ‘a’ is valued
at 97.
1.4 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.
Assignment Operators
The assignment operator is used for storing a value at some memory location (typically denoted
by a variable). Its left operand should be an lvalue, and its right operand may be an arbitrary
expression. The latter is evaluated and the outcome is stored in the location denoted by the
lvalue.
An lvalue (standing for left value) is anything that denotes a memory location in which a value
may be stored. The only kind of lvalue we have seen so far is a variable. Other kinds of lvalue
14
(based on pointers and references) will be described later. The assignment operator has a
number of variants, obtained by combining it with the arithmetic and bitwise operators.
An assignment operation is itself an expression whose value is the value stored in its left
operand. An assignment operation can therefore be used as the right operand of another
assignment operation. Any number of assignments can be concatenated in this fashion to form
one expression. For example:
int m, n, p;
m = n = p = 100; // means: n = (m = (p = 100));
m = (n = p = 100) + 2; // means: m = (n = (p = 100)) + 2;
This is equally applicable to other forms of assignment. For example:
m = 100;
m += n = p = 10; // means: m = m + (n = p = 10);
Arithmetic Operators
C++ provides five basic arithmetic operators. These are summarized in table below
Table 1.5: Arithmetic Operator
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 // 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 real numbers then the result will be a real (or double to be
exact).
15
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:
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:
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:
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.
There are also a number of predefined library functions, which perform arithmetic operations.
As with input & output statements, if you want to use these you must put a #include statement
at the start of your program.
Parameter Result
Function Result
Header File Type(s) Type
16
<math.h> sin(x) float float Sine of x (x is in radians)
Relational Operators
C++ provides six relational operators for comparing numeric quantities. These are summarized
in table below. Relational operators evaluate to 1 (representing the true outcome) or 0
(representing the false outcome).
Table 1.7: Relational Operators
Operator Name Example
== Equality 5 == 5 // gives 1
!= Inequality 5 != 5 // gives 0
Relational operators
Note that the <= and >= operators are only supported in the form shown. In particular, =< and
=> are both invalid and do not mean anything.
The operands of a relational operator must evaluate to a number. Characters are valid operands
since they are represented by numeric values. For example (assuming ASCII coding):
'A' < 'F' // gives 1 (is like 65 < 70)
The relational operators should not be used for comparing strings, because this will result in
the string addresses being compared, not the string contents. For example, the expression
"HELLO" < "BYE" causes the address of "HELLO" to be compared to the address of "BYE".
17
As these addresses are determined by the compiler (in a machine-dependent manner), the
outcome may be 0 or 1, and is therefore undefined. C++ provides library functions (e.g., strcmp)
for the lexicographic comparison of string.
Logical Operators
C++ provides three logical operators for combining logical expression. These are summarized
in the table below. Like the relational operators, logical operators evaluate to 1 or 0.
Logical operators
Logical negation is a unary operator, which negates the logical value of its single operand. If
its operand is nonzero it produces 0, and if it is 0 it produces 1.
Logical and produces 0 if one or both of its operands evaluate to 0. Otherwise, it produces 1.
Logical or produces 0 if both of its operands evaluate to 0. Otherwise, it produces 1.
Note that here we talk of zero and nonzero operands (not zero and 1). In general, any nonzero
value can be used to represent the logical true, whereas only zero represents the logical false.
The following are, therefore, all valid logical expressions:
!20 // gives 0
10 && 5 // gives 1
10 || 5.5 // gives 1
10 && 0 // gives 0
C++ does not have a built-in boolean type. It is customary to use the type int for this purpose
instead. For example:
int sorted = 0; // false
int balanced = 1; // true
Increment/decrement Operators
The auto increment (++) and auto decrement (--) operators provide a convenient way of,
respectively, adding and subtracting 1 from a numeric variable. These are summarized in the
following table. The examples assume the following variable definition:
18
int k = 5;
Both operators can be used in prefix and postfix form. The difference is significant. When used
in prefix form, the operator is first applied and the outcome is then used in the expression.
When used in the postfix form, the expression is evaluated first and then the operator applied.
Both operators may be applied to integer as well as real variables, although in practice real
variables are rarely useful in this form.
Precedence of Operators
The order in which operators are evaluated in an expression is significant and is determined by
precedence rules. These rules divide the C++ operators into a number of precedence levels.
Operators in higher levels take precedence over operators in lower levels.
Table 1.10: Order of Precedence
Level Operator Kind Order
- -- ~ & delete
19
Level Operator Kind Order
-= /= %= |= >>=
For example, in
a==b+c*d
c * d is evaluated first because * has a higher precedence than + and ==. The result is then
added to b because + has a higher precedence than ==, and then == is evaluated. Precedence
rules can be overridden using brackets. For example, rewriting the above expression as
a == (b + c) * d
causes + to be evaluated before *.
20
Operators with the same precedence level are evaluated in the order specified by the last
column of Table 1.10. For example, in
a = b += c
the evaluation order is right to left, so first b += c is evaluated, followed by a = b.
21
Week 1:- Practice:
C++ IDE
The complete development cycle in C++ is: Write the program, compile the source code, link
the program, and run it.
Writing a Program
To write a source code, your compiler may have its own built-in text editor, or you may be
using a commercial text editor or word processor that can produce text files. The important
thing is that whatever you write your program in, it must save simple, plain-text files, with no
word processing commands embedded in the text. Examples of safe editors include Windows
Notepad, the DOS Edit command. Many commercial word processors, such as WordPerfect,
Word, and dozens of others, also offer a method for saving simple text files.
The files you create with your editor are called source files, and for C++ they typically are
named with the extension .CPP.
Compiling
Your source code file can't be executed, or run, as a program can. To turn your source code
into a program, you use a compiler. How you invoke your compiler, and how you tell it where
to find your source code, will vary from compiler to compiler; check your documentation. In
Borland's Turbo C++ you pick the RUN menu command or type
tc<filename>
from the command line, where <filename> is the name of your source code file (for example,
test.cpp). Other compilers may do things slightly differently. After your source code is
compiled, an object file is produced. This file is often named with the extension .OBJ. This is
still not an executable program, however. To turn this into an executable program, you must
run your linker.
Linking
C++ programs are typically created by linking together one or more OBJ files with one or more
libraries. A library is a collection of linkable files that were supplied with your compiler, that
you purchased separately, or that you created and compiled. All C++ compilers come with a
library of useful functions (or procedures) and classes that you can include in your program. A
function is a block of code that performs a service, such as adding two numbers or printing to
the screen. A class is a collection of data and related functions.
Summary
The steps to create an executable file are
22
1. Create a source code file, with a .CPP extension.
2. Compile the source code into a file with the .OBJ extension.
3. Link your OBJ file with any needed libraries to produce an executable program.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout<< "Hello World!\n";
6. return 0;
7. }
On line 1, the file iostream is included in the file. The first character is the # symbol, which is
a signal to the preprocessor. Each time you start your compiler, the preprocessor is run. The
preprocessor reads through your source code, looking for lines that begin with the pound
symbol (#), and acts on those lines before the compiler runs.
include is a preprocessor instruction that says, "What follows is a filename. Find that file and
read it in right here." The angle brackets around the filename tell the preprocessor to look in
all the usual places for this file. If your compiler is set up correctly, the angle brackets will
cause the preprocessor to look for the file iostream.h in the directory that holds all the H files
for your compiler. The file iostream.h (Input-Output-Stream) is used by cout, which assists
with writing to the screen. The effect of line 1 is to include the file iostream.h into this program
as if you had typed it in yourself.
The preprocessor runs before your compiler each time the compiler is invoked. The
preprocessor translates any line that begins with a pound symbol (#) into a special command,
getting your code file ready for the compiler.
Line 3 begins the actual program with a function named main(). Every C++ program has a
main() function. In general, a function is a block of code that performs one or more actions.
Usually functions are invoked or called by other functions, but main() is special. When your
program starts, main() is called automatically.
main(), like all functions, must state what kind of value it will return. The return value type for
main() in HELLO.CPP is int, which means that this function will return an integer value.
All functions begin with an opening brace ({) and end with a closing brace (}). The braces for
the main() function are on lines 4 and 7. Everything between the opening and closing braces is
considered a part of the function.
23
Assessments
I. Practical
1. Write C++ program which accepts any three integers from user and calculates the sum
and average of the three numbers.
2. Write a C++ program that accepts your information and display in the following format.
Name: ________________________
Department: ____________________
Year: __________________________
Semester: ________________________
3. Write a C++ program for XYZ supermarket that computes transactions in the following
format.
Given: VAT=10%total
Service Charge=2%total
XYZ Supermarket
Pasta: __________________________________
Macaroni: ______________________________
Soap: _________________________________
Milk: _________________________________
Total: ________________________________
VAT: ___________________________________
Service Charge: __________________________
Total Payment: __________________________
THANK YOU!!!
2. Which one of the following function is the point where all C++ programs start its
execution
A. main() C. cout<<
24
B. return 0; D. cin>>
3. Every statement or instruction of a C++ statement must end with one of the following.
A. : (colon) C. , (comma)
B. ; (semicolon) D. { } (curly brackets)
4. Which one of the following is NOT true about C++ Identifiers?
A. Must begin with a letter or underscore or digits.
B. It should not be a reserved (key) word.
C. It can be a combination of letters, digits and the underscore characters.
D. It should not start with digits
5. Which one of the following is INVALID identifier?
A. _xyz C. int
B. xyz9 D. xy_9
6. Which one of the following is NOT a correct variable declaration?
A. Int x; C. x float;
B. int y,z; D. float x;
7. Which one of the following data type is used to represent textual information?
A. int C. string
B. float D. char
8. What will be the output of the following statement when it is executed inside the main
function?
{ cout<<(11%2); }
A. 0 C. 5.5
B. 1 D.5
9. Which one of the following statement has a correct syntax?
A. cin<<x; C. cin>>x;
B. cint<x; D. cin>x
10. What will be the output of the following statement when it is executed with in the main
function?
{ cout<<((5>4)&&(4>5)); }
A. 5 C.1
B. 4 D.0
25
Week 2:
The if Statement
It is sometimes desirable to make the execution of a statement dependent upon a condition
being satisfied.
The different forms of the ‘If” statement will be used to decide whether to execute part of the
program based on a condition which will be tested either for TRUE or FALSE result.
The different forms of the “if” statements are:
• The simple if statement
• The If else statement
• The if else if statement
The simple if statement
The simple if statement will decide only one part of the program to be executed if the condition
is satisfied or ignored if the condition fails.
• The General Syntax is:
if (expression)
26
statements;
In any “if” statement, first the “expression” will be evaluated and if the outcome is non zero
(which means TRUE), then the “statements” is executed. Otherwise, nothing happens (the
statement will not be executed) and execution resumes to the line immediately after the “if”
block.
To make multiple statements dependent on the same condition we can use a compound
statement, which will be implemented by embracing the group of instructions within the left
“{“ and right “}” French bracket.
Eg:
if(age>18)
cout<<”you are an adult”;
Eg.:
if(balance > 0)
{
interest = balance * creditRate;
balance += interest;
}
Most of the time “expression” will have relational expressions testing whether something is
equal, greater, less, or different from something else.
It should be noted that the output of a relational expression is either True (represented by
anything different from zero) or False (represented by Zero).
Thus any expression, whose final result is either zero or non-zero can be used in ”expression”
Eg:
int x;
cin >>x;
if(x)
cout <<”you are an adult”;
In the above example, the value of variable x will be an input from the user. The “if” statement
will be true if the user gives anything different from zero, which means the message “you are
an adult” will be displayed on the screen. If the user gives zero value for x, which will be
interpreted as False, nothing will be done as the if statement is not satisfied.
Thus, expression can be:
• Relational expression,
• A variable,
• A literal constant, or
• Assignment operation, as the final result is whatever we have at the right hand side and
the one at the left hand side is a variable.
27
• The General Syntax is:
if (expression)
statement1;
else
statment2;
First “expression” is evaluated and if the outcome is none zero (true), then “statements1” will
be executed. Otherwise, which means the “expression” is false “statements2” will be executed.
E.g.:
if(balance > 0)
{
interest = balance * creditRate;
balance += interest;
}
else
{
interest = balance * debitRate;
balance += interest;
}
28
constants in the case labels, in the order they appear, until a match is found. Note, however,
that the evaluation of the switch tag with the case labels is only for equality. The statements
following the matching case are then executed. Note the plural: each case may be followed by
zero or more statements (not just one statement). After one case is satisfied, execution
continues until either a break statement is encountered or all intervening statements are
executed, which means until the execution reaches the right French bracket of the switch
statement. The final default case is optional and is exercised if none of the earlier cases provide
a match. This means that, if the value of the “expression” is not equal to any of the case labels,
then the statements under default will be executed. Now let us see the effect of including a
break statement in the switch statement.
Scenario One
switch (N)
{
case 1: x=10;
case 2: x=20;
case 3: x=30;
}
Even if N is 1 or 2 x will have 30
Scenario Two
switch(N)
{
case 1: x=10; break;
case 2: x=20; break;
case 3: x=30; break;
}
X will have either 10, 20 or 30 based on the value of N
29
for(expression1 ; expression2 ; expression3)
statements;
The for loop has three expressions:
Expression1: is one or more statements that will be executed only once and before the looping
starts.
Expression2: is the part that decides whether to proceed with executing the instructions in the
loop or to stop. Expression2 will be evaluated each time before the loop continues. The output
of expression2 should be either zero (to proceed with the loop) or none zero (to stop the loop)
to represent false and true output respectively.
Expression3: is one or more statements that will be executed after each iteration. Thus, first
expression1 is evaluated and then each time the loop is executed, expression2 is evaluated. If
the outcome of expression2 is nonzero, then statements are executed and expression3 is
evaluated. Otherwise, the loop is terminated.
In most programs, the “for loop” will be used for such expressionswhereexpression1 is
initialization, expression2 is condition andexpression3is either increment or decrement.
The general format can be expressed as follows for the sake of clarity:
for(initialization; condition; increase/decrease)
statement;
Steps of execution of the for loop:
1. Initialization is executed. (Will be executed only once)
2. Condition is checked, if it is true the loop continues, otherwise the loop finishes and
statement is skipped.
3. Statement is executed.
4. Finally, whatever is specified in the increase or decrease field is executed and the loop gets
back to step 2.
30
Eg:
for (;n<10;) //if we want nether initialization nor increase/decrease
for (;n<10;n++) //if no initialization is needed.
for ( ; ; ) //is an infinite loop unless an otherwise there is if statement inside the loop.
It is declared above that expression1 and expression3 can be one or more statements. The
composite statements should be separated by comma. This means, optionally, using the comma
operator (,) we can specify more than one instruction in any of the two fields included in a “for”
loop.
Eg:
for(n=0,i=100;n!=i; n++,i--)
{
//what ever here
}
In the above example, n=0 and i=100 will be part of expression1and will be executed only once
before the loop starts. In addition, n++ and i-- will be part of expression3 and will be executed
after each looping/iteration.
Eg:1
//The following for statement adds the numbers between 0 and n
int Sum=0;
for(inti=0; i<=n;i++)
Sum=Sum+i;
Eg:2
//the following for statement adds the even numbers between 0 and n
int Sum=0;
for(int i=0; i<=n;)
{
Sum=Sum+i;
i+=2;
}
Eg:3
//the following for statement adds the even numbers between 0 and n
//where all the three expressions are null.
int Sum=0;
int i=0;
for( ; ; )
{
if(i<=n)
break;
else
{
Sum=Sum+i;
31
i++;
}
}
In the above example, the initialization is at the top before the looping starts, the condition is
put in if statement before the instructions are executed and the increment is found immediately
after the statements to be executed in the loop.
NB: even though there is the option of making all the three expressions null in a “for” loop, it
is not recommended to make the condition to take null statement.
The while statement
The while statement (also called while loop) provides a way of repeating a statement or a block
as long as a condition holds / is true.
The general form of the while loop is:
while(expression)
statements;
First expression (called the loop condition) is evaluated. If the outcome is non zero then
statement (called the loop body) is executed and the whole process is repeated. Otherwise, the
loop is terminated.
Suppose that we wish to calculate the sum of all numbers from 1 to some integer value n. this
can be expressed as:
Eg1:
// adds the numbers between 0 and any given number n
i=1;
sum = 0;
while(i<= n)
sum += i++;
Eg2:
//adds the numbers between 0 and 100
number=1;
sum=0;
while(number <= 100)
{
sum += number;
number++;
}
E.g.3:
32
/*the following while loop will request the user to enter his/her age which should be between
0 and 130. If the user enters a value which is not in the range, the while loop test the value and
request the age again until the user enters a valid age.*/
cout<<“\n enter your age [between 0 and 130]:”;
cin>>age;
while(age < 0 || age > 130)
{
cout<<“\n invalid age. Plz try again!”;
cin>>age;
}
do…while loop
The do statement (also called the do loop) is similar to the while statement, except that its body
is executed first and then the loop condition is examined.
In do…while loop, we are sure that the body of the loop will be executed at least once. Then
the condition will be tested.
First statement is executed and then expression is evaluated. If the outcome of the expression
is nonzero, then the whole process is repeated. Otherwise the loop is terminated.
E.g.:
//our previous example (Eg3) in the while loop might be changed as:
age=-1;
do
{
cout<<“\n enter your valid age [between 0 and 130]:”;
cin>>age;
}
while(age < 0 || age > 130);
33
Pitfalls in witting repetition statements
There are some pitfalls worth mentioning in repletion statements. These pit falls are the most
common programming errors committed by programmers ’Infinite loop: no matter what you
do with the while loop (and other repetition statements), make sure that the loop will eventually
terminate.
Eg1:
//Do you know why the following is an infinite loop?
int product = 0;
while(product < 50)
product *= 5;
Eg2:
//Do you know why the following is an infinite loop?
int counter = 1;
while(counter != 10)
counter += 2;
In the first example, since product is initialized with zero, the expression “product*=5” will
always give us zero which will always be less than 50.
In the second example, the variable counter is initialized to 1 and increment is 2, counter will
never be equal to 10 as the counter only assumes odd values. In theory, this while loop is an
infinite loop, but in practice, this loop eventually terminates because of an overflow error as
counter is an integer it will have a maximum limit.
Off-By-One Bugs (OBOB): another thing for which you have to watch out in writing a loop
is the so called Off-By-One Bugs or errors. Suppose we want to execute the loop body 10 times.
Does the following code work?
Eg:1
count = 1;
while(count < 10)
{
…
count++;
}
No, the loop body is executed nine times. How about the following?
Eg:2
count = 0;
while(count <= 10)
{
…
count++;
}
No this time the loop body is executed eleven times. The correct is
E.g:3
34
count = 0;
while(count < 10)
{
…
count++;
}
or
count = 1;
while(count <= 10)
{
…
count++;
}
Types of Loops
Count controlled loops
Repeat a statement or block a specified number of times Count-controlled loops contain
• An initialization of the loop control variable
• An expression to test if the proper number of repetitions has been completed
• An update of the loop control variable to be executed with each iteration of the body
The continue and break statements
The continue statement
The continue statement terminates the current iteration of a loop and instead jumps to the next
iteration. It is an error to use the continue statement outside a loop. In while & do while loops,
the next iteration commences from the loop condition.
In a “for” loop, the next iteration commences from the loop’s third expression.
Eg:
for(int n=10;n>0;n--)
{
if(n==5)
continue; //causes a jump to n
cout<<n<< “,”;
}
When the continue statement appears inside nested loops, it applies to the loop immediately
enclosing it, and not to the outer loops. For example, in the following set of nested loops, the
continue statement applies to the “for” loop, and not to the “while” loop.
while(more)
{
for(i=0;i<n;i++)
{
cin>>num;
if(num<0)
continue; //causes a jump to :i++
}
35
}
36
Week 2:- Practice:
If statement
The simple if statement will decide only one part of the program to be executed if the condition
is satisfied or ignored if the condition fails.
The General Syntax is:
if (expression)
statements;
#include <iostream>
using namespace std;
int main ()
{
int x = 10;
if( x< 20 )
{
cout<<"This is if statement";
}
return 0;
}
if….else statement
Another form of the “if” is the “if…else” statement.
The “if else if” statement allows us to specify two alternative statements: One which will be
executed if a condition is satisfied and another which will be executed if the condition is not
satisfied.
The General Syntax is:
if (expression)
statement1;
else
statment2;
#include <iostream>
using namespace std;
int main ()
{
37
int x = 30;
if( x< 20 )
{
cout<<"This is if statement";
}
else
{
cout<<"This is else statement";
}
}
Eg.
#include <iostream>
using namespace std;
int main ()
{
int x = 30;
if( x == 10 )
{
cout<<"Value of X is 10";
}
else if( x == 20 )
{
cout<<"Value of X is 20";
}
else if( x == 30 )
{
cout<<"Value of X is 30";
}
else
{
cout<<"This is else statement";
}
38
}
switch statement:
Another C++ statement that implements a selection control flow is the switch statement
(multiple-choice statement). The switch statement provides a way of choosing between a set of
alternatives based on the value of an expression.
The switch statement has four components:
• switch
• case
• default
• break
Where Default and Break are Optional.
Eg 1.
#include <iostream>
using namespace std;
int main ()
{
char grade = 'C';
switch(grade)
{
case 'A' :
cout<<"Excellent!";
39
break;
case 'B' :
cout<<"Very Good!";
break;
case 'C' :
cout<<"Well done";
break;
case 'D' :
cout<<"You passed";
break
case 'F' :
cout<<"Better try again";
break;
default :
cout<<"Invalid grade";
}
cout<<"Your grade is " + grade;
}
for loop:
The “for” statement (also called loop) is used to repeatedly execute a block of instructions until
a specific condition fails.
The General Syntax is:
for(initialization ; condition ; increase/decrease)
statement;
Eg 1.
#include <iostream>
using namespace std;
int main ()
{
for(int i=1; i<11; i++)
{
cout<<"Count is:" <<i<<"\n";
}
}
while Loop
The while statement (also called while loop) provides a way of repeating a statement or a block
as long as a condition holds / is true.
The general form of the while loop is:
while(expression)
statements;
Eg1:
#include <iostream>
using namespace std;
int main ()
{
40
int count = 1;
while (count < 11)
{
cout<<"Count is: " <<count<<"\n";
count++;
}
}
Eg 2:
#include <iostream>
using namespace std;
int main ()
{
int count = 5;
while (count < 4)
{
cout<<"Count is: " <<count<<"\n";
count++;
}
}
Do -- while Loop
The do statement (also called the do loop) is similar to the while statement, except that its body
is executed first and then the loop condition is examined.In do…while loop, we are sure that
the body of the loop will be executed at least once. Then the condition will be tested.
The general form is:
do
{
statement;
}
while(expression);
Eg 1
#include <iostream>
using namespace std;
int main ()
{
int count = 1;
do
{
cout<<"Count is: " << count<<"\n";
count++;
}
while (count < 11);
}
41
Assessment
I. Practical Questions
1. Write a C++ program which receive a number and determine whether it is odd or even.
2. Write a C++ program which calculates sum, difference, product and quotient of two
integers by accepting from key board. Use switch for each arithmetic calculation (case 1:
for Addition, case 2: for subtraction, case 3: for multiplication and case 4 for division.
3. Write a C++ program which displays integers from 1to 50 in ascending and descending
order using for, while, and do while loops.
4. Write a C++ program which adds odd numbers between 1 to 100.
5. Write a C++ program that accepts marks of five students and then displays their average.
The program should not accept mark which is less than 0 and mark greater than 100.
6. Write for, do-while, and while statements to compute the following sums and products.
a. 1+2+3+…+100
b. 5+10+15+…+50
c. 1+1/2+1/3+1/4+…1/15
d. 1*2*3*…*20
7. write an application to print out the numbers 10 through 49 in the following manner 10 11
12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
8. A prime number is an integer greater than one and divisible only by itself and one. The
first seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all
the prime numbers between 1 and 100.
9. Write a C++ program that counts the number of digits in an integer number. For example;
23,498 has five digits.
10. Write a C++ application that can compute the letter grade of a student after accepting the
student’s mid and final mark. The program should only accept mid result [0-40] and final
[0- 60]. If the data entered violates this rule, the program should display that the user should
enter the mark in the specified range. The program is also expected to run until the user
refuses to continue.
11. Write a C++ program that accepts a positive number from the user and displays the
factorial of that number. Use for loops to find the factorial of the number.
12. Write a C++ code that computes the sum of the following series.
Sum = 1! + 2! + 3! + 4! + …n!
The program should accept the number from the user.
13. Using the ASCII table numbers, write a program to print the following output, using a
nested for loop. (Hint: the outer loop should loop from 1 to 5, and the inner loop’s start
variable should be 65, the value of ASCII “A”).
A
AB
ABC
42
ABCD
ABCDE
14. Write a C++ program that displays the following output using their ASCII values.
a
bc
def
gehi
jklmn
opqrst
15. Write a C++ program that will print the following shapes.
A.
*
**
***
****
*****
B.
*****
****
***
**
*
C.
*
***
*****
*******
*********
D.
*
***
*****
***
*
16. Write a weather-calculator program that asks for a list of the previous 10 days’
temperatures, computes the average, and prints the results. You have to compute the total
as the input occurs, then divide that total by 10 to find the average. Use a while loop for the
10 repetitions.
17. Write a C++ program that accepts marks of five students and then displays their average.
The program should not accept mark which is less than 0 and mark greater
Than 100.
43
1. What is flow control in programming?
2. List three types of selection statements and their syntax.
3. List some types of looping statements.
44
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill,
2003
[3]. P B Mahapatra, Programming in C++, S Chand, 2008
[4]. Ashok Kamthane, Programming in C++, Pearson, 2013
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com/
45
Chapter 2: Arrays and String Manipulation
Lesson Plan
1. Learning objectives
• Outputs through Question and Answer; repetitive discussion and using array variables;
47
• Learning through assignment (practical Exercises)
9. Review/Reflection
• Review the outcome of the practice, improvement measure and previous reflected
opinions.
48
Week 3:
2.1 Introduction
Variables in a program have values associated with them. During program execution these
values are accessed by using the identifier associated with the variable in expressions etc. In
none of the programs written so far have very many variables been used to represent the values
that were required. Thus even though programs have been written that could handle large lists
of numbers it has not been necessary to use a separate identifier for each number in the list.
This is because in all these programs it has never been necessary to keep a note of each number
individually for later processing. For example, in summing the numbers in a list only one
variable was used to hold the current entered number which was added to the accumulated sum
and was then overwritten by the next number entered. If that value were required again later in
the program, there would be no way of accessing it because the value has now been overwritten
by the later input.
If only a few values were involved a different identifier could be declared for each variable,
but now a loop could not be used to enter the values. Using a loop and assuming that after a
value has been entered and used no further use will be made of it allows the following code to
be written. This code enters six numbers and outputs their sum:
sum = 0.0;
for (i = 0; i < 6; i++)
{
cin >> x;
sum += x;
}
This of course is easily extended to n values where n can be as large as required. However, if
it was required to access the values later the above would not be suitable. It would be possible
to do it as follows by setting up six individual variables:
float a, b, c, d, e, f;
and then handling each value individually as follows:
sum = 0.0;
cin >> a; sum += a;
cin >>b; sum += b;
cin >>c; sum += c;
cin >> d; sum += d;
cin >> e; sum += e;
cin>>f; sum += f;
Which is obviously a very tedious way to program. To extend this solution so that it would
work with more than six values then more declarations would have to be added, extra
assignment statements added and the program re-compiled. If there were 10000 values imagine
49
the tedium of typing the program (and making up variable names and remembering which is
which)!
To get round this difficulty all high-level programming languages use the concept of a data
structure called an Array.
Properties of arrays:
• Arrays in C++ are zero-bounded; that is the index of the first element in the array is 0
and the last element is N-1, where N is the size of the array.
• It is illegal to refer to an element outside of the array bounds, and your program will
crash or have unexpected results, depending on the compiler.
• Array can only hold values of one type
50
2.3.1 Declaration of one Dimensional Array
An array declaration is very similar to a variable declaration. First a type is given for the
elements of the array, then an identifier for the array and, within square brackets, the number
of elements in the array. The number of elements must be an integer.
For example, data on the average temperature over the year in Ethiopia for each of the last 100
years could be stored in an array declared as follows:
float annual_temp[100];
This declaration will cause the compiler to allocate space for 100 consecutive float variables
in memory. The number of elements in an array must be fixed at compile time. It is best to
make the array size a constant and then, if required, the program can be changed to handle a
different size of array by changing the value of the constant,
Constint NE = 100;
float annual_temp[NE];
then if more records come to light it is easy to amend the program to cope with more values by
changing the value of NE. This works because the compiler knows the value of the constant
NE at compile time and can allocate an appropriate amount of space for the array. It would not
work if an ordinary variable was used for the size in the array declaration since at compile time
the compiler would not know a value for it.
Declaring the name and type of an array and setting the number of elements in an array is called
dimensioning the array. The array must be declared before one uses in like other variables. In
the array declaration one must define:
1. The type of the array (i.e. integer, floating point, char etc.)
2. Name of the array,
3. The total number of memory locations to be allocated or the maximum value of each
subscript i.e. the number of elements in the array.
The general syntax for the declaration is:
Note: array size cannot be a variable whose value is set while the program is running.
Thus to declare an integer with size of 10 having a name of num is: int num [10];
This means: ten consecutive two byte memory location will be reserved with the name num.
That means, we can store 10 values of type int without having to declare 10 different variables
each one with a different identifier. Instead of that, using an array we can store 10 different
values of the same type, int for example, with a unique identifier.
51
2.3.2 Accessing Array Elements
In any point of the program in which the array is visible we can access individually
anyone of its elements for reading or modifying it as if it was a normal variable. To
access individual elements, index or subscript is used. The format is the following:
name [index]
In C++ the first element has an index of 0 and the last element has an index, which is one less
the size of the array (i.e. arraysize-1). Thus, from the above declaration, day[0] is the first
element and day[4] is the last element.
Following the previous examples where day had 5 elements and each element is of type int,
the name, which we can use to refer to each element, is the following one:
For example, to store the value 75 in the third element of the array variable day a suitable
sentence would be: day[2] = 75; //as the third element is found at index 2
And, for example, to pass the value of the third element of the array variable day to the variable
a , we could write:
a = day[2];
Therefore, for all the effects, the expression day[2] is like any variable of type int with the same
properties. Thus an array declaration enables us to create a lot of variables of the same type
with a single declaration and we can use an index to identify individual elements.
Notice that the third element of day is specified day[2] , since first is day[0] , second day[1] ,
and therefore, third is day[2] . By this same reason, its last element is day [4]. Since if we wrote
day [5], we would be acceding to the sixth element of day and therefore exceeding the size of
the array. This might give you either error or unexpected value depending on the compiler.
In C++ it is perfectly valid to exceed the valid range of indices for an Array, which can cause
certain detectable problems, since they do not cause compilation errors but they can cause
unexpected results or serious errors during execution. The reason why this is allowed will be
seen ahead when we begin to use pointers.
At this point it is important to be able to clearly distinguish between the two uses the square
brackets [ ] have for arrays.
• One is to set the size of arrays during declaration
• The other is to specify indices for a specific array element when accessing the elements
of the array
• We must take care of not confusing these two possible uses of brackets [ ] with arrays:
52
Eg:
int day[5]; // declaration of a new Array (begins with a type name)
day[2] = 75; // access to an element of the Array.
#include <iostream>
int n, result=0;
void main ( )
result += day[n];
getch();
}
Given the declaration above of a 100-element array the compiler reserves space for 100
consecutive floating point values and accesses these values using an index/subscript that takes
values from 0 to 99.
The first element in an array in C++ always has the index 0, and if the array has n elements the
last element will have the index n-1.
An array element is accessed by writing the identifier of the array followed by the subscript
in square brackets. Thus to set the 15th element of the array above to 1.5 the following
assignment is used:
annual_temp[14] = 1.5;
Note that since the first element is at index 0, then the ith element is at index i-1. Hence in
the above the 15th element has index 14.
53
An array element can be used anywhere an identifier may be used. Here are some examples
assuming the following declarations:
const int NE = 100, N = 50;
int i, j, count[N];
float annual_temp[NE];
float sum, av1, av2;
A value can be read into an array element directly, using cin.
cin>> count[i];
The element can be increased by 5,
count[i] = count[i] + 5;
or, using the shorthand form of the assignment
count[i] += 5;
Array elements can form part of the condition for an if statement, or indeed, for any other
logical expression:
if (annual_temp[j] < 10.0)
cout<< "It was cold this year "<<endl;
for statements are the usual means of accessing every element in an array. Here, the first NE
elements of the array annual_temp are given values from the input stream cin.
for (i = 0; i< NE; i++)
cin>>annual_temp[i];
The following code finds the average temperature recorded in the first ten elements of the array.
sum = 0.0;
for (i = 0; i <10; i++)
sum += annual_temp[i];
av1 = sum / 10;
Notice that it is good practice to use named constants, rather than literal numbers such as 10.
If the program is changed to take the average of the first 20 entries, then it is easy to forget to
change 10 to 20. If a const is used consistently, then changing its value will be all that is
necessary.
For example, the following example finds the average of the last k entries in the array. k could
either be a variable, or a declared constant. Observe that a change in the value of k will still
calculate the correct average (provided k<=NE).
sum = 0.0;
for (i = NE - k; i < NE; i++)
sum += annual_temp[i];
av2 = sum / k;
54
Important - C++ does not check that the subscript that is used to reference an array element
actually lies in the subscript range of the array. Thus C++ will allow the assignment of a value
to annual_temp[200], however the effect of this assignment is unpredictable. For example it
could lead to the program attempting to assign a value to a memory element that is outside the
program's allocated memory space.
This would lead to the program being terminated by the operating system. Alternatively it
might actually access a memory location that is within the allocated memory space of the
program and assign a value to that location, changing the value of the variable in your program
which is actually associated with that memory location, or overwriting the machine code of
your program.
Similarly reading a value from annual_temp[200] might access a value that has not been set
by the program or might be the value of another variable. It is the programmer's responsibility
to ensure that if an array is declared with n elements then no attempt is made to reference any
element with a subscript outside the range 0 to n-1. Using an index, or subscript, that is out of
range is called Subscript Overflow. Subscript overflow is one of the commonest causes of
erroneous results and can frequently cause very strange and hard to spot errors in programs.
55
Figure 2- 2:Array initialization to 0
But additionally, when we declare an Array, we have the possibility to assign initial values to each one
of its elements using curly brackets { } . For example: int day [5] = { 16, 2, 77, 40, 12071 };
The above declaration would have created an array like the following one:
56
The requirement specification for a program is:
A set of positive data values (200) are available. It is required to find the average value of these
values and to count the number of values that are more than 10% above the average value.
Since the data values are all positive a negative value can be used as a sentinel to signal the end
of data entry. Obviously this is a problem in which an array must be used since the values must
first be entered to find the average and then each value must be compared with this average.
Hence the use of an array to store the entered values for later re-use.
An initial algorithmic description is:
initialize.
enter elements into array and sum elements.
evaluate average.
scan array and count number greater than 10% above average.
output results.
57
const int NE = 200; // maximum no of elements in array
float sum = 0.0; // accumulates sum
int count = 0; // number of elements entered
int nogt10 = 0; // counts no greater than 10%
// above average
float x; // holds each no as input
float indata[NE]; // array to hold input
float average; // average value of input values
int i; // control variable
// Data entry, accumulate sum and count
// number of +ve numbers entered
cout<< "Enter numbers, -ve no to terminate: " <<endl;
cin>> x;
while (x >= 0.0)
{
sum = sum + x;
indata[count] = x;
count = count + 1;
cin>> x;
}
// calculate average
average = sum/count;
// Now compare input elements with average
for (i = 0; i< count; i++)
{
if (indata[i] > 1.1 * average)
nogt10++;
}
// Output results
cout<< "Number of values input is " << n;
cout<<endl<< "Number more than 10% above average is "<< nogt10<<endl;
}
Since it was assumed in the specification that there would be less than 200 values the array size
is set at 200. In running the program less than 200 elements may be entered, if n elements
where n < 200 elements are entered then they will occupy the first n places in the array indata.
It is common to set an array size to a value that is the maximum we think will occur in practice,
though often not all this space will be used.
Example Program: Test of Random Numbers
The following program simulates the throwing of a dice by using a random number generator
to generate integers in the range 0 to 5. The user is asked to enter the number of trials and the
program outputs how many times each possible number occurred.
58
An array has been used to hold the six counts. This allows the program to increment the correct
count using one statement inside the loop rather than using a switch statement with six cases
to choose between variables if separate variables had been used for each count. Also it is easy
to change the number of sides on the dice by changing a constant. Because C++ arrays start at
subscript 0 the count for an i occurring on a throw is held in the i-1th element of this count
array. By changing the value of the constant die_sides the program could be used to simulate a
die_sides-sided die without any further change.
#include <iostream.h>
#include <stdlib.h> // time.h and stdlib.h required for
#include <time.h> // random number generation
void main()
{
const int die_sides = 6; // maxr-sided die
int count[die_sides]; // holds count of each
// possible value
int no_trials, // number of trials
int roll, // random integer
int i; // control variable
float sample; // random fraction 0 .. 1
59
60
Week 3:- Practice:
Declaration of One dimensional Array
To declare an array in C++, the programmer specifies the type of the elements and the number
of elements required by an array as follows:
This is called a single-dimension array. The arraySize must be an integer constant greater than
zero and type can be any valid C++ data type. For example, to declare a 10-element array called
balance of type double, use this statement:
double balance[10];
Initializing Arrays You can initialize C++ array elements either one by one or using a single
statement as follows:
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ]. Following is an example to assign a single
element of the array: If you omit the size of the array, an array just big enough to hold the
initialization is created. Therefore, if you write:
You will create exactly the same array as you did in the previous example.
balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th
index will be 5th, i.e., last element because all arrays have 0 as the index of their first element
which is also called base index. Following is the pictorial representation of the same array we
discussed above:
61
An element is accessed by indexing the array name. This is done by placing the index of the
element within square brackets after the name of the array. For example:
The above statement will take 10th element from the array and assign the value to salary
variable. Following is an example, which will use all the above mentioned three concepts with
declaration, assignment.
Example:
C++ Program to store 5 numbers entered by user in an array and display first and last number
only.
62
#include <iostream>
int main( )
int n[5];
cin>>n[i];
return 0;
Copying Arrays
The assignment operator cannot be applied to array variables:
63
int y [SIZE] ;
x = y ; // Error - Illegal
Only individual elements can be assigned using the index operator, e.g., x[1] = y[2];.
To make all elements in 'x' the same as those in 'y' (equivalent to assignment), a loop has to
be used.
This code will copy the elements of array y into x, overwriting the original contents of x. A
loop like this has to be written whenever an array assignment is needed.
Notice the use of a constant to store the array size. This avoids the literal constant '10' appearing
number times in the code. If the code needs to be edited to use different size arrays, only the
constant needs to be changed. If the constant is not used, all the '10's would have to be changed
individually - it is easy to miss one out.
64
Assessments
I. Practical
1. Write an array declaration for the following
A) A list of 100 floating-point voltage
B) A list of 100 integer years
C) A list of 30 characters
D) A list of 32 floating-point velocities
2. Write a program to input the following values into an array named volts: 10.95, 16.32, 8.22,
15.98, 26.22, 13.54, 8.22, 6.45, and 173.86. After the data has been entered, have your
program output the values.
3. Write a program to input eight integer numbers into an array named temp. As each number
is input, add the number into a total. After all numbers are input, display the number and
their average.
4. Create an array that can hold ten integers, and get input from user. Display those values on
the screen, and then prompt the user for an integer. Search through the array, and count the
number of times the item is found.
5. Write a program that asks for an index and a number. Then it includes the number at the
indicated index of the array ={1,2,3,4,5,6} and moves a position forward (from u to u+1)
each element after the selected index.
6. Write a program that reverses a string and prints it on the screen.
65
Week 4:
2.4 Multidimensional arrays
Multidimensional arrays can be described as arrays of arrays. For example, a bi-dimensional
array can be imagined as a bi-dimensional table of a uniform concrete data type.
Matrix represents a bi-dimensional array of 3 per 5 values of type int . The way to
declare this array would be:
int matrix[3][5];
For example, the way to reference the second element vertically and fourth horizontally in an
expression would be:
matrix[1][3]
66
Multidimensional arrays are nothing else than an abstraction, since we can simply obtain the
same results with a simple array by putting a factor between its indices:
int matrix [3][5]; is equivalent to
int matrix [15]; (3 * 5 = 15)
With the only difference that the compiler remembers for us the depth of each imaginary
dimension. Serve as example these two pieces of code, with exactly the same result, one using
bi-dimensional arrays and the other using only simple arrays:
// multidimensional array
#include <iostream>
#define WIDTH 5
#define HEIGHT 3
int matrix [HEIGHT][WIDTH];
int n,m;
int main ( )
{
for (n=0;n<HEIGHT;n++)
for (m=0;m<WIDTH;m++)
{
matrix [n][m]=(n+1)*(m+1);
}
return 0;
}
None of the programs above produce any output on the screen, but both assign values to the
memory block called matrix in the following way:
Figure 2- 6:matrix
67
C++ allows multidimensional arrays. Here is the general form of a multidimensional array
declaration:
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional 5 . 10 . 4 integer array:
int threedim[5][10][4];
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional array. A two-
dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional
integer array of size x,y, you would write something as follows:
type arrayName [ x ][ y ];
An array may have more than one dimension. Each dimension is represented as a subscript in
the array. Therefore a two dimensional array has two subscripts, a three dimensional array has
three subscripts, and so on.
Arrays can have any number of dimensions, although most of the arrays that you create will
likely be of one or two dimensions.
A chess board is a good example of a two-dimensional array. One dimension represents the
eight rows, the other dimension represents the eight columns.
Such a representation does not correspond as closely to the real-world object as the two
dimensional array, however.
Suppose that when the game begins. The king id located in the fourth position in the first row.
Counting from zero that position corresponds to board[0][3] in the two dimensional array,
assuming that the first subscript corresponds to the row, and the second to the column.
The compiler ignores the inner braces, which clarify how the numbers are distributed.
Each value should be separated by comma, regardless of whither inner braces are include. The
entire initialization must set must appear within braces, and it must end with a semicolon.
68
2.4.2 Omitting the Array Size
If a one-dimensional array is initialized, the size can be omitted as it can be found from the
number of initializing elements:
int x[] = { 1, 2, 3, 4} ;
This initialization creates an array of four elements.
Note however:
int x[][] = { {1,2}, {3,4} } ; // error size cannot be omitted
int x[2][2] = { {1,2}, {3,4} } ;
69
Week 4 Practice:
Multi-dimensional array
To declare a multi-dimensional array, define the variable type, specify the name of the array
followed by square brackets which specify how many elements the main array has, followed
by another set of square brackets which indicates how many elements the sub-arrays have:
string letters[2][4];
As with ordinary arrays, you can insert values with an array literal - a comma-separated list
inside curly braces. In a multi-dimensional array, each element in an array literal is another
array literal.
string letters[2][4]={
};
Each set of square brackets in an array declaration adds another dimension to an array. An
array like the one above is said to have two dimensions.
Arrays can have any number of dimensions. The more dimensions an array has, the more
complex the code becomes. The following array has three dimensions:
70
Eg:
int a[5][4]; // row then column twenty elements, numbered from [0][0] to [4][3]
To change the value of an element, refer to the index number of the element in each of the
dimensions:
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
letters[0][0] = "Z";
cout << letters[0][0]; // Now outputs "Z" instead of "A"
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
string letters[2][2][2] = {
{
71
{ "A", "B" },
{ "C", "D" }
},
{
{ "E", "F" },
{ "G", "H" }
}
};
Multi-dimensional arrays are great at representing grids. This example shows a practical use
for them. In the following example we use a multi-dimensional array to represent a small
game of Battleship:
// Keep track of how many hits the player has and how many turns they have played
in these variables
int hits = 0;
int numberOfTurns = 0;
// Allow the player to keep going until they have hit all four ships
while (hits < 4) {
int row, column;
cout << "Selecting coordinates\n";
72
// Ask the player for a column
cout << "Choose a column number between 0 and 3: ";
cin >> column;
Application of Array
An array is a linear data structure which is used to store collection of similar type of data each
of which can be accessed by using an index. The memory location of every element can be
calculated by using a simple mathematical formula. The main advantage of an array over any
other data structure is any location can be accessed in constant time in an array which makes it
so useful in the design of other data structures. So an array is used as a basic building block in
many data structures.
• Arrays are used to implement vectors and lists which are an important part of C++ STL.
• Arrays are also used to implement stack and queues.
• Trees also use array implementation whenever possible as arrays are easy to handle
compared to pointers. Tress, in turn, are used to implement various other types of data
structures.
• Matrices which are an important part of the mathematical library in any programming
languages is implemented using arrays.
• Adjacency list implementation of graph uses vectors which are again implemented using
arrays.
• Data structures like a heap, map, and set use binary search tree and balanced binary trees
which uses can be implemented using arrays.
73
• Arrays are used to maintain multiple variables with the same name.
• CPU scheduling algorithms use implemented using arrays.
• All sorting algorithms use arrays at its core.
• Used in mathematical problems like matrices etc.
• They are used in the implementation of other data structures like linked lists etc.
• Database records are usually implemented as arrays.
• Used in lookup tables by computer.
• It effectively executes memory addressing logic wherein indices act as addresses to the
one-dimensional array of memory.
Here are some examples which shows the application of arrays using C++ programming
language.
#include<iostream>
int main()
int i, j, r, c;
cin>>r;
cin>>c;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<"\nmatrix1["<<i<<"]["<<j<<"]= ";
cin>>m1[i][j];
74
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<"\nmatrix2["<<i<<"]["<<j<<"]= ";
cin>>m2[i][j];
cout<<"\nAdding Matrices...\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
m3[i][j]=m1[i][j]+m2[i][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cout<<"\t"<<m3[i][j];
cout<<endl;
The ouput for the above example looks like the following.
75
• We take two matrices m1 and m2 with a maximum of 5 rows and 5 columns. And another
matrix m3 in which we are going to store the result,
• As user inputs, we took the number of rows and columns for both the matrices. Since we are
performing matrix addition, the number of rows and columns should be the same for both the
matrices,
• After that, we take both the matrices as user inputs, again using nested for loops,
• At this point, we have both the matrices m1 and m2,
• then we traverse through the m3 matrix, using two for loops and update the respective
elements m3[ i ][ j ] by the value of m1[i][j]+m2[i][j]. In this way, by the end of the outer for
loop, we get our desired matrix,
• At last, we print out the resultant matrix m3.
76
2. Pointer to a 2D Array in C++
#include<iostream>
int main( )
int s[5][2] = {
{1, 2},
{1, 2},
{1, 2},
{1, 2}
};
int (*p)[2] ;
int i, j;
p=&s[i];
cout<<"Row"<<i<<":";
cout<<"\t"<<*(*p+j);
cout<<endl;
77
• In the above code, we try to print a 2D array using pointers,
• As we earlier did, at first we initialize the 2D array, s[5][2]. And also a pointer (*p)[2],
where p is a pointer which stores the address of an array with 2 elements,
• As we already said, we can break down a 2D array as an array of arrays. So in this case, s is
actually an array with 5 elements, which further are actually arrays with 2 elements for each
row.
• We use for loop to traverse over these 5 elements of the array, s. For each iteration, we
assign p with the address of s[i],
• Further, the inner for loop prints out the individual elements of the array s[i] using the pointer p.
Here, (*p + j) gives us the address of the individual element s[i][j], so using *(*p+j) we can
access the corresponding value.
#include<iostream>
int i, j ;
for(i=0;i<row;i++)
78
for(j=0;j<col;j++)
cout<<"\t"<<*(*(q + i)+j);
cout<<"\n";
cout<<"\n";
int i, j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cout<<"\t"<<q[i][j];
cout<<"\n";
cout<<"\n";
int main()
int a[3][4] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21} ;
return 0;
79
4. C++ Program to Sort the given matrix
Given n x n matrix. The problem is to sort the given matrix in strict order. Here strict order
means that matrix is sorted in a way such that all elements in a row are sorted in increasing
order and for row ‘i’, where 1 <= i <= n-1, first element of row 'i' is greater than or equal to
the last element of row 'i-1'.
#define SIZE 10
80
// copy the elements of matrix one by one
// into temp[]
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
temp[k++] = mat[i][j];
// sort temp[]
sort(temp, temp + k);
Output:
Original Matrix:
547
138
296
Matrix After Sorting:
123
456
789
81
82
Assessments
I. Practical
1. Determine the output produced by the following program
#include <iostream>
int main()
{
int i, j,val[3][4]={8,16,9,52,3,15,27,6,14,25,2,10};
for (i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<” “<<val[i][j];
}
}
return 0;
}
2. Write a C++ program to select the values in a four by five array of integers in increasing
order and store the selected values in a single dimensional array named sort. Use the
following values to initialize the array 16, 22, 99, 4, 18, -258, 4, 101, 5, 98, 105, 6, 15, 2,
45, 33, 88, 72, 16.
3. Write a program which reads two matrix and multiply them if possible.
4. Write a program which reads a 3 x 2 matrix and then calculates the sum of each row and
store that in a one dimension array.
5. what is the value of sum after this code executes?
int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0; int col = matrix[0].length - 2;
for (int row = 0; row < 4; row++)
{
sum = sum + matrix[row][col];
}
83
Week 5:
This maximum size of 20 characters is not required to be always fully used. For example, name
could store at some moment in a program either the string of characters "Hello" or the string
"studying C++”. Therefore, since the array of characters can store shorter strings than its total
length, there has been reached a convention to end the valid content of a string with a null
character, whose constant can be written as '\0’.
We could represent name (an array of 20 elements of type char) storing the strings of
characters "Hello" and "Studying C++" in the following way:
The C++ Standard Library implements a powerful string class, which is very useful to handle
and manipulate strings of characters. However, because strings are in fact sequences of
characters, we can represent them also as plain arrays of char elements.
For example, the following array:
char jenny [20];
is an array that can store up to 20 elements of type char. It can be represented as:
jenny
84
a program either the sequence "Hello" or the sequence "Merry Christmas", since both are
shorter than 20 characters.
Therefore, since the array of characters can store shorter sequences than its total length, a
special character is used to signal the end of the valid sequence: the null character, whose
literal constant can be written as '\0' (backslash, zero).
The array of 20 elements of type char, called jenny, can be represented storing the character’s
sequences "Hello" and "Merry Christmas" as:
Notice how after the valid content a null character ('\0') has been included in order to indicate
the end of the sequence. The panels in grey colour represent char elements with undetermined
values.
2.5.1 Initialization of Strings
Because strings of characters are ordinary arrays they fulfil same rules as any array. For
example, if we want to initialize a string of characters with predetermined values we
can do it in a similar way to any other array:
char mystring[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
In this case we would have declared a string of characters (array) of 6 elements of type char
initialized with the characters that compose Hello plus a null character '\0' .
Nevertheless, string of characters have an additional way to initialize its values: using constant
strings.
In the expressions we have used in examples of previous chapters there have already appeared
several times constants that represented entire strings of characters. These are specified
enclosed between double quotes ( “ “ ), for example: "the result is: " is a constant string that
we have probably used in some occasion.
Unlike single quotes ( ' ) which allow to specify single character constants, double quotes ( " )
are constants that specify a succession of characters. These strings enclosed between double
quotes have always a null character ( '\0' ) automatically appended at the end.
Therefore we could initialize the string mystring with values by any of these two ways:
char mystring [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char mystring [] = "Hello";
In both cases the Array or string of characters mystring is declared with a size of 6 characters
(elements of type char ): the 5 characters that compose Hello plus a final null character ( '\0' )
which specifies the end of the string and that, in the second case, when using double quotes
( " ) it is automatically appended.
85
Before going further, you should note that the assignation of multiple constants like double-
quoted constants ( " ) to arrays are only valid when initializing the array, that is, at the moment
when declared.
The following expressions within a code are not valid for arrays
mystring="Hello";
mystring[] = "Hello";
At the moment of initializing an Array it is a special case, since it is not an assignation, although
the same equal sign ( =) is used. Anyway, have always present the rule previously underlined.
2.5.2 Assigning Values to Strings
Just like any other variables, array of character can store values using assignment operators.
But the following is not allowed.
mystring=”Hello”;
This is allowed only during initialization. Therefore, since the lvalue of an assignation can only
be an element of an array and not the entire array, what would be valid is to assign a string of
characters to an array of char using a method like this:
mystring[0] = 'H';
mystring[1] = 'e';
mystring[2] = 'l';
mystring[3] = 'l';
mystring[4] = 'o';
mystring[5] = '\0';
But as you may think, this does not seem to be a very practical method. Generally for assigning
values to an array, and more specifically to a string of characters, a series of functions like
strcpy are used. strcpy ( string copy ) is defined in the ( string.h ) library and can be called
the following way:
strcpy ( string1 , string2 );
This does copy the content of string2 into string1 .string2 can be either an array, a pointer, or
a constant string , so the following line would be a valid way to assign the constant string
"Hello" to mystring :
86
strcpy (mystring, "Hello");
For example:
#include <iostream>
#include <string>
int main ()
{
char szMyName [20];
strcpy (szMyName,"Abebe");
cout << szMyName;
return 0;
}
In this case we would have declared an array of 6 elements of type char initialized with the
characters that form the word "Hello" plus a null character '\0' at the end.
But arrays of char elements have an additional method to initialize their values: using string
literals.
In the expressions we have used in some examples in previous chapters, constants that represent
entire strings of characters have already showed up several times. These are specified enclosing
the text to become a string literal between double quotes (").
Double quoted strings (") are literal constants whose type is in fact a null-terminated array of
characters. So string literals enclosed between double quotes always have a null character ('\0')
automatically appended at the end.
Therefore we can initialize the array of char elements called myword with a null-terminated
sequence of characters by either one of these two methods:
char myword [] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword [] = "Hello";
In both cases the array of characters myword is declared with a size of 6 elements of type char:
the 5 characters that compose the word "Hello" plus a final null character ('\0') which specifies
the end of the sequence and that, in the second case, when using double quotes (") it is appended
automatically.
Notice that we are talking about initializing an array of characters in the moment it is being
declared, and not about assigning values to them once they have already been declared. In fact
because this type of null terminated arrays of characters are regular arrays we have the same
87
restrictions that we have with any other array, so we are not able to copy blocks of data with
an assignment operation.
Assuming mystext is a char[] variable, expressions within a source code like:
mystext = "Hello";
mystext[] = "Hello";
would not be valid, like neither would be:
The reason for this may become more comprehensible once you know a bit more about pointers,
since then it will be clarified that an array is in fact a constant pointer pointing to a block of
memory.
String in C++ is nothing but a sequence of character in which the last character is the null
character ‘\0’. The null character indicates the end of the string. Any array of character can be
converted into string type in C++ by appending this special character at the end of the array
sequence.
In C++ strings of characters are held as an array of characters, one character held in each
array element. In addition a special null character, represented by `\0', is appended to the end
of the string to indicate the end of the string. Hence if a string has n characters then it requires
an n+1 element array (at least) to store it. Thus the character `a' is stored in a single byte,
whereas the single-character string "a" is stored in two consecutive bytes holding the character
`a' and the null character.
A string variable s1 could be declared as follows:
char s1[10];
The string variable s1 could hold strings of length up to nine characters since space is needed
for the final null character. Strings can be initialized at the time of declaration just as other
variables are initialized. For example:
char s1[] = "example";
char s2[20] = "another example";
would store the two strings as follows:
s1 |e|x|a|m|p|l|e|\0|
s2 |a|n|o|t|h|e|r| |e|x|a|m|p|l|e|\0|?|?|?|?|
In the first case the array would be allocated space for eight characters that is space for the
seven characters of the string and the null character. In the second case the string is set by the
declaration to be twenty characters long but only sixteen of these characters are set, i.e. the
fifteen characters of the string and the null character. Note that the length of a string does not
include the terminating null character.
Using null-terminated sequences of characters
Null-terminated sequences of characters are the natural way of treating strings in C++, so they
can be used as such in many procedures. In fact, regular string literals have this type (char[])
and can also be used in most cases.
88
For example, cin and cout support null-terminated sequences as valid containers for sequences
of characters, so they can be used directly to extract strings of characters from cin or to insert
them into cout.
For example:
null-terminated sequences of characters
#include <iostream>
using namespace std;
int main ( )
{
char question[ ] = "Please, enter your first name: ";
char greeting[ ] = "Hello, ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname <<"!";
return 0;
}
string mystring;
char myntcs[ ]="some text";
mystring = myntcs;
string s1;
cout<< "The string s1 is " << s1 <<endl;
would print
The string s1 is example
The setw(width) I/O manipulator can be used before outputting a string, the string will then be
output right-justified in the field width. If the field width is less than the length of the string
89
then the field width will be expanded to fit the string exactly. If the string is to be left-justified
in the field then the setiosflags manipulator with the argument ios::left can be used.
|f|i|r|s|t|\0|e|\0|
Note that the last two elements are a relic of the initialization at declaration time. If the string
that is entered is longer than the space available for it in the character array then C++ will just
write over whatever space comes next in memory. This can cause some very strange errors
when some of your other variables reside in that space!
To read a string with several words in it using cin we have to call cin once for each word. For
example to read in a name in the form of a Christian name followed by a surname we might
use code as follows:
char christian[12], surname[12];
cout<< "Enter name ";
cin>>christian;
cin>> surname;
cout<< "The name entered was "<<christian<< " "<< surname;
The name would just be typed by the user as, for example, Abebe and the output would then
be
The name entered was Abebe
Where did the rest of the phrase go?
It turns the insertion operator >> consider a space to be a terminating character. Thus it will
read strings consisting of a single word, but anything typed after a space is thrown away.
To read text containing blanks we use another function, cin::get().
#include<iostream>
void main()
{
const int max=80;
char str[max];
cout<<"\n Enter a string;";
90
cin.get(str,max); // max avoid buffer overflow
cout<<"\n You entered : "<<str;
}
91
{
const int max=80;
char str1[]='' Oh, Captain, my Captain!" our fearful trip is done";
char str2[max];
for(int i=0; i<strlen(str1);i++)
str2[i]=str1[1];
str2[i]='\0';
cout<<endl;
cout<<str2;
}
92
Week 5: Practice
The string concatenation can have two forms, where the first one is to append the whole
content of the source to the destination the other will append only part of the source to the
destination.
• Appending the whole content of the source
strcat (char* dest , const char* src );
• Appending part of the source
strncat (char* dest , const char* src, int size ); Where size is the number
characters to be appended
c) String Copy:
Overwrites the content of the dest string by the src string. Returns dest.
The string copy can have two forms, where the first one is to copying the whole content of the
source to the destination and the other will copy only part of the source to the destination.
• Copy the whole content of the source
strcpy (char* dest , const char* src );
• Appending part of the source
strncpy (char* dest , const char* src, int size ); Where size is the number
characters to be copied
d) String Compare:
Compares the two string string1 and string2.
The string compare can have two forms, where the first one is to compare the whole content of
the two strings and the other will compare only part of the two strings.
• Copy the whole content of the source
strcmp (const char* string1 , const char* string2 );
• Appending part of the source
strncmp (const char* string1 , const char* string2, int size ); Where size is the
number characters to be compaired
Both string compare functions returns three different values:
• Returns 0 is the strings are equal
• Returns negative value if the first is less than the second string
• Returns positive value if the first is greater than the second string
93
2. Copying string the easy way
Of course you don't need to use for loop to copy a string. As you might have guesses, a library
function will do it for you. You can copy strings using strcpy or strncpy function. We assign
strings by using the string copy function strcpy. The prototype for this function is instring.h.
strcpy(destination, source);
strcpy copies characters from the location specified by source to the location specified by
destination. It stops copying characters after it copies the terminating null character.
• The return value is the value of the destination parameter.
You must make sure that the destination string is large enough to hold all of the characters in
the source string (including the terminating null character).
Example:
#include <iostream.h>
#include <string.h>
void main()
{
char me[20] = "David";
cout<< me <<endl;
strcpy(me, "YouAreNotMe");
cout<< me <<endl ;
return 0;
}
There is also another function strncpy, is like strcpy, except that it copies only a specified
number of characters.
strncpy(destination, source, int n);
#include <string>
void main()
char one[10];
94
one[9] = '\0';
strcpy(one, str2);
3. Concatenating strings
In C++ the + operator cannot normally be used to concatenate string, as it can in some
languages such as BASIC; that is you can't say
Str3 = str1 + str2;
• The first character of the source string is copied to the location of the terminating null
character of the destination string.
• The destination string must have enough space to hold both strings and a terminating null
character.
Example:
#include <iostream>
#include <string>
void main()
char str1[30];
strcpy(str1, "abc");
strcat(str1, "def");
95
strcat(str1, str2);
str1[4] = '\0';
The function strncat is like strcat except that it copies only a specified number of characters.
strncat(destination, source, int n);
#include <string>
void main() {
char str1[30];
strcpy(str1, "abc");
str1[5] = '\0';
strcat(str1, str2);
str1[4] = '\0';
4. Comparing strings
Strings can be compared using strcmp or strncmp functions. The function strcmp compares
two strings.
strcmp(str1, str2);
96
strcmp returns: <0 if str1 is less than str2
Example:
#include <iostream>
#include <string>
void main()
The function strncmp is like strcmp except that it compares only a specified number of
characters.
strncmp(str1, str2, int n);
strncmp does not compare characters after a terminating null character has been found in one
of the strings.
Example:
#include <iostream.h>
#include <string.h>
void main()
97
cout<<strncmp("abc", "abcdef", 20) <<endl;
98
Assessment
I. Practical
1. Define strlen function (i.e. write the function body of strlen)?
2. Define the strcmp function and the strncmp function ?
3. Define strcpy function and the strncpy function ?
4. Define strcat function and the strncat function ?
5. Write a program to store the ages of six of your friends in a single array. Store each of the six
ages using the assignment operator. print the ages on the screen ?
6. Write a C++ program that accepts 10 integers from the user and finally displays the smallest
value and the largest value.
7. Write a program that accepts ten different integers from the user and display these numbers
after sorting them in increasing order.
8. Write a program to store six of your friend’s ages in a single array. Assign the ages in a random
order. print the ages, from low to high, on-screen
9. Modify the program on Q8 to print the ages in descending order.
10. Write a C++ program that calculates the letter grades of 20 students. The program should
accept the mid result and the final result from the students. Use the appropriate validity control
mechanism to prevent wrong inputs.
11. Write a C++ program that has two functions toBinary and toDecimal. The program should
display a menu prompting the user to enter his choice. If the user selects toBinary, then the
function should accept a number in base ten and displays the equivalent binary representation.
The reverse should be done if the user selects toDecimal.
12. Develop a C++ program that accepts a word from the user and then checks whether the word
is palindrome or not. (NB a word is palindrome if it is readable from left to right as well as
right to left).
13. Write a C++ program that accepts a word from the user and then displays the word after
reversing it.
14. Develop a C++ program that accepts the name of a person and then counts how many vowels
the person’s name have.
99
15. Modify the question in Q14 in such a way that it should replace vowel characters with * in the
person name.
16. Write a program in C++ which read a three digit number and generate all the possible
permutation of numbers using the above digits. For example n = 123 then the permutations are
– 123, 213, 312, 132, 231, 321
17. Write a program which read a set of lines until you enter #.
18. Write a program which read two matrixes and then print a matrix which is addition of these
two matrixes.
19. Write a program which reads two matrices and multiply them if possible
20. Write a program which reads a 3 x 2 matrix and then calculates the sum of each row and store
that in a one dimension array.
100
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill, 2003
[3]. P B Mahapatra, Programming in C++, S Chand, 2008
[4]. Ashok Kamthane, Programming in C++, Pearson, 2013
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com
101
Chapter 3: Pointers
Lesson Plan
1. Learning objectives
• Be able to define pointer.
• Be able to define pointer variables.
• Be able to differentiate single and multi-dimensional array.
• Be able to apply the array variables in C++ programming.
2. Motivation
• Outputs through Question and Answer; repetitive discussion and using array variables;
• Solving a problem using pointers in C++.
3. Expectations or Outcomes
Define the terminologies, differentiate pointer with references, explain pointer
variables; the assessment standard attached at the end of the job task sheet.
Can write C++ instructions to solve any problem using the concept of arrays in
programming.
4. Equipment
• Desktop or Laptop computer.
• DEV C++ software
5. Practice contents/Activities/Safety
Active practice; on declaring pointer variables, dynamic memory allocation of variab
les, using pointers with function.
Active practice on single and multi-dimensional array.
6. Assessments
• How to declare pointer variables?
• How to apply pointer with functions?
7. Clean-up
After finishing practice, computers should be properly shut down.
8. Independent practice/Follow-up activities
• Learning through assignment (practical Exercises)
9. Review/Reflection
Review the outcome of the practice, improvement measure and previous reflected opinions.
103
Week 6:
ptr1 num
104
converts ptr1 to char pointer before assigning it to ptr2.
Regardless of its type, a pointer may be assigned the value 0 (called the null pointer). The null
pointer is used for initializing pointers, and for marking the end of pointer-based data structures
(e.g., linked lists).
Dynamic Memory
In addition to the program stack (which is used for storing global variables and stack frames
for function calls), another memory area, called the heap, is provided. The heap is used for
dynamically allocating memory blocks during program execution. As a result, it is also called
dynamic memory. Similarly, the program stack is also called static memory.
Two operators are used for allocating and de-allocating memory blocks on the heap. The new
operator takes a type as argument and allocated a memory block for an object of that type. It
returns a pointer to the allocated block. For example,
int *ptr = new int;
char *str = new char[10];
allocate, respectively, a block for storing a single integer and a block large enough for storing
an array of 10 characters.
Memory allocated from the heap does not obey the same scope rules as normal variables. For
example, in
void Foo (void)
{
char *str = new char[10];
//...
}
When Foo returns, the local variable str is destroyed, but the memory block pointed to by str is
not. The latter remains allocated until explicitly released by the programmer.
The delete operator is used for releasing memory blocks allocated by new. It takes a pointer as
argument and releases the memory block to which it points. For example:
Note that when the block to be deleted is an array, an additional [] should be included to indicate
this. Should delete be applied to a pointer which points to anything but a dynamically-allocated
object (e.g., a variable on the stack), a serious runtime error may occur. It is harmless to apply
delete to the 0 pointer. Dynamic objects are useful for creating data which last beyond the
function call which creates them.
105
Because of the limited memory resources, there is always the possibility that dynamic memory
may be exhausted during program execution, especially when many large blocks are allocated
and none released. Should new be unable to allocate a block of the requested size, it will return
0 instead. It is the responsibility of the programmer to deal with such possibilities.
Until now, in our programs, we have only had as much memory as we have requested in
declarations of variables, arrays and other objects that we included, having the size of all of
them to be fixed before the execution of the program. But, What if we need a variable amount
of memory that can only be determined during the program execution (runtime)?
For example, in case that we need a user input to determine the necessary amount of space.
The answer is dynamic memory, for which C++ integrates the operators new and delete.
Pointers are useful for creating dynamic objects during program execution. Unlike normal
(global and local) objects which are allocated storage on the runtime stack, a dynamic object
is allocated memory from a different storage area called the heap. Dynamic objects do not
obey the normal scope rules. Their scope is explicitly controlled by the programmer.
You could ask what is the difference between declaring a normal array and assigning memory
to a pointer as we have just done. The most important one is that the size of an array must be a
constant value, which limits its size to what we decide at the moment of designing the program
106
before its execution, whereas the dynamic memory allocation allows assigning memory during
the execution of the program using any variable, constant or combination of both as size.
• The dynamic memory is generally managed by the operating system, and in the multi-
task interfaces can be shared between several applications, so there is a possibility that
the memory exhausts. If this happens and the operating system cannot assign the
memory that we request with the operator new , a null pointer will be returned. For that
reason it is recommendable to always verify if after a call to instruction new the
returned pointer is null:
int * ptr3; ptr3 = new int [5];
if (ptr3 == NULL) { // error assigning memory. Take measures.
};
• if ptr3 is NULL, it means that there is no enough memory location in the heap to be given for
ptr3.
b) Operator delete
• Since the necessity of dynamic memory is usually limited to concrete moments within a
program, once this one is no longer needed it shall be freed so that it become available for
future requests of dynamic memory. For this exists the operator delete , whose form is:
delete pointer ;
or
delete [] pointer ;
• The first expression should be used to delete memory allocated for a single element, and the
second one for memory allocated for multiple elements (arrays).
• In most compilers both expressions are equivalent and can be used without distinction,
although indeed they are two different operators and so must be considered for operator
overloading.
• In the following simple example, a program that memorizes numbers, does not have a limited
amount of numbers that can be introduced, thanks to the concept and power of pointer that we
request to the
A pointer is a variable which stores the address of another variable. The only difference
between pointer variable and regular variable is the data they hold.
There are two pointer operators in C++: & the address of operator
107
A pointer is simply the address of an object in memory. Generally, objects can be accessed in
two ways: directly by their symbolic name, or indirectly through a pointer. The act of getting
to an object via a pointer to it, is called dereferencing the pointer. Pointer variables are defined
to point to objects of a specific type so that when the pointer is dereference, a typed object is
obtained.
At the moment in which we declare a variable this one must be stored in a concrete location in
this succession of cells (the memory). We generally do not decide where the variable is to be
placed - fortunately that is something automatically done by the compiler and the operating
system on runtime, but once the operating system has assigned an address there are some cases
in which we may be interested in knowing where the variable is stored.
This can be done by preceding the variable identifier by an ampersand sign (&), which literally
means, "address of”. For example:
ptr= &var;
This would assign to variable ptr the address of variable var , since when preceding the name
of the variable var with the ampersand ( & ) character we are no longer talking about the
content of the variable, but about its address in memory.
We are going to suppose that var has been placed in the memory address 1776 and that we
write the following:
We have assigned to x the content of variable var as we have done in many other occasions in
previous sections, but to ptr we have assigned the address in memory where the operating
system stores the value of var , that we have imagined that it was 1776 (it can be any address).
The reason is that in the allocation of ptr we have preceded var with an ampersand ( & )
character.
108
The variable that stores the address of another variable (like ptr in the previous example) is
what we call a pointer.
The dereference operator produces a value that tells the pointer where to point. Without the *,
(i.e cout<<p_age), a cout statement would print an address (the address of age). With the *, the
cout prints the value at that address.
You can assign a different value to age with the following statement:
age = 13; //assigns a new value to variable age
*p_age = 13 //assigns 13 as a value to the memory p_age points at.
N.B: the * appears before a pointer variable in only two places: when you declare a pointer
variable and when you dereference a pointer variable (to find the data it points to).
The following program is one you should study closely. It shows more about pointers and the
pointer operators, & and *, than several pages of text could do.
#...
#...
109
void main()
{
int num = 123; // a regular integer variable
int *p_num; //declares an integer pointer
cout<< “num is ”<<num<<endl;
cout<< “the address of num is ”<<&num<<endl;
p_num = #// puts address of num in p_num;
cout<< “*p_num is ”<<*p_num<<endl; //prints value of num
cout<< “p_num is ”<<p_num<<endl; //prints adress of P_num
getch();
}
110
Week 6:- Practice
var = 5;
pointVar = &var;
Here, 5 is assigned to the variable var. And, the address of var is assigned to the pointVar
pointer with the code pointVar = &var.
var = 5;
pointVar = &var;
In the above code, the address of var is assigned to pointVar. We have used the *pointVar to
get the value stored in that address.
When * is used with pointers, it's called the dereference operator. It operates on a pointer and
gives the value pointed by the address stored in the pointer. That is, *pointVar = var.
#include <iostream>
int main() {
int var = 5;
111
pointVar = &var; // store address of var
cout << "var = " << var << endl; // print value of var
cout << "Address of var (&var) = " << &var << endl// print address of var << endl;
cout << "pointVar = " << pointVar << endl; // print pointer pointVar
cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar <<
endl;
return 0;
Output
var = 5
pointVar = 0x61ff08
Exercise 1
Write a program that asks the user to enter integers as inputs to be stored in the variables 'a'
and 'b' respectively. There are also two integer pointers named ptrA and ptrB. Assign the
values of 'a' and 'b' to ptrA and ptrB respectively, and display them.
Solution
// Exercises: Pointers
112
// Exercise 1
#include <iostream>
int main(){
int a; int b;
cin >> a;
cin >> b;
int *ptrA=&a;
int *ptrB=&b;
cout << "Value of ptrA is " << *ptrA << " sored in address "<< ptrA<<"\n";
cout << "Value of ptrB is " << *ptrB <<" sored in address "<< ptrB<<"\n";
return 0;
113
Assessment
I. Subjective Type Question
1. What is a pointer?
2. Explain why we use pointers in programming.
3. What is the difference between pointers and references?
4. Write the syntax of declaring a pointer variable with an example.
114
Week 7:
The expression *(ara+2) is not vague at all if you remember that an array name is just a pointer
that always points to the array’s first element. *(ara+2) takes the address stored in ara, adds 2
to the address, and dereferences that location.
Unlike arrays, you can change a pointer variable. By changing pointers, you can make them
point to different values in memory. Have a look at the following code:
#...
#...
void main()
{
clrscr();
float v1 = 679.54;
float v2 = 900.18;
float * p_v;
p_v = &v1;
cout<< “\nthe first value is ”<<*p_v;
p_v = &v2;
cout<< “\nthe second value is ”<<*p_v;
getch();
}
You can use pointer notation and reference pointers as arrays with array notation. Study the
following program carefully. It shows the inner workings of arrays and pointer notation.
void main()
{
clrscr();
int ctr;
int iara[5] = {10,20,30,40,50};
int *iptr;
iptr = iara; //makes iprt point to array’s first element. Or iprt = &iara[0]
cout<< “using array subscripts:\n”
cout<< “iara\tiptr\n”;
for(ctr=0;ctr<5;ctr++)
{
116
cout<<iara[ctr]<< “\t”<< iptr[ctr]<< “\n”;
}
cout<< “\nUsing pointer notation\n”;
for(ctr=0;ctr<5;ctr++)
{
cout<< *(iara+ctr) << “\t” << *(iptr+ctr)<< “\n”;
}
getch();
}
Suppose that you want to store a person’s name and print it. Rather than using arrays, you can
use a character pointer. The following program does just that.
void main()
{
clrscr();
char *c = “Meseret Belete”;
cout<< “your name is : ”<<c;
}
Suppose that you must change a string pointed to by a character pointer, if the person’s name
in the above code is changed to Meseter Alemu: look at the following code:
void main()
{
char *c = “Meseret Belete”;
cout<< “your name is : ”<<c;
c = “Meseret Alemu”;
cout<< “\nnew person name is : ”<<c;
getch();
}
If c were a character array, you could never assign it directly because an array name can’t be
changed.
Pointer Arithmetic
In C++ one can add an integer quantity to or subtract an integer quantity from a pointer. This
is frequently used by programmers and is called pointer arithmetic. Pointer arithmetic is not
the same as integer arithmetic, because the outcome depends on the size of the object pointed
to. For example, suppose that an int is represented by 4 bytes. Now, given
117
str++ advances str by one char (i.e., one byte) so that it points to the second character of "HELLO",
whereas ptr++ advances ptr by one int (i.e., four bytes) so that it points to the second element of
nums. The figure below illustrates this diagrammatically.
H E L L O \0 10 20 30 40
str ptr
str++ ptr++
It follows, therefore, that the elements of "HELLO" can be referred to as *str, *(str + 1), *(str + 2),
etc. Similarly, the elements of nums can be referred to as *ptr, *(ptr + 1), *(ptr + 2), and *(ptr + 3).
Another form of pointer arithmetic allowed in C++ involves subtracting two pointers of the
same type. For example:
Pointer arithmetic is very handy when processing the elements of an array. The following
shows as an example a string copying function similar to strcpy.
Example:
1 void CopyString (char *dest, char *src)
2 {
3 while (*dest++ = *src++)
4 ;
5 }
The condition of this loop assigns the contents of src to the contents of dest and then
increments both pointers. This condition becomes 0 when the final null
character of src is copied to dest.
In turns out that an array variable (such as nums) is itself the address of the first element of the
array it represents. Hence the elements of nums can also be referred to using pointer arithmetic
on nums, that is, nums[i] is equivalent to *(nums + i). The difference between nums and ptr is that
nums is a constant, so it cannot be made to point to anything else, whereas ptr is a variable and
can be made to point to any other integer.
Function Pointers
118
It is possible to take the address of a function and store it in a function pointer. The pointer can
then be used to indirectly call the function. For example,
119
You can also initialize a reference to a constant. In this case a copy of the constant is made
(after any necessary type conversion) and the reference is set to refer to the copy.
int &n = 1; // n refers to a copy of 1
The reason that n becomes a reference to a copy of 1 rather than 1 itself is safety. Consider
what could happen if this were not the case.
int &x = 1;
++x;
int y = x + 1;
The 1 in the first and the 1 in the third line are likely to be the same object (most compilers do
constant optimization and allocate both 1’s in the same memory location). So although we
expect y to be 3, it could turn out to be 4. However, by forcing x to be a copy of 1, the compiler
guarantees that the object denoted by x will be different from both 1’s.
Typedefs
Typedef is a syntactic facility for introducing symbolic names for data types. Just as a reference
defines an alias for an object, a typedef defines an alias for a type. Its main use is to simplify
otherwise complicated type declarations as an aid to improved readability. Here are a few
examples:
typedef char string;
Typedef char Name[12];
typedef unsigned int uint;
The effect of these definitions is that String becomes an alias for char*, Name becomes an alias
for an array of 12 chars, and uint becomes an alias for unsigned int. Therefore:
The complicated declaration of Compare in Listing 3.7 is a good candidate for typedef:
120
121
Week 7:- Practice
Listing 1:
1 #include <string.h>
5 strcpy(copy, str);
6 return copy;
7 }
Annotation (analysis)
1 This is the standard string header file which declares a variety of functions for
manipulating strings.
4 The strlen function (declared in string.h) counts the characters in its string
argument up to (but excluding) the final null character. Because the null
character is not included in the count, we add 1 to the total and allocate an
array of characters of that size.
5 The strcpy function (declared in string.h) copies its second argument to its
Listing 2 shows how the HighestTemp function (shown earlier in the earlier Listing 1) can be
improved using pointer arithmetic.
Listing 2:
122
1 int HighestTemp (const int *temp, const int rows, const int columns)
2 {
3 int highest = 0;
for (register i = 0; i < rows; ++i)
4 for (register j = 0; j < columns; ++j)
5 if (*(temp + i * columns + j) > highest)
6 highest = *(temp + i * columns + j);
7 return highest;
8 }
9
Annotation
1 Instead of passing an array to the function, we pass an int pointer and two
additional parameters which specify the dimensions of the array. In this way,
the function is not restricted to a specific array size.
6 The expression *(temp + i * columns + j) is equivalent to temp[i][j] in the previous
version of this function.
HighestTemp can be simplified even further by treating temp as a one-dimensional array of row
* column integers. This is shown in Listing 3
Listing 3
1 int HighestTemp (const int *temp, const int rows, const int columns)
2 {
3 int highest = 0;
As shown in Listing 4, by making the comparison function a parameter of the search function,
we can make the latter independent of the former.
Listing 4
123
1 int BinSearch (char *item, char *table[ ], int n,
2 int (*Compare)(const char*, const char*))
3 {
4 int bot = 0;
5 int top = n - 1;
6 int mid, cmp;
Annotation
1 Binary search is a well-known algorithm for searching through a sorted list of
items. The search list is denoted by table which is an array of strings of
dimension n. The search item is denoted by item.
2 Compare is the function pointer to be used for comparing item against the array
elements.
7 Each time round this loop, the search span is reduced by half. This is repeated
until the two ends of the search span (denoted by bot and top) collide, or until
a match is found.
9 The item is compared against the middle item of the array.
10 If item matches the middle item, the latter’s index is returned.
11 If item is less than the middle item, then the search is restricted to the lower
half of the array.
14 If item is greater than the middle item, then the search is restricted to the upper
half of the array.
16 Returns -1 to indicate that there was no matching item.
The following example shows how BinSearch may be called with strcmp passed as the
comparison function:
char *cities[] = {"Boston", "London", "Sydney", "Tokyo"};
cout<<BinSearch("Sydney", cities, 4, strcmp) << '\n';
This will output 2 as expected.
124
The most common use of references is for function parameters. Reference parameters
facilitates the pass-by-reference style of arguments, as opposed to the pass-by-value style
which we have used so far. To observe the differences, consider the three swap functions in
Listing 5.
Listing 5
1 void Swap1 (int x, int y) // pass-by-value (objects)
2 {
3 int temp = x;
4 x = y;
5 y = temp;
6 }
Annotation
1 Although Swap1 swaps x and y, this has no effect on the arguments passed to
the function, because Swap1 receives a copy of the arguments. What happens
to the copy does not affect the original.
7 Swap2 overcomes the problem of Swap1 by using pointer parameters instead.
By dereferencing the pointers, Swap2 gets to the original values and swaps
them.
13 Swap3 overcomes the problem of Swap1 by using reference parameters instead.
The parameters become aliases for the arguments passed to the function and
therefore swap them as intended.
Swap3 has the added advantage that its call syntax is the same as Swap1 and involves no
addressing or dereferencing. The following main function illustrates the differences:
int main (void)
{
int i = 10, j = 20;
Swap1(i, j); cout << i << ", " << j << '\n';
Swap2(&i, &j); cout<<i<< ", " << j << '\n';
125
Swap3(i, j); cout<<i<< ", " << j << '\n';
}
10, 20
20, 10
10, 20
126
Assessments
5. Write C++ program to copy one string to another string using pointer
7. Write C++ program to print the elements of the array in reverse order using a pointer
127
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill,
2003
[3]. P B Mahapatra, Programming in C++, S Chand, 2008
[4]. Ashok Kamthane, Programming in C++, Pearson, 2013
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com
128
Chapter 4: Function (Built in, User defined)
Lesson Plan
1. Learning objectives
• Be able to define modular programming.
• Be able to define and declare function.
• Be able to pass function arguments.
• Be able to know return values of a function
• Be able to apply inline and recursive functions.
2. Motivation
• Outputs through Question and Answer; repetitive discussion and using function in C++.
• Solving a problem using function in C++.
3. Expectations or Outcomes
Define the terminologies, differentiate different types of function, explain function
(declaration, definition and calling); the assessment standard attached at the end of the
job task sheet.
Can write C++ instructions to solve any problem using the concept of modular
programming.
4. Equipment
• Desktop or Laptop computer.
• DEV C++ software
5. Practice contents/Activities/Safety
Active practice; on declaring function, defining function, calling function.
Active practice on inline and recursive functions.
6. Assessments
• How to apply modular programming?
• How to create function using C++?
7. Clean-up
After finishing practice, computers should be properly shut down.
8. Independent practice/Follow-up activities
• Learning through assignment (practical Exercises)
9. Review/Reflection
• Review the outcome of the practice, improvement measure and previous reflected
opinions.
129
Week 9:
Modular programming is breaking down the design of a program into individual components
(modules) that can be programmed and tested independently. It is a requirement for effective
development and maintenance of large programs and projects. With modular programming,
procedures of a common functionality are grouped together into separate modules. A program
therefore no longer consists of only one single part. It is now divided into several smaller parts
which interact and which form the whole program.
Definition of Functions
Modules in C++ are called functions. A function is a subprogram that can act on data and return
a value. Every C++ program has at least one function, main(). When your program starts,
main() is called automatically. main() might call other functions, some of which might call
still others. Each function has its own name, and when that name is encountered, the execution
of the program branches to the body of that function. When the function returns, execution
resumes on the next line of the calling function. When a program calls a function, execution
switches to the function and then resumes at the line after the function call. Well-designed
functions perform a specific and easily understood task. Complicated tasks should be broken
down into multiple functions, and then each can be called in turn. Functions come in two
varieties: user-defined and built-in. Built-in functions are part of your compiler package--they
are supplied by the manufacturer for your use. In this chapter we will discuss about user-defined
functions.
• Write your prototype into a file, and then use the #include directive to include it in your
program.
• Write the prototype into the file in which your function is used.
• Define the function before it is called by any other function. When you do this, the
definition acts as its own declaration.
130
Although you can define the function before using it, and thus avoid the necessity of creating
a function prototype, this is not good programming practice for three reasons. First, it is a bad
idea to require that functions appear in a file in a particular order. Doing so makes it hard to
maintain the program as requirements change. Second, it is possible that function A() needs to
be able to call function B(), but function B() also needs to be able to call function A() under
some circumstances. It is not possible to define function A() before you define function B()
and also to define function B() before you define function A(), so at least one of them must be
declared in any case. Third, function prototypes are a good and powerful debugging technique.
If your prototype declares that your function takes a particular set of parameters, or that it
returns a particular type of value, and then your function does not match the prototype, the
compiler can flag your error instead of waiting for it to show itself when you run the program.
Function Prototypes
Many of the built-in functions you use have their function prototypes already written in the
files you include in your program by using #include. For functions you write yourself, you
must include the prototype. The function prototype is a statement, which means it ends with a
semicolon. It consists of the function's return type, name, and parameter list. The parameter list
is a list of all the parameters and their types, separated by commas.
The function prototype and the function definition must agree exactly about the return type,
the name, and the parameter list. If they do not agree, you will get a compile-time error. Note,
however, that the function prototype does not need to contain the names of the parameters, just
their types. A prototype that looks like this is perfectly legal:
This prototype declares a function named Area() that returns a long and that has two
parameters, both integers. Although this is legal, it is not a good idea. Adding parameter names
makes your prototype clearer. The same function with named parameters might be
It is now obvious what this function does and what the parameters are. Note that all functions
have a return type. If none is explicitly stated, the return type defaults to int. Your programs
will be easier to understand, however, if you explicitly declare the return type of every function,
including main().
long FindArea(long length, long width);// returns long, has two parameters
131
void PrintMessage(int messageNumber);// returns void, has one parameter
statements;
A function prototype tells the compiler the return type, name, and parameter list. Functions are
not required to have parameters, and if they do, the prototype is not required to list their names,
only their types. A prototype always ends with a semicolon (;). A function definition must
agree in return type and parameter list with its prototype. It must provide names for all the
parameters, and the body of the function definition must be surrounded by braces. All
statements within the body of the function must be terminated with semicolons, but the function
itself is not ended with a semicolon; it ends with a closing brace. If the function returns a value,
it should end with a return statement, although return statements can legally appear
anywhere in the body of the function. Every function has a return type. If one is not explicitly
designated, the return type will be int. Be sure to give every function an explicit return type.
If a function does not return a value, its return type will be void.
132
cout<< "Goodbye.\n";
if (whichMsg> 1)
cout<< "I'm confused.\n";
}
Function Statements
There is virtually no limit to the number or types of statements that can be in a function body.
Although you can't define another function from within a function, you can call a function, and
of course main() does just that in nearly every C++ program. Functions can even call
themselves, which is discussed soon, in the section on recursion. Although there is no limit to
the size of a function in C++, well-designed functions tend to be small. Many programmers
advise keeping your functions short enough to fit on a single screen so that you can see the
entire function at one time. This is a rule of thumb, often broken by very good programmers,
but a smaller function is easier to understand and maintain. Each function should carry out a
single, easily understood task. If your functions start getting large, look for places where you
can divide them into component tasks.
Execution of Functions
When you call a function, execution begins with the first statement after the opening brace ({).
Branching can be accomplished by using the if statement. Functions can also call other
functions and can even call themselves (see the section "Recursion," later in this chapter). Each
function has its own name, and when that name is encountered, the execution of the program
branches to the body of that function. When the function returns, execution resumes on the next
line of the calling function. This flow is illustrated in the following figure.
133
4.4 Scope of variables
A variable has scope, which determines how long it is available to your program and where it
can be accessed. There are two scopes Local and Global.
Local Variables
Not only can you pass in variables to the function, but you also can declare variables within
the body of the function. This is done using local variables, so named because they exist only
locally within the function itself. When the function returns, the local variables are no longer
available. Local variables are defined like any other variables. The parameters passed in to the
function are also considered local variables and can be used exactly as if they had been defined
within the body of the function. Variables declared within the function are said to have "local
scope." That means that they are visible and usable only within the function in which they are
defined. In fact, in C++ you can define variables anywhere within the function, not just at its
top. The scope of the variable is the block in which it is defined. Thus, if you define a variable
inside a set of braces within the function, that variable is available only within that block.
Listing 4.1 is an example of using parameters and locally defined variables within a function.
1: #include <iostream>
3: float Convert(float);
4: int main()
5: {
6: float TempFer;
7: float TempCel;
9: cout<< "Please enter the temperature in Fahrenheit: ";
10: cin>>TempFer;
11: TempCel = Convert(TempFer);
12: cout<< "\nHere's the temperature in Celsius: ";
13: cout<<TempCel<<endl;
14: return 0;
15: }
17: float Convert(float TFer)
18: {
19: float TCel;
134
20: TCel = ((TFer - 32) * 5) / 9;
21: return TCel;
22: }
Output:
Please enter the temperature in Fahrenheit: 212
Here's the temperature in Celsius: 100
Please enter the temperature in Fahrenheit: 32
Here's the temperature in Celsius: 0
Please enter the temperature in Fahrenheit: 85
Here's the temperature in Celsius: 29.4444
Analysis: On lines 6 and 7, two float variables are declared, one to hold the temperature in
Fahrenheit and one to hold the temperature in degrees Celsius. The user is prompted to enter a
Fahrenheit temperature on line 9, and that value is passed to the function Convert().
Execution jumps to the first line of the function Convert() on line 19, where a local variable,
named TCel, is declared. Note that this is local variable that exists only within the function
Convert(). The value passed as a parameter, TFer, is also just a local copy of the variable
passed in by main().The local function variable TCel is assigned the value that results from
subtracting 32 from the parameter TFer, multiplying by 5, and then dividing by 9. This value
is then returned as the return value of the function, and on line 11 it is assigned to the variable
TempCelin the main() function. The value is printed on line 13. The program is run three times.
The first time, the value 212 is passed in to ensure that the boiling point of water in degrees
Fahrenheit (212) generates the correct answer in degrees Celsius (100). The second test is the
freezing point of water. The third test is a random number chosen to generate a fractional result.
Variables declared within a block are scoped to that block; they can be accessed only within
that block and "go out of existence" when that block ends. Global variables have global scope
and are available anywhere within your program. Normally scope is obvious, but there are
some tricky exceptions. Currently, variables declared within the header of a for loop (for int i
= 0; i<SomeValue; i++) are scoped to the block in which the for loop is created.
Global Variables
Variables defined outside of any function have global scope and thus are available from any
function in the program, including main(). Local variables with the same name as global
variables do not change the global variables. A local variable with the same name as a global
variable hides the global variable, however. If a function has a variable with the same name as
a global variable, the name refers to the local variable--not the global--when used within the
function. Listing 4.2 illustrates these points.
135
1: #include <iostream.h>
2: void myFunction(); // prototype
4: int x = 5, y = 7; // global variables
5: int main()
6: {
8: cout<< "x from main: " << x << "\n";
9: cout<< "y from main: " << y << "\n\n";
10: myFunction();
11: cout<< "Back from myFunction!\n\n";
12: cout<< "x from main: " << x << "\n";
13: cout<< "y from main: " << y << "\n";
14: return 0;
15: }
17: void myFunction()
18: {
19: int y = 10;
21: cout<< "x from myFunction: " << x << "\n";
22: cout<< "y from myFunction: " << y << "\n\n";
23: }
Output:
x from main: 5
y from main: 7
x from myFunction: 5
y from myFunction: 10
Back from myFunction!
x from main: 5
y from main: 7
Analysis: This simple program illustrates a few key, and potentially confusing, points about
local and global variables. On line 1, two global variables, x and y, are declared. The global
variable x is initialized with the value 5, and the global variable y is initialized with the value
7. On lines 8 and 9 in the function main(), these values are printed to the screen. Note that the
function main() defines neither variable; because they are global, they are already available to
main().
When myFunction() is called on line 10, program execution passes to line 18, and a local
variable, y, is defined and initialized with the value 10. On line 21, myFunction() prints the
value of the variable x, and the global variable x is used, just as it was in main(). On line 22,
however, when the variable name y is used, the local variable y is used, hiding the global
variable with the same name. The function call ends, and control returns to main(), which
again prints the values in the global variables. Note that the global variable y was totally
unaffected by the value assigned to myFunction()'s local y variable.
136
In C++, global variables are legal, but they are almost never used. Global variables are
dangerous because they are shared data, and one function can change a global variable in a way
that is invisible to another function. This can and does create bugs that are very difficult to find.
When a local variable has the same name as a global variable, all references to the variable name
made with in the scope of the local variable. This situation is illustrated in the following program.
# include<iostream>
float num = 42.8; //global variable
int main()
{
float num = 26.4; //local variable
cout<<”the value of num is:”<<num<<endl;
return 0;
}
As shown by the above output, the local variable name takes precedence over the global
variable. In such cases, we can still access the global variable by using C++’s scope resolution
operator (::). This operator must be placed immediately before the variable name, as in ::num.
When used in this manner, the :: tells the compiler to use global variable.E.g
As indicated the above output, the scope resolution operator causes the global, rather the local
variable to be accessed.
137
Week 9:- Practice
1. Declaration of Functions
• Functions must be declared before use
• The declaration tells the compiler
– The name,
– Return type,
– Parameters of the function
• Three ways
– Write your prototype into a file, and then use the #include directive to include it in
your program.
– Write the prototype into the file in which your function is used.
– Define the function before it is called by any other function.
• The declaration of a function is called its prototype
• Is a statement - it ends with a semicolon
• It consists of the function's
– return type,
– name,
– parameter list
• Syntax
E.g.
or
2. Defining a Function
• The definition tells the compiler how the function works.
• Consists of :
– the function header :
• like the function prototype except that the parameters must be named
• there is no terminating semicolon
– its body
• the task of the function
Syntax
138
return_type function_name(parameter declarations)
statements;
Eg:
return l * w;
• The return statement without any value is typically used to exit the function early
• C++ does not allow nested functions
• The definition of one function cannot be included in the body of another function. A
function definition must agree in return type and parameter list with its prototype
3. Calling a Function
Function can be called in 2 ways. (a) Call by value (b) Call by reference
A. Call by Value:
This is one way of data transformation from calling portion to called portion. Means changes
inside the function cannot affect the main program.
B. Call by reference:
This is two way of data transformation from calling portion to called portion and called portion
to calling portion. Means when function is called by reference, changes inside the function
affect main program also. When function is called, argument corresponding to a reference
parameter is not copied. A reference is an alias for another variable & when we specify a
function argument as a reference type, function will use pass-by-reference technique for
passing the variables. Reason for this is because the parameter name simply becomes alias for
the argument value in the calling program. Whenever variable name is used in the body, it will
access argument value in the calling function directly.
#include<iostream>
cin>>p>>q>>x>>y;
int temp;
q = temp;
int temp;
temp = x;
x = y;
140
y = temp;
printHeading();
• After the function terminates, execution resumes in the calling function at point of call.
} // end main
1 4 9 16 25 36 49 64 81 100
22
Listing 4.1 demonstrates a program that includes a function prototype for the Area() function.
Listing 4.1. A function declaration and the definition and use of that function.
141
1: // Listing 4.1 - demonstrates the use of function prototypes
4: #include <iostream.h>
7: int main()
8: {
9: int lengthOfYard;
14: cin>>widthOfYard;
16: cin>>lengthOfYard;
21: cout<<areaOfYard;
23: return 0;
24: }
27: {
28: return l * w;
29: }
Output:
142
Analysis: The prototype for the FindArea() function is on line 5. Compare the prototype with
the definition of the function on line 26. Note that the name, the return type, and the parameter
types are the same. If they were different, a compiler error would have been generated. In fact,
the only required difference is that the function prototype ends with a semicolon and has no
body. Also note that the parameter names in the prototype are length and width, but the
parameter names in the definition are l and w. The names in the prototype are not used; they
are there as information to the programmer. When they are included, they should match the
implementation when possible. This is a matter of good programming style and reduces
confusion, but it is not required, as you see here.
The arguments are passed in to the function in the order in which they are declared and defined,
but there is no matching of the names. Had you passed in widthOfYard, followed by
lengthOfYard, the FindArea() function would have used the value in widthOfYard for
length and lengthOfYard for width. The body of the function is always enclosed in braces,
even when it consists of only one statement, as in this case.
143
Out of block, in myFunc, x: 8
Back in main, x is: 5
Analysis: This program begins with the initialization of a local variable, x, on line 10, in
main(). The printout on line 11 verifies that x was initialized with the value 5. MyFunc() is
called, and a local variable, also named x, is initialized with the value 8 on line 22. Its value is
printed on line 23. A block is started on line 25, and the variable x from the function is printed
again on line 26. A new variable also named x, but local to the block, is created on line 28 and
initialized with the value 9. The value of the newest variable x is printed on line 30. The local
block ends on line 31, and the variable created on line 28 goes "out of scope" and is no longer
visible.
When x is printed on line 33, it is the x that was declared on line 22. This x was unaffected by
the x that was defined on line 28; its value is still 8. On line 34, MyFunc() goes out of scope,
and its local variable x becomes unavailable. Execution returns to line 15, and the value of the
local variable x, which was created on line 10, is printed. It was unaffected by either of the
variables defined in MyFunc(). Needless to say, this program would be far less confusing if
these three variables were given unique names!
144
Assessments
Practical
1. Write a C++ program using function which accept three integers as an argument and return
its product. Call this function from main() and print the results in the main().
2. Write a program that ask for two numbers, compare them and show the maximum.
Declare a function called max that compares the numbers and returns the maximum.
3. Write a C++ program which determines whether the given number is even or odd by
declaring function.
4. Write an int function cube () that returns the cube of its single int formal parameter.
5. Write a float function triangle() that computes the area of a triangle using its two formal
parameters h and w, where h is the height and w is the length of the bases of the triangle.
6. Write a program that accepts a positive integer from the user and displays the factorial of
the given number. You should use a recursive function called factorial() to calculate the
factorial of the number.
145
Week 10:
Although it is legal for one function to take as a parameter a second function that returns a
value, it can make for code that is hard to read and hard to debug. As an example, say you have
the functions double(), triple(), square(), and cube(), each of which returns a value. You
could write
Answer = (double(triple(square(cube(myValue)))));
This statement takes a variable, myValue, and passes it as an argument to the function cube(),
whose return value is passed as an argument to the function square(), whose return value is
in turn passed to triple(), and that return value is passed to double(). The return value of
this doubled, tripled, squared, and cubed number is now passed to Answer.
It is difficult to be certain what this code does (was the value tripled before or after it was
squared?), and if the answer is wrong it will be hard to figure out which function failed. An
alternative is to assign each step to its own intermediate variable:
Now each intermediate result can be examined, and the order of execution is explicit.
The parameters of a function are list of variables used by the function to perform its task and
the arguments passed to the function during calling of a function are values sent to the function.
The arguments of function calling can be using either of the two supported styles in C++:
passing by value or passing by reference.
146
4.6 Passing arguments
Pass by Value
The arguments passed in to the function are local to the function. Changes made to the
arguments do not affect the values in the calling function. This is known as passing by value,
which means a local copy of each argument is made in the function. These local copies are
treated just like any other local variables.
A value parameter receives a copy of only the value of the argument passed to it. As a result,
if the function makes any changes to the parameters, this will not affect the argument.
For Example:
Num = 0;
int main(void)
int x = 10;
Foo(x);
getch();
return 0;
• The single parameter of Foo is a value parameter. As far as this function is concerned,
num behaves just like a local variable inside the function.
• When the function is called and x passed to it, num receives a copy of the value of x.
As a result, although num is set to 0 by the function, this does not affect x. The program
produces the following output:
147
Num = 0
x = 10
• Passing arguments in this way, where the function creates copies of the arguments passed
to it is called passing by value.
Pass by reference
In C++, passing by reference is accomplished in two ways: using pointers and using references.
The syntax is different, but the net effect is the same. Rather than a copy being created within
the scope of the function, the actual original object is passed into the function. Passing an object
by reference allows the function to change the object being referred to. In previous section
showed that a call to the swap() function did not affect the values in the calling function.
A reference parameter, on the other hand, receives the argument passed to it and works on it
directly. Any change made by the function to a reference parameter is in effect directly applied
to the argument. Passing parameters in this way is called pass-by-reference. Taking the same
example above:
#.....
#.....
void Foo(int & num)
{
num = 0;
cout<< “num = ” << num << “ \n”;
}
int main(void)
{ int x = 10;
Foo(x);
cout<< “x = ”<<x<< “\n”;
getch();
return 0;
}
The parameter of Foo is a reference parameter. Num will correspond to x for this specific
program as it x is sent by its reference and not its value.
Any change made on num will be effected to x. Thus, the program produces the following
output:
num = 0
x=0
148
Suppose you have pairs of numbers in your program and you want to be sure that the smaller
one always precedes the larger one. To do this, you call a function, order(), which checks two
numbers passed to it by reference and swaps the originals if the first is larger than the second.
#.......
#.......
void order(int &, int &);
int main()
{
int n1 = 99, n2=11;
int n3 = 22, n4=88;
order(n1,n2);
order(n3,n4);
cout<< “n1=”<<n1<<endl;
cout<< “n2=”<<n2<<endl;
cout<< “n3=”<<n3<<endl;
cout<< “n4=”<<n4<<endl;
return 0;
}
void order(int & num1,int & num2)
{
if(num1 > num2)
{
int temp=num1;
num1 = num2;
num2 = temp; }
}
}
• In main() there are two pairs of numbers-the first pair is not ordered and the second pair is
ordered. The order() function is called once for each pair, and then all the numbers are
printed out. The output revels that the first pair has been swapped while the second pair has
not. Here it is:
N1 = 11
N2 = 99
N3 = 22
N4 = 88
• In the order() function the first variable is called num1 and the second is num2. If num1 is
greater than num2, the function stores num1 in temp, puts num2 in num1, and finally puts
temp back in num2.
• Using reference arguments in this way is a sort of remote control operation. The calling
program tells the function what variables in the calling program to operate on, and the
function modifies these variables without ever knowing their real names.
149
4.7 Return Values
Functions return a value or return void. Void is a signal to the compiler that no value will be
returned. To return a value from a function, write the keyword return followed by the value
you want to return. The value might itself be an expression that returns a value. For example:
return 5;
return (x > 5);
return (MyFunction());
These are all legal return statements, assuming that the function MyFunction() itself returns
a value. The value in the second statement, return (x > 5), will be zero if x is not greater
than 5, or it will be 1. What is returned is the value of the expression, 0 (false) or 1 (true),
not the value of x.
When the return keyword is encountered, the expression following return is returned as the
value of the function. Program execution returns immediately to the calling function, and any
statements following the return are not executed. It is legal to have more than one return
statement in a single function.
Eg: You might pass a function an error message that is stored in a character array, and the
function displays the error for a certain period of time. The prototype for such a function can
be this:
Therefore, to request that pr_msg() display the line ‘Turn printer on’, you call it this way:
Pr_msg(“Turn printer on”);
As you write more of the program, you begin to realize that you are displaying one message-
for instance, the ‘Turn printer on’ msgmore often than any other message.
Instead of calling the function over and over, typing the same message each time, you can set
up the prototype for pr_msg() so that it defaults to the ‘turn printer on’ message in this way:
void pr_msg(char note[] = “Turn printr on”);
This makes your programming job easier. Because you would usually want pr_msg() to display
‘turn printer on’, the default argument list takes care of the message and you don’t have to pass
the message when you call the function.
150
For functions having more than one argument with default, the parameters with possible
default value should be declared in the right side of the function definition and prototype.
For every parameter you declare in a function prototype and definition, the calling function
must pass in a value. The value passed in must be of the declared type. Thus, if you have a
function declared as
long myFunction(int);
the function must in fact take an integer variable. If the function definition differs, or if you fail
to pass in an integer, you will get a compiler error. The one exception to this rule is if the
function prototype declares a default value for the parameter. A default value is a value to use
if none is supplied. The preceding declaration could be rewritten as
This prototype says, "myFunction() returns a long and takes an integer parameter. If an
argument is not supplied, use the default value of 50." Because parameter names are not
required in function prototypes, this declaration could have been written as
The function definition is not changed by declaring a default parameter. The function
definition header for this function would be
If the calling function did not include a parameter, the compiler would fill x with the default
value of 50. The name of the default parameter in the prototype need not be the same as the
name in the function header; the default value is assigned by position, not name. Any or all of
the function's parameters can be assigned default values. The one restriction is this: If any of
the parameters does not have a default value, no previous parameter may have a default value.
#include…..
void Add_Display(int x=10, int y=20, int z=30)
{
cout<< (x+y+z);
}
void Mult_Dispaly (int x, int y=70)
151
{
cout<< (x*y);
}
void main()
{
int a=40, b=50, c=60;
Add_Display(a,b,c); //will print 150 (ie 40+50+60)
Add_Display(a,b); //will print 120 (ie 40+50+30)
Add_Display(a); //will print 90 (ie 40+20+30)
Add_Display(); //will print 60 (ie 10+20+30)
Mult_Display(a,b) //will print 2000 (40*50)
Mult_Display(a) //will print 2800 (40*70)
//Mult_Display() //is invalid as there is no default for x
getch();
}
//the following function definition is invalid as z is
//a parameter without a default and written after y
//parameters with default should always be at the
//right side of function declaration void
Mult_Dispaly (int x, int y=70, int z)
{
cout<< (x*y*z);
}
//thus the following function definition will be correct
void Mult_Dispaly (int x, int z, int y=70)
{
cout<< (x*y*z);
}
Argument passing
Function is dependent on whether arguments are present or not, and a value is returned or not
to the calling function. Based on these criteria a function can be classified as:
(i) A function with no argument does not receive data from calling function.
(ii) A function with no return value, does not give any data to calling function.
152
Thus the function can be used as an independent statement but not as an expression.
Here the function will receive data from calling function, but will not return any data to it.
#include<iostream>
int main()
int a, b, c;
cin>>a>>b>>c;
big(a, b, c);
else
Here, two way data communication takes place i.e., both the called & calling functions
receive and transfer data from each other.
153
#include<iostream>
int main()
float x, y, c;
cin>>x>>y;
c=area(x, y);
return 0;
154
21: cout<< "Swap. Before swap, x: " << x << " y: " <<y<< "\n";
23: temp = x;
24: x = y;
25: y = temp;
27: cout<< "Swap. After swap, x: " << x << " y: " << y << "\n";
29: }
Output:
Main. Before swap, x: 5 y: 10
Swap. Before swap, x: 5 y: 10
Swap. After swap, x: 10 y: 5
Main. After swap, x: 5 y: 10
Analysis: This program initializes two variables in main() and then passes them to the swap()
function, which appears to swap them. When they are examined again in main(), however,
they are unchanged! The variables are initialized on line 9, and their values are displayed on
line 11. swap() is called, and the variables are passed in. Execution of the program switches to
the swap() function, where on line 21 the values are printed again. They are in the same order
as they were in main(), as expected. On lines 23 to 25 the values are swapped, and this action
is confirmed by the printout on line 27. Indeed, while in the swap() function, the values are
swapped. Execution then returns to line 13, back in main(), where the values are no longer
swapped.
As you've figured out, the values passed in to the swap() function are passed by value, meaning
that copies of the values are made that are local to swap(). These local variables are swapped
in lines 23 to 25, but the variables back in main() are unaffected.
155
22: cout<< "Swap. Before swap, rx: " <<rx<< " ry: "<<ry<<"\n";
24: temp = rx;
25: rx = ry;
26: ry = temp;
28: cout<< "Swap. After swap, rx: " <<rx<< " ry: " <<ry<< "\n";
30: }
Output:
Main. Before swap, x:5 y: 10
Swap. Before swap, rx:5 ry:10
Swap. After swap, rx:10 ry:5
Main. After swap, x:10, y:5
Analysis: Two variables are declared on line 10 and their values are printed on line 12. On
line 13, the function swap() is called, but note that x and y, not their addresses, are passed. The
calling function simply passes the variables.
When swap() is called, program execution jumps to line 18, where the variables are identified
as references. Their values are printed on line 22, but note that no special operators are required.
These are aliases for the original values, and can be used as such. On lines 24-26, the values
are swapped, and then they're printed on line 28. Program execution jumps back to the calling
function, and on line 14, the values are printed in main(). Because the parameters to swap()
are declared to be references, the values from main() are passed by reference, and thus are
changed in main() as well. References provide the convenience and ease of use of normal
variables.
156
23: cout<< "\ninput: " << input << "doubled: " <<result<<"\n";
26: return 0;
27: }
28:
29: int Doubler(int original)
30: {
31: if (original <= 10000)
32: return original * 2;
33: else
34: return -1;
35: cout<< "You can't get here!\n";
36: }
Output:
Enter a number between 0 and 10,000 to double: 9000
Before doubler is called...
input: 9000 doubled: 0
Back from doubler...
input: 9000 doubled: 18000
Enter a number between 0 and 10,000 to double: 11000
Before doubler is called...
input: 11000 doubled: 0
Back from doubler...
input: 11000 doubled: -1
Analysis: A number is requested on lines 14 and 15, and printed on line 18, along with the
local variable result. The function Doubler() is called on line 20, and the input value is passed
as a parameter. The result will be assigned to the local variable result, and the values will be
reprinted on lines 22 and 23. On line 31, in the function Doubler(), the parameter is tested to
see whether it is greater than 10,000. If it is not, the function returns twice the original number.
If it is greater than 10,000, the function returns -1 as an error value. The statement on line 35
is never reached, because whether or not the value is greater than 10,000, the function returns
before it gets to line 35, on either line 32 or line 34. A good compiler will warn that this
statement cannot be executed, and a good programmer will take it out!
1: // demonstrates use
2: // of default parameter values
4: #include <iostream>
6: int AreaCube(int length, int width = 25, int height = 1);
8: int main()
9: {
10: int length = 100;
11: int width = 50;
12: int height = 2;
13: int area;
14:
157
15: area = AreaCube(length, width, height);
16: cout<< "First area equals: " << area << "\n";
18: area = AreaCube(length, width);
19: cout<< "Second time area equals: " << area << "\n";
21: area = AreaCube(length);
22: cout<< "Third time area equals: " << area << "\n";
23: return 0;
24: }
25:
26: AreaCube(int length, int width, int height)
27: {
28:
29: return (length * width * height);
30: }
Output:
First area equals: 10000
Second time area equals: 5000
Third time area equals: 2500
Analysis: On line 6, the AreaCube() prototype specifies that the AreaCube() function takes
three integer parameters. The last two have default values. This function computes the area of
the cube whose dimensions are passed in. If no width is passed in, a width of 25 is used and a
height of 1 is used. If the width but not the height is passed in, a height of 1 is used. It is
not possible to pass in the height without passing in a width. On lines 10-12, the dimensions
length, height, and width are initialized, and they are passed to the AreaCube() function on
line 15. The values are computed, and the result is printed on line 16. Execution returns to line
18, where AreaCube() is called again, but with no value for height. The default value is used,
and again the dimensions are computed and printed. Execution returns to line 21, and this time
neither the width nor the height is passed in. Execution branches for a third time to line 27.
The default values are used. The area is computed and then printed.
DO remember that function parameters act as local variables within the function.
DON'T try to create a default value for a first parameter if there is no default value for the second.
DON'T forget that arguments passed by value cannot affect the variables in the calling function.
DON'T forget that changes to a global variable in one function change that variable for all functions.
158
Assessment
Part I: Objective Questions: Choose the correct answer from the given alternatives
A. Parameters C. Arguments
B. Constants D. Variables
5. What are the basic parts in function declaration?
A. Return type, function name C. Function name
B. Return type, function name, parameters D. All
6. Keywords in C++ are ignored by the compiler.
A. True B. False
7. Which of the following is Programmer given name?
A. Directives C. Keywords
B. Identifiers D. Comments
8. What is the only function all C++ programs must contain?
A. start() C. main()
B. system() D. program()
159
1. What is function?
2. Write the syntax of function declaration with example?
3. Write the definition of the function that you declare in question number 2?
4. Write the function calling that you define in question number 3?
5. What is the difference between parameter passing by value and by reference?
6. What is the difference between parameter and argument?
7. What is inline function? Explain its difference from normal function?
Part III: Practical Questions
1. Write a C++ program by creating a function with three default integer parameters display
the sum. Call the function four times from main().
• Call with no argument
• Call with one argument
• Call with two argument
• Call with three argument
2. Write a program that lets the user perform arithmetic operations on two numbers. Your
program must be menu driven, allowing the user to select the operation (+, -, *, or /) and
input the numbers. your program must consist of following functions:
• Function showChoice: This function shows the options to the user and explains
how to enter data.
• Function add: This function accepts two number as arguments and returns sum.
• Function subtract: This function accepts two number as arguments and returns
their difference.
• Function multiply: This function accepts two number as arguments and returns
product.
• Function divide: This function accepts two number as arguments and returns
quotient.
a)
#include<iostream>
using namespace std;
void function1();
int main()
{
function1();
cout<<" I am ICT first year student"<<endl;
cout<<" Programming is interesting"<<endl;
160
return 0;
}
void function1()
{
cout<<"Hello every one"<<endl;
}
b)
#include<iostream>
using namespace std;
int sum(int=10,int=5,int=2);
int main()
{
sum(10,10);
sum();
sum(7);
sum(2,3,4);
return 0;
}
int sum( int a, int b, int c)
{
int result;
result=a+b+c;
cout<<"sum="<<result<<endl;
}
161
Week 11:
int iabs(int i)
{
if(i<0)
return (i*-1);
else
return (i);
}
float fabs(float x)
{
if(x<0.0)
return (x * -1.0);
else
return (x);
}
162
else
return I;
}
float abs(flaot x)
{
if(x<0.0)
return x*-1.0;
else
return x;
}
N.B: if two or more functions differ only in their return types, C++ can’t overload them. Two
or more functions that differ only in their return types must have different names and can’t be
overloaded
(n > 0 ? n : -n)
However, instead of replicating this expression in many places in the program, it is better to
define it as a function:
int Abs(int n)
{
return n > 0 ? n : -n;
}
The function version has a number of advantages.
• Its frequent use can lead to considerable performance penalty due to overheads
associated with calling a function.
The effect of this is that when Abs is called, the compiler, instead of generating code to call
Abs, expands the program body and substitutes the body of Abs in place of the call. While
essentially the same computation is performed, no function call is involved and hence no stack
frame is allocated.
163
The "inline" keyword is merely a hint to the compiler or development environment. Not every
function can be inlined. Some typical reasons why inlining is sometimes not done include:
Another good reason to inline is that you can sometimes speed up your program by inlining the
right function. Instead of calling the function every time it is invoked, the compiler will replace
the function call with a copy of the function body. If it's a small function which gets called a
lot, this can sometimes speed things up.
Most of the advantage of inline functions comes from avoiding the overhead of calling an actual
function. Such overhead includes saving registers, setting up stack frames, and so on. But with
large functions the overhead becomes less important.
Concerning inline functions, the compiler is free to decide whether a function qualifies to be
an inline function. If the inline function is found to have larger chunk (amount) of code, it will
not be treated as an inline function, but as like other normal functions.
Then, Why not inline everything? :Since the compiler will copy the entire function body every
time the function is called, if it is a large function (more than three or four lines), inlining can
increase the size of your executable program significantly.
When you define a function, normally the compiler creates just one set of instructions in
memory. When you call the function, execution of the program jumps to those instructions,
and when the function returns, execution jumps back to the next line in the calling function. If
you call the function 10 times, your program jumps to the same set of instructions each time.
This means there is only one copy of the function, not 10.
There is some performance overhead in jumping in and out of functions. It turns out that some
functions are very small, just a line or two of code, and some efficiency can be gained if the
program can avoid making these jumps just to execute one or two instructions. When
programmers speak of efficiency, they usually mean speed: the program runs faster if the
function call can be avoided.
If a function is declared with the keyword inline, the compiler does not create a real function:
it copies the code from the inline function directly into the calling function. No jump is made;
it is just as if you had written the statements of the function right into the calling function.
Syntax
164
statements;
Note that inline functions can bring a heavy cost. If the function is called 10 times, the inline
code is copied into the calling functions each of those 10 times. The tiny improvement in speed
you might achieve is more than swamped by the increase in size of the executable program.
What's the rule of thumb? If you have a small function, one or two statements, it is a candidate
for inline. When in doubt, though, leave it out.
target = 2 * target;
target = Double(target);
By the time your program executes, the instructions are already in place, compiled into the
OBJ file. This saves a jump in the execution of the code, at the cost of a larger program.
factorial of 0 is 1
The second line clearly indicates that factorial is defined in terms of itself and hence can be
expressed as a recursive function.
return n == 0 ? 1 : n * factrial(n-1);
165
For n set to 4, the following figure shows the recursive call:
The stack frames for these calls appear sequentially on the runtime stack, one after the other.
A recursive function must have at least one termination condition which can be satisfied.
Otherwise, the function will call itself indefinitely until the runtime stack overflows.
Let us implement two more mathematical functions using recursion. E.g the following function
computes the sum of the first N positive integers 1,2,…,N. Notice how the function includes
the three necessary components of a recursive method.
int sum(int N)
{
if(N==1)
return 1;
else
return N+sum(N);
}
The last method computes the exponentiation An where A is a real number and N is a positive
integer. This time, we have to pass two arguments. A and N. the value of A will not change in
the calls, but the value of N is decremented after each recursive call.
166
0,1,1,2,3,5,8,13,21,…
Fibonacci (0) =0
Fibonacci (1) =1
Recursion has disadvantage as well. It repeatedly invokes the mechanism, and consequently
the overhead of method calls. This can be costly in both processor time and memory space.
Each recursive call creates another copy of the method (actually, only the function’s variables);
this consumes considerable memory.
Week 11 :- Practice
Most mathematical functions that we are familiar with are described by a simple formula. For
example, we can convert temperatures from Fahrenheit to Celsius by applying the formula
C = 5 (F-32) / 9
Given this formula, it is trivial to write a C++function; with declarations and braces removed,
the one line formula above translates into one line C++ statement. Mathematical functions are
sometimes defined in a less standard form. For example we can define a function f, valid on
non negative integers, that satisfies:
167
f(0) = 0
1. Base case: you must always have some Base case, which can be solved with out
recursion.
2. Making progress: for the cases that are to be solved recursively, the recursive call must
always be to a case that makes progress to ward a base case.
3. Design rule: Assume that all recursive calls work without fail. In order to find f(n)
recursively you can assume f(n-k) will work (k 1)
4. Compound interest rule: never duplicate work by solving the same instance of a
problem in separate recursive rule.
Some examples of recursive problems
168
Nx f(N - 1) for N 0
f (N ) = this is called recursion.
1 for N = 0
169
else if n > 2
{
int val1 = i_th_fibonacci_element ( n-1);
int val2 = i_th_fibonacci_element ( n-2);
return (val1 + val2);
}
}
Some function cannot be implemented easily without recursion. Other functions, which
do admit both iterative and recursive solution are easier to understand in their recursive
form. So recursion is an essential tool for computer scientist.
It might seem that finding gcd of large integers could be very tedious and time
consuming. But fortunately the ancient Greek scientist Euclid discovered a clever
recursive algorithm as follow.
N if N = M
gcd (M, N) = gcd (N, M) if M N
gcd (N - M, M) if M N
if (m == n)
170
return n;
else if (m > n)
else
D) Binary Search
Binary search for an element x in sorted array A[] looks at the middle of the array. If that is not
x (the middle element of A ≠ x) then it continues the search in the half that potential could
hold x. Assuming the array is sorted in ascending order, the potential half array is the left array
if the middle is larger than x otherwise the right the potential list to hold x. The process
contributes this recursive step until either x is found or the sub array is empty. What should be
returned in the index of the array that hold x if it founds or -1.
Implementation
return -1;
if (x == a[mid]
return mid;
if (x < a[mid]
else
171
The call from other program to this function may look like
A B C
The objective is to transfer the entire disks from peg A to one of the other pegs, say C moving
only one disk at one time, never moving the larger disk on the top of smaller disk.
1- Reduce the problem that say move the top N-1 disk from peg A to B using C.
2- Move the Nth disk (which is the largest) from peg A to C.
3- Now move the N-1 disks from B to C using A. And this will tell us what to do for any N.
The implementation is as follows:
#include <iostream>
#include <conio.h>
void Form(int N, char pegA ,char pegB , char pegC )
{
if (N==1)
cout<<"move top disk on peg "<<pegA<<" to peg "<<pegC<<endl;
else
{
Form(N-1,pegA,pegC,pegB);
cout<<"move top disk on peg "<<pegA<<" to peg "<<pegC<<endl;
Form(N-1,pegB,pegA,pegC);
}
172
}
int main ()
{
char F;
int N;
cout<<"How many disks ?=====>";cin>>N;
Form(N,'A','B','C') ;
getch () ;
return 0;
}
Output:
Enter a number to work with: 20
Target: 40
173
Target: 80
Target: 160
Analysis: On line 5, Double() is declared to be an inline function taking an int parameter and
returning an int. The declaration is just like any other prototype except that the keyword inline
is pretended just before the return value. This compiles into code that is the same as if you had
written the following:
174
Assessment
Practical Questions
1. Write a function named mult() that accepts two floating-point numbers as a parameters,
multiplies these two numbers and display the result.
2. Write a function that accepts a year as a user input and returns a one if the year is a leap
year or a zero if it is not.
3. Write a program that can be used to play a number guessing game. The player is first asked
for the number of games he wishes to play. Each game gives the player a maximum of three
chances to guess a number chosen randomly. If the player makes a correct guess, he is
given a score depending on the number of guesses he has made (10, 8, or 5). The final score
is given as a percentage with some complementary remarks.
4. Consider the following program:
int foo(int x)
static int k = x;
if (x == 0)
return 0;
if (x < 0)
return k + foo(-x);
else {
int y = foo(x-1);
return k + y + 1;
175
# include <iostream>
char key;
int main()
int a,b,c;
double x,y;
return 0;
double secnum;
int o,p;
float q;
return p;
int a,b,c,o,p;
floar r;
double s,t,x;
return s*t; }
176
Draw a box around the appropriate section of the above code to enclose the scope of
the variables key, secnum, y, and r.
6. Describe the difference between a local auto variable and a local static variable.
7. Given the following code:
int sum(int 0)
if(n==0)
return 0;
else
return(n+sum(n-1));
int firstnum=20;
void display(void);
int main()
display();
return 0;
177
void display(void)
return;
10. Write a modular program that reads a list of marks and display their range, mean, median.
Suggested functions and their prototypes:
178
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill,
2003
[3]. P B Mahapatra, Programming in C++, S Chand, 2008
[4]. Ashok Kamthane, Programming in C++, Pearson, 2013
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com/
179
Chapter 5: Structure
Lesson Plan
1. Learning objectives
• Be able to define structure.
• Be able to declare structure and structure variables.
• Be able to initialize structure variables.
• Be able to access members of structure variable
• Be able to define structure within a stucture.
2. Motivation
• Outputs through Question and Answer; repetitive discussion and using structure in
C++.
• Solving a problem using structure in C++.
3. Expectations or Outcomes
Define the terminologies, differentiate different types of function, explain (structure
declaration, members of structure, and accessing member variables); the assessment
standard attached at the end of the job task sheet.
Can write C++ instructions to solve problem using structure in C++ programming.
4. Equipment
• Desktop or Laptop computer.
• DEV C++ software
5. Practice contents/Activities/Safety
Active practice; on declaring of structure, member variables, accessing member varia
bles.
Active practice on array of struts and structure of structure.
6. Assessments
• How to apply structure in C++ programming?
• How to create structure using C++?
7. Clean-up
After finishing practice, computers should be properly shut down.
8. Independent practice/Follow-up activities
• Learning through assignment (practical Exercises)
9. Review/Reflection
• Review the outcome of the practice, improvement measure and previous reflected
opinions.
181
Week 12 :
This isn’t so bad, but what happens if you want to store two dates and not one? You’d have to
create three more variables and give them unique names:
int day1, month1, year1;
int day2, month1, year2;
This begins to become a hassle. Not only do you have to create many variables, but you have
to keep giving them unique names. C++ provides a way to collect similar variables into a
single structure.
An array is a data structure which holds multiple numbers of objects having the same basic
property (data type) in a contiguous memory slots. Programmer can also reserve contiguous
memory space for aggregates of elements of arbitrary data types each. A data type which is
created to reserve such type of memory space is called user defined data type.
User defined data types can be equally used like predefined data types to declare variable
identifiers. For example we can define simple, array, or pointer variable identifier from this
user defined data type.
5.1.1 Structure
The term structure in C++ means both a user-defined type which is a grouping of variables as
well as meaning a variable based on a user-defined structure type. For the purpose of distinction
we will refer to the user-defined type side as structure definition and the variable side as
structure variable.
A structure definition is a user-defined variable type which is a grouping of one or more
variables. The type itself has a name, just like ‘int’, ‘double’, or ‘char’ but it is defined by the
user and follows the normal rules of identifiers. Once the type has been defined through the
C++ ‘struct’ keyword, you can create variables from it just like you would any other type.
Since a structure definition is a grouping of several types: it is a group of one or more variables.
These are known as elements or member variables as they are members of the structure
definition they are part of. Following through with our hinted example, a structure definition
could be a ‘date’ which might be made up of three ‘int’ member variables: ‘day’, ‘month’, and
‘year’.
182
Before creating a structure variable you must create a structure definition. This is a blue print
for the compiler that is used each time you create a structure variable of this type. The structure
definition is a listing of all member variables with their types and names.
When you create a structure variable based on a structure definition, all of the member variables
names are retained. The only name you have to give is that of the new structure variable. The
element names within that variable will be the same as in the structure type. If you create two
structure variables from ‘date’, both will have all three member variables with the same name
in both: ‘day’, ‘month’, and ‘year’.
Member variables are distinguished by the structure variable they are part of. You wouldn’t
simply be able to use ‘day’ by itself; instead you’d have to refer to both the structure variable’s
name (that you gave when you created it) as well as ‘day’.
};
Discussion: Writing a structure definition begins with the word ‘struct’ followed by the type-
to-be, and ended with a structure block that is ultimately terminated with a semicolon. Do not
forget this trailing semi-colon when defining a structure!
The name of a structure definition is known as the structure tag. This will be the name of the
type that you create, like ‘int’ or ‘float’. It is the type that you will specify when creating a
structure variable. This structure block is remarkably similar to a statement block since it starts
and ends with curly braces. But don’t forget that it ultimately ends with a semi-colon. Within
the structure block you declare all the member variables you want associated with that type.
Declare them as you would normal variables, but do not try to initialize them. This is simply
a data blue print, it is not logic or instructions and the compiler does not execute it.
The data members (synonym for member variables) of a structure won’t actually be created
until a variable based on the structure is created. Technically an ‘int’ is just this as well. It’s a
description of a storage unit. That storage unit isn’t reserved until you create a variable with
it.
183
Example defining a student struct,
struct student
{
int id;
char name[15];
};
Example: The follow defines a structure called ‘date’ which contains three ‘int’ member
variables: ‘day’, ‘month’, and ‘year’:
struct date {
int day;
int month;
int year;
};
struct date
{
int day, month, year;
};
Note: You cannot initialize member variables in a structure definition. The following is wrong
and will not compile:
struct date{
int day = 24, month = 10, year = 2001;
};
A structure definition has the same type of scoping as a variable. If you define a structure in a
function, you will only be able to use it there. If you define it in a nested statement block, you
will only be able to use it inside there and any statement blocks nested within it. But the most
common place is defining it globally, as in outside of any functions or blocks. Typically, you’ll
want to be able to create variables of the defined structure anywhere in your program, so the
definition will go at the top. The following is an empty program that defines a ‘date’ structure
globally:
#include <iostream>
struct date
{
int day, month, year;
};
int main(){
return 0;
}
student std1={143,"Ababe"};
This creates a variable called ‘nco_birthday’ and initializes it to a list of values. The values are
assigned to the member variables in the order they are declared in the structure definition.
Remember what is mentioned about each member variable having an offset. The same order
in which each member is given an offset is the order in which each is assigned a value in an
initialization. So the first initializer is used to initialize the first member variable in the
structure, next is the second, and so on and so forth. This order of initialization continues until
the values run out.
If you try to assign more values than are member variables, you will get a compiler error.
However, you can assign fewer values than there are member variables. If there are no more
values to be assigned, the assignment will simply end. For example, if we had omitted the last
value, ‘1979’, then no value would be assigned to ‘year’.
185
It is possible to use any expression that you normally would. But remember that the expression
must result in a value. Here is an example of initialization with things other than literals:
int myday = 19;
int mymonth = 5;
date nco_birthday = { myday, mymonth + 3, 2001 - 22 };
Although you can assign a value to a variable in the same way you initialize it, the same is not
true with structures. So while this works:
int x;
x = 0;
This doesn’t:
date nco_birthday;
nco_birthday = { 19, 8, 1979 };
Assigning values to multiple members of a structure variable is only possible when that
variable is first created. Once a structure variable has been declared, you must access each
member individually.
186
Week 12: Practice
C/C++ arrays allow you to define variables that combine several data items of the same kind,
but structure is another user defined data type which allows you to combine data items of
different kinds. A structure is a collection of one or more variable types grouped together that
can be referred using a single name (group name) and a member name.
You can refer to a structure as a single variable, and you also can initialize, read and change
the parts of a structure (the individual variables that make it up). Before creating a structure
variable you must create a structure definition. Each element (called a member) in a structure
can be of different data type.
The General Syntax of structures is: tag
struct structname
{
datatype1 variable1;
Members
datatype2 variable2;
};
Example of struct Inventory
struct Inventory
{
char description[15];
char part_no[6];
int quantity; // members of the structure
float cost;
}; // all structures end with semicolon
187
• The above “Student” structure is aimed to store student record with all the relevant
details.
• After the definition of the structure, one can declare a structure variable using the
structure tag. If we need two variables to have the above structure property then
the declaration would be:
struct Inventory inv1,inv2; //or
struct Student Stud1,stud2,Stud3;
• Structure tag is not a variable name. Unlike array names, which reference the array
as variables, a structure tag is simply a label for the structure’s format.
• The structure tag Inventory informs C++ that the tag called Inventory looks like
two character arrays followed by one integer and one float variables.
• A structure tag is actually a newly defined data type that you, the programmer,
defined
Referencing members of a structure
• To refer to the members of a structure we need to use the dot operator (.)
• The General syntax to access members of a structure variable would be:
VarName.Member
• Where VarName is the varaibale name of the structure variable And Member is
varaibale name of the members of the structure
Eg:
For the above student structure:
struct Student Stud; //declaring Stud to have the property of the Student structure
You can initialize members when you declare a structure, or you can initialize a structure in
the body of the program. Here is a complete program.
.
..
struct cd_collection
{
char title[25];
188
char artist[20];
int num_songs;
float price;
char date_purchased[9];
}
cd1 = {"Red Moon Men","Sams and the Sneeds", 12, 11.95,"08/13/93"};
cout<<"\nhere is the info about cd1"<<endl;
cout<<cd1.title<<endl;
cout<<cd1.artist<<endl;
cout<<cd1.num_songs<<endl;
cout<<cd1.price<<endl;
ut<<cd1.date_purchased<<endl;
.
.
.
A better approach to initialize structures is to use the dot operator(.). the dot operator is one
way to initialize individual members of a structure variable in the body of your program. The
syntax of the dot operator is : structureVariableName.memberName
Structures are used to represent a record, suppose you want to keep track of your books in a
library. You might want to track the following attributes about each book −
• Title
• Author
• Subject
• Book ID
• Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member, for your program. The format of the struct statement
is this –
189
The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's
definition, before the final semicolon, you can specify one or more structure variables but it is
optional. Here is the way you would declare the Book structure –
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access. You would use struct keyword to define variables of structure
type. Following is the example to explain usage of structure –
#include <iostream>
#include <cstring>
using namespace std;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
190
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result –
here is an example:
#include<iostream>
#include<conio>
#include<string.h>
void main()
{
clrscr();
struct cd_collection{
191
char title[25];
char artist[20];
int num_songs;
float price;
char date_purchased[9];
}cd1;
//initialize members here
strcpy(cd1.title,"Red Moon Men");
strcpy(cd1.artist,"Sams");
cd1.num_songs= 12;
cd1.price = 11.95f;
strcpy(cd1.date_purchased,"22/12/02");
//print the data
cout<<"\nHere is the info"<<endl;
cout<<"Title : "<<cd1.title<<endl;
cout<<"Artist : "<<cd1.artist<<endl;
cout<<"Songs : "<<cd1.num_songs<<endl;
cout<<"Price : "<<cd1.price<<endl;
cout<<"Date purchased : "<<cd1.date_purchased;
getch();
}
Example:-a program that creates student struct and uses it to store student information.
#include<iostream>
#include<conio.h>
using namespace std;
struct student
{
int id;
char name[15];
};
int main()
{
//creating three student variables
student s1,s2;
cout<<"\n Enter Student Id";
cin>>s1.id;
cout<<"\nEnter Name";
cin>>s1.name;
cout<<"\n Enter Student Id";
cin>>s2.id;
192
cout<<"\nEnter Name";
cin>>s2.name;
cout<<"\nStudents Information";
cout<<"\n Student id\t Student Name";
cout<<endl<<s1.id<<"\t""\t""\t"<<s1.name;
cout<<endl<<s2.id<<"\t""\t""\t"<<s2.name;
getch();
}
The above program shows you how to capture data in student variables.
Example 2: This program demonstrates using member variables for user input, output, and
mathematical operations.
#include <iostream>
using namespace std;
struct date
{
int day, month, year;
};
int main()
{
date birth;
cout << "Enter your birth date!" << endl;
cout << "Year: ";
cin >> birth.year;
cout << "Month: ";
cin >> birth.month;
cout << "Day: ";
cin >> birth.day;
cout << "You entered " << birth.day << "/"<< birth.month
<< "/" << birth.year << endl;
cout << "You were born in the "<< ((birth.year / 100) + 1)
<< "th Century!"<< endl;
return 0;
}
193
Assessment
Part I: Answer the following questions based on the program given below?
// Program which demonstrates structure
#include<iostream>
using namespace std;
struct student {
int id;
char name[15];
};
int main()
{
//creating 5 student using an array
student s[5];
int i;
for(i=0; i < 5; i ++)
{
cout<<"\n Enter Student Id";
cin>>s[i].id;
cout<<"\nEnter Name";
cin>>s[i].name;
}
cout<<"\n Displaying student Info";
cout<<"\nStudent Id \t Student Name";
cout<<"\n============================";
for( i = 0; i < 5; i++)
cout<<endl<<s[i].id<<"\t\t\t"<<s[i].name;
getch();
}
194
Part II: Answer the following questions?
1. Write a C++ program which shows list of books which have title, Author and price
using structure data type and the total number of books to be displayed should be
entered from user.
2. Write a C++ program by creating structure data type which accepts two distances
(in inch & feet), adds the two inches together and the two feets’ together and
display the result on the screen.
3. Write a C++ program which stores the information (name, Id and marks of three
subjects entered by the user) of a student in a structure and displays name, Id and
average of marks entered for one student. The marks entered should be between 1
and 100
195
Week 13:
An array is a collection of data items of the same type. Each element of the array can be int,
char, float, double, or even a structure. We have seen that a structure allows elements of
different data types to be grouped together under a single name. This structure can then be
thought of as a new data type in itself. So, an array can comprise elements of this new data
type. An array of structures finds its applications in grouping the records together and provides
for fast accessing. Below is the demonstration of an array of structures. The array holds the
details of the students in a class. The details include the roll number, grade, and marks, which
have been grouped under a structure (record). There exists one record for each student.
#include<iostream>
#include<conio.h>
struct student
{
int id;
char name[15];
};
void main()
{
clrscr();
//creating 5 student using an array
student s[5];
int i;
for(i=0; i < 5; i ++)
{
cout<<"\n Enter Student Id";
cin>>s[i].id;
cout<<"\nEnter Name";
cin>>s[i].name;
}
cout<<"\n Displaying student Info";
cout<<"\nStudent Id \t Student Name";
cout<<"\n============================";
for( i = 0; i < 5; i++)
cout<<endl<<s[i].id<<"\t\t\t"<<s[i].name;
getch();
}
196
. Table 5-1: Memory map of the above struct declaration
0 id 1
name Tameru
1 id 2
name Hassen
2 id 3
name Selamawit
3 id 4
name Asia
4 id 5
name Micheal
If you use s[0].id you will be referring to 1, and s[0].name will refer to Tameru.
The following program declares and uses a book struct. Also swaps the content of two book
struct variables.
#include<iostream>
#include<conio.h>
struct Book
{
int id;
char title[15];
};
void main()
{
//creating three Book variables
Book b1,b2,temp;
cout<<"\n Enter Book Id";
cin>>b1.id;
cout<<"\nEnter Title";
cin>>b1.title;
cout<<"\n Enter Book Id";
cin>>b2.id;
cout<<"\nEnter Title";
197
cin>>b2.title;
cout<<"\n Book Information";
cout<<"\n Before Changing Contents";
cout<<"\n Book id\t Title";
cout<<"\n=========================";
cout<<endl<<b1.id<<"\t\t\t"<<b1.title;
cout<<endl<<b2.id<<"\t\t\t"<<b2.title;
//swapping content
temp=b1;
b1=b2;
b2=temp;
cout<<"\nAfter swapping contents";
cout<<"\n Book Information";
cout<<"\n Book id\t Title";
cout<<"\n=========================";
cout<<endl<<b1.id<<"\t"<<b1.title;
cout<<endl<<b2.id<<"\t"<<b2.title;
getch();
}
Declaring struct types as part of a struct
A structure definition contains multiple variables, but not necessarily just primitives. You can
define a structure to have structure member variables.
Now if you have data's like birth of day of an employee, published year of a book, address of
a person. What are you going to do? You must be able to incorporate this type of data's in
other structs. The following program declares two structs one for address and other for student.
#include<iostream>
#include<conio.h>
struct Address
{
int kebele;
char Kefle_ketema[20];
char roadname[20];
};
struct Student {
int id;
char name[15];
char section[6];
//declaring address type within student
Address studaddress;
};
198
void main(){
clrscr();
//creating Student type that encapsulates Address
Student s1;
cout<<"\n Enter Student Id";
cin>>s1.id;
cout<<"\nEnter Student Name";
cin>>s1.name;
cout<<"\n Enter Section";
cin>>s1.section;
//reading address attributes
cout<<"\nEnter Kebele";
cin>>s1.studaddress.kebele;
cout<<"\nEnter Street Name";
cin>>s1.studaddress.roadname;
cout<<"\nEnter Kefle Ketema";
cin>>s1.studaddress.Kefle_ketema;
cout<<"\n Student Information";
cout<<"\n id\t Name\t Section \t Kebele";
cout<<" \t Street Name \t Kefele Ketema ";
cout<<"\n==================================";
cout<<endl<<s1.id<<"\t"<<s1.name;
cout<<"\t"<<s1.section<<"\t"<<s1.studaddress.kebele;
cout<<"\t"<<s1.studaddress.roadname<<"\t";
cout<<s1.studaddress.Kefle_ketema;
getch();
}
Table 5-2: The memory map of student s;
Id 1
Name Asefa
Section RDDOB02
studaddress kebele 21
Kefle_ketema Arada
roadname Gahandi
Using the above figure. You should be able to understand how data is accessed. Example s.id
refers id of student, but s.studadress.kebele refers kebele of student where the kebele is
referenced by studaddress, intern part of student.
199
Example: This assumes that ‘date’ and ‘time’ are structures that have already been declared.
struct moment
date theDate;
time theTime;
};
These structure members can then be accessed as normal member variables, but then their
variables must be accessed as well. If there is a variable based on ‘moment’ called ‘birth’, then
we would need to write the following to assign ‘19’ to “birth’s date’s day”:
birth.theDate.day = 19;
A structure only has to be declared before it can be used in another structure’s definition. The
following is perfectly acceptable:
struct date;
struct time;
struct moment
{
date theDate;
time theTime;
};
struct date
{
int day, month, year;
};
struct time
{
int sec, min, hour;
};
To be able to define a structure you only must know the types and the names of the member
variables declared inside. With the above we declare the structures ‘date’ and ‘time’ but do not
define them until later. This simply acknowledges that they exist and they can therefore be
used within ‘moment’.
What if ‘date’ and ‘time’ hadn’t defined? It would still be legal, but then I would not be able to
use ‘moment’ at all. Why? Since ‘date’ or ‘time’ have not been defined, the compiler does not
know how big they are supposed to be or what kind of data they contain. You couldn’t then
200
create a variable based on ‘moment’ because the compiler doesn’t know how big of a memory
block to allocate. Likewise if you try to use a structure that has been declared before it has
been defined, you will encounter the same problem.
It is possible to define a structure inside a structure definition and create variables from it at
the same time. For example:
struct moment
{
struct date
{
int day, month, year;
} theDate;
struct time
{
int second, minute, hour;
} theTime;
};
The drawback of the above is that the ‘date’ and ‘time’ definitions cannot be used elsewhere
without also referring to the parent structure. If the ‘date’ definition isn’t going to be used
elsewhere anyway, the structure tag can simply be omitted. Thus we could remove the ‘date’
and ‘time’ structure type identifiers and it would work fine. You can write any number of
variables in a structure definition and a valid variable declaration statement (minus
initialization of course) is valid inside a structure definition.
Let’s say we want to be able to use ‘date’ elsewhere, but not ‘time’. The following program
demonstrates how the structure definitions would be written as well as uses the defined
structure types in an extended “birth date” sample:
#include <iostream.h>
struct date { int day, month, year; } ;
struct moment
{
date theDate;
201
struct
{
int sec, min, hour;
} theTime;
};
int main()
{
moment birth;
cout << “Enter your birth moment!” << endl;
cout << “Year: “;
cin >> birth.theDate.year;
cout << “Month: “;
cin >> birth.theDate.month;
cout << “Day: “;
cin >> birth.theDate.day;
cout << “Hour (military): “;
cin >> birth.theTime.hour;
cout << “Minute: “;
cin >> birth.theTime.min;
cout << “Second: “;
cin >> birth.theTime.sec;
cout << “You entered “ << birth.theDate.month << “/”
<< birth.theDate.day << “/” << birth.theDate.year
<< “ @ “ << birth.theDate.hour << “:”
<< birth.theDate.min << “:” << birth.theDate.sec
<< endl;
if (birth.theTime.hour > 20 || birth.theTime.hour < 8)
cout << “You were born early in the morning!”
<< endl;
return 0;
}
Any number of structure definitions can be nested; you can get extremely complex with
structures, which is why they are sometimes known as complex types.
References to structure variables, work the same way as normal references. To create one you
would write out the name of the structure type, followed by the reference name which is
preceded by the reference operator (&):
struct_name &reference;
202
The following creates a structure variable based on ‘date’ and a reference to it:
date birth;
Both of these, ‘birth’ and ‘mybirth’, would have access to the same member variables and their
values. Thus they can be used interchangeably:
birth.year = 1981;
mybirth.year -= 2;
Can you guess what the value of ‘birth.year’ would be from the above? It would be ‘1979’.
The reference ‘mybirth’ is just an alias to ‘birth’ and its group of member variables. Remember
that a reference is not a real variable (it has no value), but simply a nickname for another.
date birth;
date *p = &birth;
(*p).year = 1979;
The pointer ‘p’ above, had to be dereferenced before the member variable ‘year’ could be
accessed through it. It was surrounded in parenthesis because the indirection operator (asterisk
‘*’) has a lower precedence than the member operator. Thus the following would not work:
*p.year = 1979;
This would be seen as “get the value pointed to by ‘p.year’” and ‘p.year’ is an invalid identifier;
hence the parenthesis around the indirection operation. This method of accessing a structure
variable’s members is cumbersome and requires two operations simply to get at the member
variable: indirection and then member. For this purpose, there is an operator specifically for
accessing a structure variable’s members through a pointer. This is a member operator known
specifically as a pointer-to-member operator or a dash followed by a greater than sign ‘->’ (also
known as an “arrow”):
p->year = 1979;
203
This operator only works on pointers to structure variables. You cannot use it on normal
structure variables for members. The following would not work:
birth->year = 1979;
The left operand must be a pointer to a variable with members, and the right operand must be
the name of a member variable within that.
Pointers to Structures
Like any other type, structures can be pointed to by its own type of pointers:
struct movies_t {
string title;
int year;
};
movies_t amovie;
movies_t * pmovie;
Here amovie is an object of structure type movies_t, and pmovie is a pointer to point to objects
of structure type movies_t. Therefore, the following code would also be valid:
pmovie = &amovie;
The value of the pointer pmovie would be assigned the address of object amovie.
Now, let's see another example that mixes pointers and structures, and will serve to introduce
a new operator: the arrow operator (->):
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
};
int main ()
{
string mystr;
204
movies_t amovie;
movies_t * pmovie;
pmovie = &amovie;
return 0;
}
The output is:
The arrow operator (->) is a dereference operator that is used exclusively with pointers to
objects that have members. This operator serves to access the member of an object directly
from its address. For example, in the example above:
pmovie->title
(*pmovie).title
Both expressions, pmovie->title and (*pmovie).title are valid, and both access the
member title of the data structure pointed by a pointer called pmovie. It is definitely something
different than:
*pmovie.title
*(pmovie.title)
205
This would access the value pointed by a hypothetical pointer member called title of the
structure object pmovie (which is not the case, since title is not a pointer type). The following
panel summarizes possible combinations of the operators for pointers and for structure
members:
A pointer variable can be created not only for native types like (int, float, double etc.) but they
can also be created for user defined types like structure. If you do not know what pointers are,
visit C++ pointers
206
Week 13:- Practice
Here is an example which shows how you can create pointer for structures:
#include <iostream>
struct temp {
int i;
float f;
};
int main() {
temp *ptr;
return 0;
#include <iostream>
struct Distance {
int feet;
float inch;
};
int main() {
Distance *ptr, d;
ptr = &d;
207
cin >> (*ptr).feet;
cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << " inches";
return 0;
Output
Enter feet: 4
Displaying information.
In this program, a pointer variable ptr and normal variable d of type structure Distance is
defined.
The address of variable d is stored to pointer variable, that is, ptr is pointing to variable d. Then,
the member function of variable d is accessed using pointer.
Notes:
• Since pointer ptr is pointing to variable d in this program, (*ptr).inch and d.inch are equivalent.
Similarly, (*ptr).feet and d.feet are equivalent.
• However, if we are using pointers, it is far more preferable to access struct members using
the -> operator, since the . operator has a higher precedence than the * operator.
Hence, we enclose *ptr in brackets when using (*ptr).inch. Because of this, it is easier to make
mistakes if both operators are used together in a single code.
208
int *ptr;
int arr[5];
ptr = arr;
Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of
the first element of the array in variable ptr.
Notice that we have used arr instead of &arr[0]. This is because both are the same. So, the code
below is the same as the code above.
int *ptr;
int arr[5];
ptr = &arr[0];
The addresses for the rest of the array elements are given by &arr[1], &arr[2], &arr[3], and
&arr[4].
Suppose we need to point to the fourth element of the array using the same pointer ptr.
Here, if ptr points to the first element in the above example then ptr + 3 will point to the fourth
element. For example,
int *ptr;
int arr[5];
ptr = arr;
209
Similarly, we can access the elements using the single pointer. For example,
*ptr == arr[0];
Note: The address between ptr and ptr + 1 differs by 4 bytes. It is because ptr is a pointer to an
int data. And, the size of int is 4 bytes in a 64-bit operating system.
Similarly, if pointer ptr is pointing to char type data, then the address between ptr and ptr + 1
is 1 byte. It is because the size of a character is 1 byte.
210
Example 1: C++ Pointers and Arrays
Output
&arr[0] = 0x61fef0
&arr[1] = 0x61fef4
&arr[2] = 0x61fef8
ptr + 0 = 0x61fef0
ptr + 1 = 0x61fef4
ptr + 2 = 0x61fef8
211
In the above program, we first simply printed the addresses of the array elements without using
the pointer variable ptr. Then, we used the pointer ptr to point to the address of a[0], ptr + 1 to
point to the address of a[1], and so on.
In most contexts, array names decay to pointers. In simple words, array names are converted
to pointers. That's the reason why we can use pointers to access elements of arrays. However,
we should remember that pointers and arrays are not the same.
Output
Notice that we haven't declared a separate pointer variable, but rather we are using the array
name arr for the pointer notation.
As we already know, the array name arr points to the first element of the array. So, we can
think of arr as acting like a pointer.
Similarly, we then used for loop to display the values of arr using pointer notation.
213
Assesement
Practical
1. Write a C++ program which shows list of books which have title, Author and price
using structure data type and the total number of books to be displayed should be
entered from user.
2. Write a C++ program which stores the information (name, Id and marks of three
subjects entered by the user) of a student in a structure and displays name, Id and
average of marks entered for one student. The marks entered should be between 1 and
100.
3. Declare a single structure data type suitable for an employee record of the type
illustrated below:
214
Week 14:
When an element of a structure is passed to a function, you are actually passing the values of
that element to the function. Therefore, it is just like passing a simple variable (unless, of course,
that element is complex such as an array of character). For example, consider the following
structure:
struct date
short day ;
short month ;
short year ;
}Bdate ;
The above function-call invokes a function, func1() by passing values of individual structure
elements of structure Bdate.
The function can either receive the values by creating its own copy for them (call by value) or
by creating references for the original variables (call by reference). If You want that the values
of the structure elements should not be altered by the function, then you should pass the
structure elements by value and if you want the function to alter the original values, then you
should pass the structure elements by reference.
But remember if one of the structure elements happens to be an array, it will automatically be
passed by reference as the arrays cannot be passed by value.
215
Passing Entire Structure to Function
Passing entire structures makes the most sense when the structure is relatively compact. The
entire structure can be passed to the functions both ways by value and by reference. Passing by
value is useful when the original values are not to be changed and passing by reference is useful
when original values are to be changed.
When a structure is used as an argument to a function, the entire structure is passed using the
standard call-by-value method. Of course, this means that any changes made to the contents of
the structure inside the function to which it is passed do not affect the structure used as an
argument.
The receiving parameter for the passed structure must match the type of the passed structure.
Example
Consider the following example program, demonstrating how to pass structure to function with
call by value method:
getch();
} // end of main()
A function prnsum() is invoked by passing two structures length1 and length2 by value and
which calculates the sum of the two and prints it. The function prnsum() creates its own copies
for length1 and length2 namely l1 and l2 and works with it. Thus the original copies length1
and length2 remains untouched.
Structures can be passed by reference just as other simple types. When a structure is passed by
reference the called function declares a reference for the passed structure and refers to the
original structure elements through its reference. Thus, the called function works with the
original values.
Example
Following example program illustrates passing of structures to function with call by reference
method:
#include<iostream>
#include<conio.h>
struct distance
{
int feet;
217
int inches;
};
void prnsum(distance &l1, distance &l2); // watch the declaration
void main()
{
clrscr();
distance length1, length2; // two structures of type distance declared
// Read values for length1
cout<<"Enter Value for length 1:\n";
cout<<"Feet: ";
cin>>length1.feet;
cout<<"\nInches: ";
cin>>length1.inches;
getch();
}
The above program invokes prnsum() by passing structures length1 and length2 by reference.
The function prnsum() creates references l1 and l2 for structures length1 and length2 and thus,
uses the original structures length1 and length2 by names l1 and l2 respectively.
See, the return type of prnsum() is distance. For Program No. 1 i.e., when structures are passed
by value and as
For Program No. 2 i.e, when structures are being passed by reference. The definition of the
function prnsum() of program no. 1 will be as follows :
distance L3 ;
If the return value of the function prnsum() is being assigned to a variable, then that variable
must be a structure variable of type distance. For example, notice the following code fragment
distance total ;
The above code fragment stores the return value of function prnsum() into the structure total
which is also of type distance and then prints the values of structure elements. The function
prnsum() that returns a structure and gets invoked in call-by-reference.
A function may even return a reference to a structure also. For example, consider the following
function :
distance & sum (distance L1, distance L2) ; //notice the declaration
219
{
return L3 ;
The above function is returning reference to a structure L3. When a function returns a reference,
it returns the lvalue (location value) in place of rvalue (data value) of a variable.
220
Week 14:- Practice:
• A structure can be passed to any function from main() function or from any sub function.
• Structure definition will be available within the function only.
• It won't be available to other functions unless it is passed to those functions by value or
by address (reference).
Ways to pass structure
If the structure itself is an argument, then it is called "call by value". If the reference of the
structure is passed as an argument, then it is called "call by reference".
• Call by value
• Call by reference
• Call by value
When a structure is passed as argument to a function using call by value method, any change
made to the contents of the structure variable inside the function to which it is passed do not
affect the structure variable used as an argument.
#include <iostream>
using namespace std;
struct Employee
{
char name[50];
int age;
float salary;
};
void printData(Employee); // Function declaration
int main()
{
Employee p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
221
cout << "Enter salary: ";
cin >> p.salary;
// Function call with structure variable as an argument
printData(p);
return 0;
}
void printData(Employee p){
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Output
Enter age: 20
Displaying Information.
Age: 20
Salary: 20000
In this program, user is asked to enter the name, age and salary of a Employee inside main()
function.
printData(p);
The return type of printData() is void and a single argument of type structure Employee is
passed.
222
Example 2: C++ Returning structure from function
#include <iostream>
using namespace std;
struct Employee {
char name[50];
int age;
float salary;
};
Employee getData(Employee);
void printData(Employee);
int main(){
Employee p;
p = getData(p);
printData(p);
return 0;
}
Employee getData(Employee p) {
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
return p;
}
void printData(Employee p){
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Output
223
The output of this program is same as program above.
In this program, the structure variable p of type structure Employee is defined under main()
function.
The structure variable p is passed to getData() function which takes input from user which is
then returned to main function.
p = getData(p);
Note: The value of all members of a structure variable can be assigned to another structure
using assignment operator = if both structure variables are of same type. You don't need to
manually assign each members.
Then the structure variable p is passed to printData() function, which displays the information.
2. Call by reference
In this method of passing the structures to function, the address of a structure variable/object
is passed to the function using address of (&) operator. So any change made to the contents of
structure variable inside the function are reflected back to the calling function.
#include <iostream>
using namespace std;
struct Employee{
char name[50];
int age;
float salary;
};
void readData(Employee &); // Function declaration
void printData(Employee); // Function declaration
int main(){
Employee p;
readData(p);
printData(p);
return 0;
}
Displaying Information.
Name: Micheal Clark
Age: 20
Salary: 20000
The output of this program is same as program above.
Structures are usually passed by reference method bacause it saves the memory space and
executes faster.
A structure variable can be passed to a function in similar way as normal argument. Consider
this example:
#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary;
};
void displayData(Person); // Function declaration
int main() {
Person p;
cout << "Enter Full name: ";
cin.get(p.name, 50);
cout << "Enter age: ";
cin >> p.age;
cout << "Enter salary: ";
cin >> p.salary;
225
// Function call with structure variable as an argument
displayData(p);
return 0;
}
void displayData(Person p) {
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}
Output:
Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4
In this program, user is asked to enter the name, age and salary of a Person inside main()
function.
displayData(p);
The return type of displayData() is void and a single argument of type structure Person is passed.
#include <iostream>
using namespace std;
struct Person {
char name[50];
int age;
float salary;
};
Person getData(Person);
void displayData(Person);
226
int main()
{
Person p, temp;
temp = getData(p);
p = temp;
displayData(p);
return 0;
}
Person getData(Person p) {
In this program, we have created two structure variables p and temp of type Person under the
main() function.
The structure variable p is passed to getData() function which takes input from the user which
is then stored in the temp variable.
temp = getData(p);
p = temp;
Then the structure variable p is passed to displayData() function, which displays the
information.
Note: We don't really need to use the temp variable for most compilers and C++ versions.
Instead, we can simply use the following code:
p = getData(p);
227
Assesement
Practical
1. Write a functions InputData and DisplayData that reads and display the elements of a
record(structure). Use the student record defined below.
struct Student{
char name[20];
char sex;
int age;
};
2. Use the functions in problem number 1 to read and display a list of students record. Modify
the display function so that it displays the list in a tabular form. Add another function to
your program in problem number 2 which can display only the list of female or male
students depending on the user choice.
3. A student record consist of his id number, a list of marks, average mark, and his rank.
struct Student{
unsigned id;
float marks[5];
float average;
unsigned rank;
};
use the above structure to write a program that can read the inputs (id number and list
of marks) for the students in a class and calculate the average mark for each student.
Your program should finally give the rank for each student. Write a function that
displays the list of students in the order of their rank
4. Write a program which defines a new structure type to contain the following information
about towns: the name (a sequence of characters); the population (aninteger); and a Boolean
value (i.e. true or false) which indicates whether the town has an airport or not. Use the
typedef statement to define your own Boolean data type -remember that true in C++ is
equivalent to the integer value 1, and false is equivalent to 0.
5. Declare a single structure data type suitable for an employee record of the type illustrated
below:
Number Name Rate Hours
3462 JONES 4.62 40
6793 Robbins 5.83 38
7834 Swain 6.89 40
9002 Williams 4.75 42
Using the data type declared above, write a C++ program that interactively accepts the above
data into an array of six structures. Once the data have been entered, the program should
create a payroll report listing each employee’s name, number, and gross pay. Include the total
gross pay of all employeesat the end of the report.
228
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill,
2003
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com/
229
230
Chapter 6: File Management
Lesson Plan
1. Learning objectives
• Be able to define file and stream.
• Be able to declare file variable identifier.
• Be able to open file.
• Be able to process a file.
• Be able to define close a file.
2. Motivation
• Outputs through Question and Answer; repetitive discussion and using file
management in C++.
• Solving a problem using file processing in C++.
3. Expectations or Outcomes
Define the terminologies, explain (file variable identifier declaration, opening a file,
reading a file and writing on a file); the assessment standard attached at the end of the
job task sheet.
4. Equipment
• Desktop or Laptop computer.
• DEV C++ software
5. Practice contents/Activities/Safety
Active practice; on declaring of file variable identifier, creating and opening a file, w
riting on a file, reading from a file and closing a file.
Active practice on array of struts and structure of structure.
6. Assessments
• How to apply file management in C++ programming?
• How to create file, how to open a file, how to process (read and write) file using C++?
7. Clean-up
After finishing practice, computers should be properly shut down.
8. Independent practice/Follow-up activities
• Learning through assignment (practical Exercises)
9. Review/Reflection
• Review the outcome of the practice, improvement measure and previous reflected
opinions.
231
Week 15:
1. Streams
The C++ file system is designed to work with a wide variety of devices, including terminals,
disk drives, and tape drives. Even though each device is very different, the C++ file system
transforms each into a logical device called stream. There are two types of streams: text and
binary.
a. Text Streams
A text stream is a sequence of characters. In a text stream, certain character translations may
occur as required by the host environment. For example a new line may be converted to a
carriage return/linefeed pair. There may not be a one-to-one relationship between the characters
that are written (or read) and those on the external device. Because of possible transformations,
the number of characters written (or read) may not be the same as those on the external device.
b. Binary streams
A binary stream is a sequence of bytes with a one-to-one correspondence to those in the external
device i.e., no character translations occur. The number of bytes written (or read) is the same
as the number on the external device. However, an implementation-defined number of null
bytes may be appended to a binary stream. These null bytes might be used to pad the
information so that it fills a sector on a disk, for example.
232
2. Files
In C++, a file can be anything from a disk file to a terminal or printer. You associate a stream
with a specific file by performing an open operation. Once a file is open, information can be
exchanged between it and a program. All streams are the same but all files are not. If the file
can support position requests, opening that file also initializes the file position indicator to the
start of the file. As each character is read from or written to the file, the position indicator is
incremented. You disassociate a file from a specific stream with a close operation. If you close
a file opened for output, then contents, if any, of its associated stream are written to the external
device. -- this process is referred to as flushing the stream. All files are closed automatically
when the program terminates normally. Files are not closed when a program terminates
abnormally. Each stream that is associated with a file has a file control structure of type FILE.
This structure FILE is defined in the header stdio.h.
A file pointer is a pointer to information that defines various things about the file, including its
name, status, and the current t position of the file. In essence, the file pointer identifies a specific
disk file and is used by the associated stream to direct the operation of the I/O functions. A file
pointer is a pointer variable of type FILE.
FILE * fp;
Three file I/O classes are used for File Read/Write operations:
233
a. ifstream - Can be used for File read/input operations
b. ofstream - Can be used for File write/output operations
c. fstream - Can be used for both read/write c++ file I/O operations
These classes are derived directly or indirectly from the classes istream, and ostream. We have
already used objects whose types were these classes: cin is an object of class istream and cout
is an object of class ostream. Therefore, we have already been using classes that are related to
our file streams. And in fact, we can use our file streams the same way we are already used to
use cin and cout, with the only difference that we have to associate these streams with physical
files. Let's see an example:
For example, the value int count 321 will be stored in three byte if it is written in text file
considering the digit sequence ‘3’, ‘2’, ‘1’. It will be stored in two byte if it is written in binary
file since int requires two byte to store any of its value. When you open the binary file you will
see the character equivalence of the two bytes.
The first byte holds the character with ASCII value equals to one and the second byte a
character with ASCII value equals 65 which is ‘A’. Then if you open the binary file you will
see these characters in place of 321.
The method open ( ) is a member of each of the three stream classes. Its prototype is:
void open (const char *filename, int mode, int access = filebuf::penprot );
Where:
Filename is the name of the file.
The value of the mode determines how the file is opened. It must be one (or more) of these
values:
Mode Description
ios::ate Open a file for output and move to the end of the file (normally used to
append data to a file). Data can be written anywhere in the file.
235
Mode Description
You can combine two or more of these values by using them together.
ofstream out ;
out.open ( "test", ios::out); // correct statement
ofstream out1 ;
out.open ( " test"); // the default value of mode is ios::out –
// correct statment
To open a stream for input and output, you must specify both the ios::in and the ios::out mode
values. (Noe default value for mode is supplied in this case.)
fstream myStream;
myStream.open ( "test", ios::in | ios::out );
if (myStream){
Each one of the open() member functions of the classes ofstream, ifstream and fstream has a
default mode that is used if the file is opened without a second argument:
ofstream ios::out
ifstream ios::in
236
For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively
assumed, even if a mode that does not include them is passed as second argument to the open()
member function. The default value is only applied if the function is called without specifying
any value for the mode parameter. If the function is called with any value in that parameter the
default mode is overridden, not combined.
File streams opened in binary mode perform input and output operations independently of any
format considerations. Non-binary files are known as text files, and some translations may
occur due to formatting of some special characters (like newline and carriage return characters).
Since the first task that is performed on a file stream object is generally to open a file, these
three classes include a constructor that automatically calls the open() member function and has
the exact same parameters as this member. Therefore, we could also have declared the previous
myfile object and conducted the same opening operation in our previous example by writing:
Combining object construction and stream opening in a single statement. Both forms to open
a file are valid and equivalent.
To check if a file stream was successful opening a file, you can do it by calling to member
is_open() with no arguments. This member function returns a bool value of true in the case that
indeed the stream object is associated with an open file, or false otherwise:
When we are finished with our input and output operations on a file we shall close it so that its
resources become available again. In order to do that we have to call the stream's member
function close(). This member function takes no parameters, and what it does is to flush the
associated buffers and close the file:
myfile.close();
Once this member function is called, the stream object can be used to open another file, and
the file is available again to be opened by other processes.
237
In case that an object is destructed while still associated with an open file, the destructor
automatically calls the member function close(). The close method takes no parameters and
returns no value.
- Another event happens when you read files. Eventually, you read all the data. Subsequently
reading produces error because there is no more data to read. C++ provides a solution to the
end-of-file occurrence.
- If you attempt to read a file that you have completely read the data from, C++ returns the
value zero. To find the end-of-file condition, be sure to check for zero when reading
information from files.
The following code asks the user for a file name and displays the content of the file to the screen.
out.close ( );
#include <iostream>
#include <fstream>
int main () {
ofstream myfile;
238
myfile.open ("example.txt");
myfile.close();
return 0;
This code creates a file called example.txt and inserts a sentence into it in the same way we are
used to do with cout, but using the file stream myfile instead.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
239
}
else cout << "Unable to open file";
return 0;
}
This last example reads a text file and prints out its content on the screen. Notice how we have
used a new member function, called eof() that returns true in the case that the end of the file
has been reached. We have created a while loop that finishes when indeed myfile.eof() becomes
true (i.e., the end of the file has been reached).
1. seekg()
2. seekp()
3. tellg()
4. tellp()
seekg(): this moves get pointer i.e input pointer to a specified location.
For eg. infile.seekg(5); move the file pointer to the byte number 5 from starting point.
seekp(): this move put pointer (output pointer) to a specified location for example:
outfile.seekp(5);
tellg(): this gives the current position of get pointer (input pointer)
tellp(): this gives the current position of put pointer (output pointer) eg. ofstream fileout;
Function Description
bad() Returns true if a reading or writing operation fails. For example in the
case that we try to write to a file that is not open for writing or if the
device where we try to write has no space left.
fail() Returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is extracted
when we are trying to read an integer number.
eof() Returns true if a file open for reading has reached the end.
good() It is the most generic state flag: it returns false in the same cases in
which calling any of the previous functions would return true.
In order to reset the state flags checked by any of these member functions we have just seen
we can use the member function clear(), which takes no parameters.
All I/O streams objects have, at least, one internal stream pointer:
• ifstream, like istream, has a pointer known as the get pointer that points to the element
to be read in the next input operation.
• ofstream, like ostream, has a pointer known as the put pointer that points to the location
where the next element has to be written.
• Finally, fstream, inherits both, the get and the put pointers, from iostream (which is
itself derived from both istream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream can
be manipulated using the following member functions:
241
tellg() and tellp()
These two member functions have no parameters and return a value of the member type
pos_type, which is an integer data type representing the current position of the get stream
pointer (in the case of tellg) or the put stream pointer (in the case of tellp).
These functions allow us to change the position of the get and put stream pointers. Both
functions are overloaded with two different prototypes. The first prototype is:
seekg ( position );
seekp ( position );
Using this prototype the stream pointer is changed to the absolute position position (counting
from the beginning of the file). The type for this parameter is the same as the one returned by
functions tellg and tellp: the member type pos_type, which is an integer value.
Using this prototype, the position of the get or put pointer is set to an offset value relative to
some specific point determined by the parameter direction. offset is of the member type
off_type, which is also an integer type. And direction is of type seekdir, which is an enumerated
type (enum) that determines the point from where offset is counted from, and that can take any
of the following values:
ios::cur offset counted from the current position of the stream pointer
242
The following example uses the member functions we have just seen to obtain the size of a file:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
long begin,end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << " bytes.\n";
return 0;
}
File streams include two member functions specifically designed to input and output binary
data sequentially: write and read. The first one (write) is a member function of ostream
inherited by ofstream. And read is a member function of istream that is inherited by ifstream.
Objects of class fstream have both members. Their prototypes are:
Where memory_block is of type "pointer to char" (char*), and represents the address of an
array of bytes where the read data elements are stored or from where the data elements to be
243
written are taken. The size parameter is an integer value that specifies the number of characters
to be read or written from/to the memory block.
There are two ways to write and read binary data to and from a file.
char in;
ifstream in ( "test", ios::in | ios::binary);
if (!in){
cout <<"Cannot open file";
return 1;
}
while (in) //inn will be 0 when eof is reached
{ in.get ( ch );
cout << ch;
}
When the end-of-file is reached, the stream associated with the file becomes zero.
ofstream out ( "chars", io::out | ios::binary);
for (int i= 0; i < 256; i++)
out.put ( (char ) i ) ; //write all characters to disk
out.close ( );
244
read ( ) and write ( )
The read ( ) method reads num bytes from the associated stream, and puts them in a memory
buffer (pointed to by buf).
int gcount ( );
This get ( ) method reads characters into the array pointed to by the buf until either num
characters have been read, or the character specified by delim has been encountered. The array
pointed to by buf will be null terminated by get ( ). If the delimiter character is encountered in
the input stream, it is not extracted. Instead, it remains in the stream until the next input
operation.
a. int get ( )
It returns the next character from the stream. It returns EOF if the end of file is encountered.
b. getline ( )
istream & getline ( char *buf, int num, char delim ='\n');
This method is virtually identical to the get ( buf, num, delim) version of get ( ). The difference
is getline ( ) reads and removes the delimiter from the input stream.
#include <iostream>
245
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
In this example the entire file is read and stored in a memory block. Let's examine how this is
done:
First, the file is open with the ios::ate flag, which means that the get pointer will be positioned
at the end of the file. This way, when we call to member tellg(), we will directly obtain the size
of the file. Notice the type we have used to declare variable size:
ifstream::pos_type size;
ifstream::pos_type is a specific type used for buffer and file positioning and is the type returned
by file.tellg(). This type is defined as an integer type, therefore we can conduct on it the same
operations we conduct on any other integer value, and can safely be converted to another
integer type large enough to contain the size of the file. For a file with a size under 2GB we
could use int:
int size;
size = (int) file.tellg();
Once we have obtained the size of the file, we request the allocation of a memory block large
enough to hold the entire file:
246
Right after that, we proceed to set the get pointer at the beginning of the file (remember that
we opened the file with this pointer at the end), then read the entire file, and finally close it:
At this point we could operate with the data obtained from the file. Our program simply
announces that the content of the file is in memory and then terminates.
Function Description
Detecting EOF It returns nonzero when the end of the file has been
reached; otherwise it returns zero.
int eof ( );
Reading and discarding characters Reads and discards characters until either num characters
from the input stream. have been nignored (1 by default ) or until the charcter
specified by delim is encounterdd (EOF by default). If
istream & ignore (int num = 1, int
the delimiting character is encountered, it is not removed
delim = EOF);
from the input stream.
Obtain the next character in the One can obtain the next character in the input stream
input stream without removing it without removing it from that stream by using peek ( ). It
from that stream returns the next character in the stream or EOF if the end
of file is encountered.
int peek ( );
247
istream & putback ( char c); One can return the last character read from a stream to
that stream using putback ( ).
Forcing data to be physically When the output is performed, data is not necessarily
written to the disk immediately written to the physical device linked to the
stream. Instead, information is stored in an internal
buffer until the buffer is full. Only then are the contents
ostream & flush ( ); of that buffer written to disk. However, you can force the
information to be physically written to the disk before the
buffer is full by calling flush ( ).
ios::beg
ios::cur
ios::end
The C++ I/O system manages two pointers associated with a file. One is the get pointer, which
specifies where in then file the next input operation will occur. The other is the put pointer,
which specifies where in the file the next output operation will occur. Each time an input or
output operation takes place the appropriate pointer is automatically sequentially advanced.
The seekg ( ) method moves the associated file's current get pointer offset number of bytes
from the specified origin, which must be one of three values. The seekp ( ) method moves the
associated file's current put pointer offset number of bytes from the specified origin, which
must be one of three values.
streampos tellg ( );
248
streampows tellp ( );
Here, streampos is a type defined in iostream.h that is capable of holding the largest value that
either function can return.
I/O Status
The C++ I/O system maintains status information about the outcome of each I/O operation.
The current state of the I/O system is held in an integer, in which the following flags are
encoded.
These flags are enumerated inside ios. Also defined in ios is goodbit, which has the value 0.
There are two ways in which you can obtain I/O status information.
int rdstate ( );
rdstate function returns the current status of the error flags encoded into an integer. It returns
zero, when no error has occurred. Otherwise, an error bit is turned on.
Method Description
int bad ( ) Returns true if badbit is set.
int fail ( ) Returns true if failbit is set.
int eof ( ) Returns true if there are no errors.
int good ( ) Otherwise they return false.
Once an error has occurred, it may need to be cleared before your program continues. to do
this, use the clear ( ) method.
249
Buffers and Synchronization
When we operate with file streams, these are associated to an internal buffer of type streambuf.
This buffer is a memory block that acts as an intermediary between the stream and the physical
file. For example, with an ofstream, each time the member function put (which writes a single
character) is called, the character is not written directly to the physical file with which the
stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer.
When the buffer is flushed, all the data contained in it is written to the physical medium (if it
is an output stream) or simply freed (if it is an input stream). This process is called
synchronization and takes place under any of the following circumstances:
• When the file is closed: before closing a file all buffers that have not yet been
flushed are synchronized and all pending data is written or read to the physical
medium.
• When the buffer is full: Buffers have a certain size. When the buffer is full it is
automatically synchronized.
250
Week 15 :- Practice:
Consider the file Header .h which contains the basic preprocessing include files, structure
definition and functions other than the main function.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
struct studList
{
char firstName[12];
char lastName[12];
int age;
char gender;
char Id[12];
};
studList getStudent()
{
studList std;
cout<<"Enter student first name ===>";cin>>std.firstName;
cout<<"Enter student last name ===>";cin>>std.lastName;
cout<<"Enter student age ===>";cin>>std.age;
cout<<"Enter student gender ===>";cin>>std.gender;
cout<<"Enter student Id ===>";cin>>std.Id;
return std;
}
void DisplayStudent(studList std)
{
cout<<"Student first name:\t"<<std.firstName<<endl;
cout<<"Student last name :\t"<<std.lastName<<endl;
cout<<"Student age:\t\t"<<std.age<<endl;
cout<<"Student gender:\t\t"<<std.gender<<endl;
cout<<"Student Id:\t\t"<<std.Id<<endl;
}
Solution 1
Solution 2
Solution 3
Solution 4
253
Assesement
I. Practical
2. Input String and write in a file. Hint: use put() function inside a loop.
12. Read and Write records from a file using switch case. Assessment
13. Write a program to create a file in “D:\\ Computer.txt” and write three lines of computer
definition.
14. Write a program that reads computer definition from the text file “D:\\ Computer.txt”
and display on the screen.
15. Write a program that accept N student record from the keyboard & store the list on a
file “D:\\ Test.txt” in a text file format
16. Write a program that reads students record from the text file “D:\\ Test.txt” and display
on the screen.
17. Note Student record consists of first name, last name, gender, age and Id.
254
Reference
[1]. Stephen R. Davis, C++ Weekend Crash, John Wiley & Sons, 2003
[2]. Herbert Schildt, C++: A Beginner's Guide, Second Edition 2nd Edition, McGraw Hill,
2003
[3]. P B Mahapatra, Programming in C++, S Chand, 2008
[4]. Ashok Kamthane, Programming in C++, Pearson, 2013
[5]. https://www.w3schools.com/cpp/
[6]. https://www.tutorialspoint.com/cplusplus/index.htm
[7]. https://www.learncpp.com/
255
This textbook was developed by TVTI faculties through
KOICA project “Capacity Development for TVET Leaders
and Trainers in Ethiopia”