0% found this document useful (0 votes)
10 views44 pages

Computer Science (1) Lab (C++)

C++ is a general-purpose programming language that enhances C by adding object-oriented features. The document covers the structure of a simple C++ program, variable types, declaration and assignment, comments, arithmetic expressions, input/output operations, and conditional statements. It also includes examples and explanations of various programming concepts and functions available in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views44 pages

Computer Science (1) Lab (C++)

C++ is a general-purpose programming language that enhances C by adding object-oriented features. The document covers the structure of a simple C++ program, variable types, declaration and assignment, comments, arithmetic expressions, input/output operations, and conditional statements. It also includes examples and explanations of various programming concepts and functions available in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Introduction

C++ is a general-purpose programming language that was developed as an


enhancement of the C language to include object-oriented paradigm. It is an
imperative and a compiled language.

General Structure of a Simple C++ Program

You can type the text as shown in the above figure into an editor and save it
to a file named simple.cpp. The extension .cpp is a common extension used
for C++ source code.
After creating this file with a text editor and compiling it, you can run the
program. The program prints the message:

1
Lab (1)
Variables and Expressions
 Variable are used in C++, where we need storage for any value, which will
change in program.
 Variable is the name of memory location allocated by the compiler
depending upon the datatype of the variable.

Basic types of Variables:

Each variable while declaration must be given a datatype, on which the


memory assigned to the variable depends. Following are some basic types of
variables:

bool For variable to store boolean values ( True or False )

char For variables to store character types.

int for variable with integral values.

float and double are also types for variables with large and floating point values

2
Declaration and Assignment:

 Variable must be declared before they are used. A declaration specifies


the type of a variable. For example:

 An assignment statement associates a value with a variable. The key to an


assignment statement is the symbol = which is known as the assignment
operator.

 Declaration and assignment can be done in one single step. This combined
declaration and immediate assignment is called initialization.
 If a variable is once declared and if try to declare it again, we will get a
compile time error.

 C++ has strict rules for variable names:


 must contain at least one character.
 The first character must be an alphabetic letter (upper or lower case)
or the underscore.
 The remaining characters (if any) may be alphabetic characters
(upper or lower case), the underscore, or a digit.
 No other characters (including spaces) are permitted.
 A reserved word cannot be used as a variable name.

Comments:
 Good programmers annotate their code by inserting remarks that explain
the purpose of a section of code or why they chose to write a section of
code the way they did. These notes are meant for human readers, not the

3
compiler.
 Any text contained within comments is ignored by the compiler. C++
supports two types of comments:
 Single line comment: The comment begins with the double forward
slash symbols (//) and continues until the end of that line. The
compiler will ignore the // symbols and the contents of the rest of
the line.
 Block comment: the second type of comment begins with the
symbols /* and is in effect until the */ symbols are encountered. The
/*. . . */ symbols delimit the comment like parentheses delimit a
parenthetical expression. Unlike parentheses, however, these block
comments cannot be nested within other block comments.
Arithmetic Expressions:
 All expressions have a value. The process of determining the
expression’s value is called evaluation.
 Arithmetic expressions used in C++ in order to perform
calculation. In general, an arithmetic expression may consist
of any arithmetic combination of constants and variables.
 When we must perform mixed arithmetic—such as adding an
int to a double—the compiler automatically produces
machine language code that copies the int value to temporary
memory location and transforms it into its double equivalent.
It then performs double-precision floating-point arithmetic to
compute the result.
 When different operators are used in the same expression, the
normal rules of arithmetic apply. All C++ operators have a
precedence and associativity:
• Precedence: when an expression contains two different
kinds of operators, which should be applied first?
• Associativity: when an expression contains two
operators with the same precedence, which should be

4
applied first?
 Next table display arithmetic operations and its precedence
and associativity.
Precedence Operation Meaning Associativity
* Multiplication
First / Division
Left
% Modulus
+ Addition
Second
- Subtraction
Last = Assignment Right

Example (1): Given the following declaration:

Evaluate the following C++ expressions.

(a) i1 + (i2 * i3)

(b) i1 * (i2 + i3)

(c) i1 / (i2 + i3)

(d) i1 / i2 + i3

(e) 3 + 4 + 5 / 3

(f) (3 + 4 + 5) / 3

(g) d1 + (d2 * d3)

5
(h) d1 + d2 * d3

(i) d1 / d2 - d3

(j) d1 / (d2 - d3)

(k) d1 + d2 + d3 / 3

(l) (d1 + d2 + d3) / 3

(m) d1 + d2 + (d3 / 3)

(n) 3 * (d1 + d2) * (d1 - d3)

(o) d3 + i1 / i2 - d1 * d2 + i3 % 2

(p) d2 * i1 / d1 - i2 % i3 % i1 + d2

(q) (d2 + d1 * (i2 / i3 - i1 % 2)) + d3

(r) (d1 * (i2 % i1 - i3)) / d3

(s) ((i1 + i2) / 3 * i3) % 4 / 2

6
Lab (2)
Sequential Statements
• A program performs three basic operations: it gets data, it manipulates the
data, and it outputs the results.
• In C++, Input/Output is a sequence of bytes, called a stream, from the
source to the destination.
• A stream is a sequence of characters from the source to the destination.
There are two types of streams:
 Input stream: A sequence of characters from an input device to the
computer.
 Output stream: A sequence of characters from the computer to an
output device.

Standard output (cout)

• On most program environments, the standard output by default is the


screen, and the C++ stream object defined to access it is cout.
• For formatted output operations, cout is used together with the insertion
operator, which is written as << (i.e., two "less than" signs).

• When the text is enclosed between double quotes (" "), the text is printed
literally; when they are not, the text is interpreted as the identifier of a
variable, and its value is printed instead. For example, these two sentences
have very different results:

7
• Multiple insertion operations (<<) may be chained in a single statement.
Chaining insertions is especially useful to mix literals and variables in a
single statement:

The output of the previous statement would be: I am 24 years old.


• What cout does not do automatically is add line breaks at the end, unless
instructed to do so. To insert a line break, a new-line character shall be
inserted at the exact position the line should be broken. In C++, a new-line
character can be specified as \n (i.e., a backslash character followed by a
lowercase n). For example:

This produces the following output:


First sentence.
Second sentence.
Third sentence.
• Alternatively, the endl manipulator can also be used to break lines. For
example:

This would print:


First sentence
Second sentence.

8
Standard input (cin)
• In most program environments, the standard input by default is the
keyboard, and the C++ stream object defined to access it is cin.
• For formatted input operations, cin is used together with the extraction
operator, which is written as >> (i.e., two "greater than" signs). This
operator is then followed by the variable where the extracted data is stored.
For example:

• Extractions on cin can also be chained to request more than one datum in
a single statement:

This is equivalent to:

In both cases, the user is expected to introduce two values, one for variable a,
and another for variable b. Any kind of space is used to separate two
consecutive input operations; this may either be a space, a tab, or a new-line
character.
• cin extraction always considers spaces (whitespaces, tabs, new-line...) as
terminating the value being extracted, and thus extracting a string means
to always extract a single word, not a phrase or an entire sentence.
• To get an entire line from cin, there exists a function, called getline, that
takes the stream (cin) as first argument, and the string variable as second.
For example:

9
Using Mathematical functions:

 In mathematics, a function computes a result from a given value, a function


in C++ works like a mathematical function.
 In C++, a function is a named sequence of code that performs a specific
task.
 The cmath library provides much of the functionality of a scientific
calculator.
 A C++ program that uses any of the functions from the cmath library must
use the following preprocessor #include directive:

 Following table list only a few of the available function in cmath library.

function Description of function


double pow(double x, double y) Raises one number to a power of
another: pow(x,y)= 𝒙𝒚
double sqrt(double x) Computes the square root of a
number: sqrt(x)= √𝒙
double exp(double x) Computes e raised a power: exp(x)=
𝒆𝒙
double log(double x) Computes the natural logarithm of a
number: log(x)= 𝐥𝐨𝐠 𝒆 𝒙 = 𝐥𝐧 𝒙
double fabs(double x) Computes the absolute value of a
number: fabs(x)= |𝒙|
double cos(double) Computes the cosine of a value
specified in radians: cos(x)= 𝒄𝒐𝒔 𝒙;
other trigonometric functions include
sine, tangent, arc cosine, arc sine, arc
tangent, hyperbolic cosine,
hyperbolic sine, and hyperbolic
tangent.

10
Example (1): Write a program to compute the following functions: 𝐴 = 𝑥 2 +
𝑦 2 , 𝐵 = 𝐴2 + 𝑥𝑦, 𝐶(𝐴 + 𝐵)2 if 𝑥 = 5 and 𝑦 = 7
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………..

Example (2): Write a program to compute the area and circumference of


triangle it sides: 𝑥 = 4, 𝑦 = 3, 𝑧 = 5.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (3): Write a program to find horizontal component and vertical

component for force = 70N slop 45 degree from x-axis.

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

11
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (4): Write a program to read 4 variables from user, then compute
arithmetic mean and geometric mean.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (5): Write a program to read student name and 6 degree then
compute and print the average of student degree (using your name and
degree).
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………….

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

12
Example (6): Write a program to compute the area and circumference of
circle its radius R.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (7): Write a program to input the length, width, and high of wall
then compute the number of bricks in wall, if you know the brick sides is
(24 × 8 × 12𝑐𝑚)
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

13
Lab (3)
Conditional Statements
 Arithmetic expressions evaluate to numeric values; a Boolean expression,
evaluates to true or false.
 The simplest kinds of Boolean expressions use relational operators to
compare two expressions. Following table lists the relational operators
available in C++

Precedence Relational Meaning Associativity


Operations
< Less than
First > Greater than
<= Less than or equal to Left
>= Greater than or equal to
== Equal to
Second
!= Not equal to

 All relational operators have a lower precedence than any of the arithmetic.
 Logical (Boolean) operators enable you to combine logical expressions.
C++ has three logical (Boolean) operators, as shown in the following table:

Precedence Logical Operations Meaning Associativity


First ! Not Right
Second && And
Left
Third || Or

 Both && and || are binary operators; that is, they require two operands,
both of which must be Boolean expressions. Logical not (!) is a unary
operator it requires a single Boolean operand immediately to its right.
 Operator (!) has higher precedence than any arithmetic and relation

14
operators.
 Both && and || have lower precedence than any arithmetic and relation
operators.
 In conditional statement (or selection), the program executes particular
statements depending on some condition(s).
 In C++, there are three selections, or conditional statements:
 if statements.
 switch statement.
 Conditional operator ( ? : ).
 The if keyword is used to execute a statement or block, if, and only if, a
condition is fulfilled. In C++, there are three types of if statements:

One-way if statement
 The if keyword is used to execute a statement or block, if, and only if, a
condition is fulfilled. Its syntax is:

 Here, condition is the expression that is being evaluated. If this condition


is true, statement is executed. If it is false, statement is not executed (it is
simply ignored), and the program continues right after the entire selection
statement.

15
 If you want to include more than a single statement to be executed when
the condition is fulfilled, these statements shall be enclosed in braces { }
forming a block.

Two-way if statement
 Selection statements with if can also specify what happens when the
condition is not fulfilled, by using the else keyword to introduce an
alternative statement. Its syntax is:

Where statement1 is executed in case condition is true, and in case it is not,


statement2 is executed.

 Again, it would have also been possible to execute more than a single

16
statement per case by grouping them into blocks enclosed in braces: { }

Multi-way if statement
 Using if/else statements, you can even pick between more than two
possibilities. Just add else if for each possibility after the first if, and else
before the last possibility. Its syntax is:

Example (1): Write a program to compute Y value where:


𝐴 + 𝑏, 𝑖𝑓 𝐴 < 𝐵
𝑌={
𝐴 × 𝐵, 𝑖𝑓 𝐴 > 𝐵
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

17
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (2): Write a program to input any integer number then determine if
the number is odd or even.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (3): Write a program to compute the real roots of an equation 𝐴𝑥 2 +


𝐵𝑥 + 𝐶 = 0
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

18
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (4): Write a program to input 3 numbers then find the maximum
one.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (5): Write a program to compute the Y value where:


𝑋 2 + 𝑋 − 3, 𝑖𝑓 𝐴 = 1 𝑜𝑟 𝐵 = 3
𝑌 = {𝑋 2 + 3𝑋 + 5, 𝑖𝑓 𝐴 > 2 𝑎𝑛𝑑 𝐵 > 4
𝑋 3 + 2𝑋 2 + 𝑥, 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

19
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (6): Write a program that estimates the grade of the student based
on his input degree.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

20
Lab (4)
Conditional Statements (2)
Switch statement
 The switch statement provides a convenient alternative for some multi-
way if/else statements. Its syntax is:

 In a switch statement:
 • The reserved word switch identifies a switch statement.
 • The required parenthesized expression that follows the word
switch must evaluate to an integral value. Any integer type,
characters, and Boolean expressions are acceptable. Floating point
expressions and other non-integer types are forbidden.
 The body of the switch is enclosed by required curly braces.
 Each occurrence of the word case is followed by value and a colon
(:), This value is an integral constant and we call it a case label. This
label can be either a literal value or a const symbolic value. In
particular, non-const variables and other expressions are expressly
forbidden.
 If the case label matches the switch’s expression, then the
statements that follow that label are executed up until the break

21
statement is encountered. The statements and break statement that
follow each case label are optional.
 The default label is matched if none of the case labels match. It
serves as a catch all option like the final else in a multi-way if/else
statement. The default label is optional. If it is missing and none of
the case labels match the expression, then no statement within the
switch’s body is executed.
 The switch statement has two restrictions that make it less general than the
multi-way if/else:
 • The switch argument must be an integral expression.
 • Case labels must be constant integral values. Integral literals and
constants are acceptable. Variables or expressions are not allowed.
Example (1): Write a program that accept two integer numbers from the user
and one operation (+ for summation, - for subtraction, * for multiplication, /
for division) then perform this operation.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

22
Example (2): Write a program to find 𝑍 value where:
2 𝑘=1
2𝑘 3 − 3 𝑘=2
𝑍={
−2 𝑘=3
𝑘 𝑘 < 1 𝑜𝑟 𝑘 > 3
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (2): Write a program to compute 𝑄 value where:


|𝑥 2 + 1|, 𝑖𝑓 𝑘 = 1
𝑄 = { √|𝑥 2 + 𝑦|, 𝑖𝑓 𝑘 = 2
ln(𝑦 2 + 3𝑥) , 𝑖𝑓 𝑘 = 3
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

23
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

The Conditional Operator


 As purely a syntactical convenience, C++ provides an alternative to the
if/else construct called the conditional operator. It has limited application
but is convenient nonetheless.
 The general form of a conditional expression is:

 Where:
 condition is a normal Boolean expression that might appear in an if
statement. Parentheses around the condition are not required but
should be used to improve the readability.
 • expression1 is the value of the conditional expression if condition
is true.
 • expression 2 is the overall value of the conditional expression if
condition is false.
 The conditional expression can be used anywhere an expression can be
used. It is not a statement itself; it is used within a statement.
Example (4): Write a program to input any integer number then determine
absolute value of this number using conditional operator.
……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

……………………………………………………………………………………………………………………………………………………………

24
Lab (5)
Loop Statements
 Loops repeat a statement a certain number of times, or while a condition is
fulfilled. They are introduced by the keywords while, do, and for.
The while loop
 The simplest kind of loop is the while-loop. Its syntax is:

 The while-loop simply repeats statement while condition is true. If, after
any execution of statement, condition is no longer true, the loop ends, and
the program continues right after the loop

 If you want to include more than a single statement to be repeated executed


when the condition is fulfilled, these statements shall be enclosed in braces
{ } forming a block.

25
The do-while loop
 A very similar loop is the do-while loop, whose syntax is:

 It behaves like a while-loop, except that condition is evaluated after the


execution of statement instead of before, guaranteeing at least one
execution of statement, even if condition is never fulfilled.

 The do-while loop is usually preferred over a while-loop when


the statement needs to be executed at least once, such as when the condition
that is checked to end of the loop is determined within the loop statement
itself.

The for loop


 The for loop is designed to iterate a number of times. Its syntax is:

26
 Like the while-loop, this loop repeats statement while condition is true.
But, in addition, the for loop provides specific locations to contain
an initialization and an update expression, executed before the loop begins
the first time, and after each iteration, respectively. Therefore, it is
especially useful to use counter variables as condition.
 It works in the following way:

1. initialization is executed. Generally, this declares a counter variable,


and sets it to some initial value. This is executed a single time, at the
beginning of the loop.
2. condition is checked. If it is true, the loop continues; otherwise, the
loop ends, and statement is skipped, going directly to step 5.
3. statement is executed. As usual, it can be either a single statement or a
block enclosed in curly braces { }.
4. update is executed, and the loop gets back to step 2.
5. the loop ends: execution continues by the next statement after it.

27
 The three fields in a for-loop are optional. They can be left empty, but in
all cases, the semicolon signs between them are required. A loop with
no condition is equivalent to a loop with true as condition (i.e., an infinite
loop).

Example (1): Write a program to print the summation and multiplication of


the numbers from 1 to 30.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (2): Write a program to find the maximum value from list of 10
numbers.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

28
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (3): Write a program to compute the summation of the series:


30
𝑥

𝑥+1
𝑥=1

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (4): Write a program to compute the (𝑁!) value. Where:


𝑁! = 1 × 2 × 3 × … × 𝑁
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

29
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (5): Write a program to compute the mean and standard deviation
where:
(∑𝑛𝑖=1 𝑥𝑖 )
∑𝑛𝑖=1 𝑥𝑖 ∑𝑛𝑖=1 𝑥𝑖2 −
𝑚𝑒𝑎𝑛 = , 𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒 = 𝑛
𝑛 𝑛−1
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

30
Lab (6)
Loop Statements (2)
Example (1): If 𝑁 represent student name and 𝑥 represent student degree,
write a program to print names and averages of the successful students only,
if you know the successful degree is 50% and number of subject (5) and the
number of student (10).
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (2): Write a program to compute the summation of following series


(use 20 term):
1 1 1 1
𝑆 =1+ + + + ⋯+
2! 3! 4! 20!

31
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (3): Write a program that display:


13 14 15 16 17 18
25 26 28 28 29 30
37 38 39 40 41 42
49 50 51 52 53 54
61 62 63 64 65 66
73 74 75 76 77 78
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………

32
Example (4): Write a program that display a multiplication table.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (5): Write a program to compute the summation of the following


series (use 50 term)
𝑥2 𝑥3 𝑥 50
𝑆 = 1 + 𝑥 + + + ⋯+
2! 3! 50!
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

33
Lab (7)
One-dimensional Array
 An array is a collection of a fixed number of components (also called
elements) all of the same data type and in contiguous memory space.
 A one-dimensional array is an array in which the components are arranged
in a list form.
 The entire array is given one name and any particular element within array
is indicated by its position in the array.
 The general form for declaring a one-dimensional array is

in which intExp specifies the number of components in the array and can
be any constant expression that evaluates to a positive integer.
 It is possible to declare an array and initialize it with a sequence of
elements, with a syntax:

 The compiler can count the elements in the initializer list, so you need not
supply a number within the square brackets. If you provide a number
within the square brackets, it should be at least as large as the number of
elements in the initialization list.
 The general form (syntax) used for accessing an array component is:

 in which index, is any expression whose value is a nonnegative integer.


The index value specifies the position of the component in the array.
 In C++, [ ] is an operator called the array subscripting operator. Moreover,
in C++, the array index starts at 0.
 For example:

34
Example (1): Write a program that find the summation of N integer numbers
accepted from user.
 Without using Array.
 Using Array.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

35
Example (2): Write a program that find the Average of five integer numbers
accepted from the user using Array.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

Example (3): Write a program that find the Maximum and Minimum value
in Array of M integer numbers accepted from the user.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

36
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

………………………………………………………………………………………………………………………………………………………………

Example (4): Write a program that search for a specific value inside an Array
of n values accepted from the user.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

37
Example (5): Write a program that print first N of Fibonacci sequence using
Array.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

38
Lab (8)
Two-dimensional Array
 Two-dimensional array: A collection of a fixed number of components
arranged in rows and columns (that is, in two dimensions), wherein all
components are of the same type.
 The syntax for declaring a two-dimensional array is:

wherein intExp1 and intExp2 are constant expressions yielding positive


integer values.The two expressions, intExp1 and intExp2, specify the
number of rows and the number of columns, respectively, in the array.
 The syntax to access a component of a two-dimensional array is:

wherein index1 and index2 are expressions yielding nonnegative integer


values. index1 specifies the row position; index2 specifies the column
position.
For example:

39
Example (1): Write a program that finds he total and average degree of three
students, each student has four degrees using Array.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

40
Example (2): Write a program to print the summation of wo matrices of type
2 × 3 of integers accepted from the user.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

41
Example (3): Write a program to print the Multiplication of two matrices of
type 2 × 2 of integers accepted from the user.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

42
Example (4): Write a program to print the elements in the main diagonal of
matrix of type 𝑛 × 𝑛 of integers accepted from the user.
…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

43
Example (5): Write a program that find the total and average of each column
in matrix of type 𝑛 × 𝑚 of integers accepted from the user.

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

…………………………………………………………………………………………………………………………………………………………………

44

You might also like