Auditory Exercises 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

10/21/23, 7:45 AM 2

Structural Programming
Auditory exercise 2

1. Introduction to C++
The C++ language was developed in the Bell Laboratories and was initially named “C with
Classes.”
The name “C++” includes operations to enhance the C language, indicating that C++ is an
extended version of C with additional capabilities.
C++ incorporates many features of the C language, supplemented with additional mechanisms
for object-oriented programming (OOP).
The C++ compiler can also be used to compile C programs.

2. Program Structure
Reminder: The source code of the C++ programming language is organized into functions.

#include<iostream>
using namespace std;

int main() {
variable declaration;
program statements;
}

3. Basic C++ Program

#include <iostream>
using namespace std;
// main function
int main() {

https://finki-mk.github.io/SP_2023/en/2.html 1/7
10/21/23, 7:45 AM 2

/*
print messages to the screen
*/
cout << "Welcome to FINKI!" << endl;
return 0;
}

#include - directive to include external libraries.


<iostream> - library for working with standard input/output streams (keyboard, screen).
cout - global object for working with the output stream (screen).
<< - operator for printing to the output stream (screen).

4. Input/Output Streams in C++ (<iostream>)


In C++, for working with input/output streams, we use the operators: - << from the cout object (cout
<<) - >> from the cin object (cin >>)

These objects are part of the standard library iostream, which is included in the program using the
directive #include <iostream> at the beginning of the code.

Example:

#include<iostream>
using namespace std;

int main() {
int value;
cout << "Enter a value for the variable value: ";
cin >> value;
cout << "The entered value is: " << value << '\n';
}

5. Variables
Variables are symbolic names for locations in memory where values are stored.
All variables must be declared before they are used.
Each time a new value is assigned to a variable, the old value is overwritten.

5.1. Variable Declaration in C++

In the C++ programming language, variables are declared with the following syntax:

https://finki-mk.github.io/SP_2023/en/2.html 2/7
10/21/23, 7:45 AM 2

data_type variable_name = initial_value;

data_type refers to the type of data that the variable will store, such as an integer, a floating-point
number, a character, etc.
variable_name denotes the name of the variable, which should be unique within a given code
block. Variable names can be a combination of letters, digits, and the underscore character (_),
but they must start with a letter or an underscore. Variable names should clearly indicate what is
stored in them.
When declaring a variable, it is optional to provide an initial value.

Examples of variable declarations of different types:

int number = 5;
float price = 7.99;
char letter = 'a';
bool is_true = true;

5.2. Variable Types in C++

Integer Character Floating-Point Boolean

int char float bool

short double

long long double

6. Comments
Comments are used for explanations or documentation within the source code. There are two
types of comments:

Single-line comments:

// This is a single-line comment

Multi-line comments:

/*
This is a

https://finki-mk.github.io/SP_2023/en/2.html 3/7
10/21/23, 7:45 AM 2

multi-line comment
*/

7. Operators
Operators are applied to numbers (integers or decimals):

Operator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus (remainder when dividing)

8. Exercises
8.1. Exercise 1

Write a program that calculates the value of the mathematical expression: x = 3/2 + (5 - 46*5/12)

Solution:

#include <iostream>
using namespace std;

int main() {
float x = 3.0 / 2 + (5 - 46 * 5.0 / 12);
cout << "x = " << x << endl;
return 0;
}

8.2. Exercise 2

Write a program that, given a value of x (as a variable declaration), calculates and prints x
squared.

Solution:

https://finki-mk.github.io/SP_2023/en/2.html 4/7
10/21/23, 7:45 AM 2

#include <iostream>
using namespace std;

int main() {
int x = 7;
cout << "The square of " << x << " is " << x * x << endl;
return 0;
}

8.3. Exercise 3

Write a program that, for given sides of an equilateral triangle, prints its perimeter and the square
of its area (let’s use a = 5, b = 7.5, c = 10.2).

Solution:

#include <iostream>
using namespace std;

int main() {
float a = 5.0;
float b = 7.5;
float c = 10.2;
float perimeter = a + b + c;
float s = perimeter / 2;
float area = s * (s - a) * (s - b) * (s - c);
cout << "The perimeter is: " << perimeter << endl;
cout << "The area square is: " << area << endl;
return 0;
}

8.4. Exercise 4

Write a program that calculates the arithmetic mean of the numbers 3, 5, and 12.

Solution:

#include <iostream>
using namespace std;

int main() {
int a = 3;
int b = 5;
int c = 12;

https://finki-mk.github.io/SP_2023/en/2.html 5/7
10/21/23, 7:45 AM 2

float average = (a + b + c) / 3.0

;
cout << "The arithmetic mean is: " << average << endl;
return 0;
}

8.5. Exercise 5

Write a program that prints the remainders when the number 19 is divided by 2, 3, 5, and 8.

Solution:

#include <iostream>
using namespace std;

int main() {
int num = 19;
cout << "The remainder when 19 is divided by 2 is: " << num % 2 << endl;
cout << "The remainder when 19 is divided by 3 is: " << num % 3 << endl;
cout << "The remainder when 19 is divided by 5 is: " << num % 5 << endl;
cout << "The remainder when 19 is divided by 8 is: " << num % 8 << endl;
return 0;
}

8.6. Exercise 6
Write a program to calculate and print the area and perimeter of a circle. The radius of the circle
should be read from the standard input (keyboard) as a decimal number.

Solution:

#include <iostream>
using namespace std;

int main() {
float radius;
cin >> radius;

float perimeter = 2 * radius * 3.14;


float area = radius * radius * 3.14;

cout << "Perimeter = " << perimeter << endl;


cout << "Area = " << area << endl;

https://finki-mk.github.io/SP_2023/en/2.html 6/7
10/21/23, 7:45 AM 2

return 0;
}

8.7. Exercise 7
Write a program that reads two integers from the standard input and prints their sum, difference,
product, and remainder when divided.

Solution:

#include <iostream>
using namespace std;

int main() {
int x, y;
cin >> x >> y;

cout << x << " + " << y << " = " << x + y << endl;
cout << x << " - " << y << " = " << x - y << endl;
cout << x << " * " << y << " = " << x * y << endl;
cout << x << " % " << y << " = " << x % y << endl;
return 0;
}

8.8. Exercise 8

Write a program that reads an uppercase letter from the standard input and prints the same letter
as lowercase. - Note: Each character is represented by an ASCII number.

Solution:

#include <iostream>
using namespace std;

int main() {
char uppercase;
cout << "Enter an uppercase letter: " << endl;
cin >> uppercase;
cout << uppercase << " is written in lowercase as " << char(uppercase + ('a' - 'A')) <<
endl;
return 0;
}

https://finki-mk.github.io/SP_2023/en/2.html 7/7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy