07 Auditory Exercises Arrays

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

11/30/23, 9:58 AM 7

Structural Programming
Auditory Exercise 7 - Arrays

1. Lecture Reminder
1.1. Array Declaration

type array_name[SIZE];

int a[10];
float x[99];
char c[5];

type - type of elements in the array


array_name - name of the array
SIZE - number of elements in the array

1.2. Accessing Array Elements

array_name[element_index];

int a[10]; // declaring an array with 10 integers


a[0] = 1; // assigning the value 1 to the first element
cout << a[9] << endl; // printing the value of the last element

2. Tasks
2.1. Task 1

Write a program that will check whether two arrays are equal or not. Display the result of the
comparison on the screen. The maximum size of the arrays is 100.

https://finki-mk.github.io/SP_2023/en/7.html 1/11
11/30/23, 9:58 AM 7

#include<iostream>
using namespace std;

int main() {
int n1, n2, element, i;
int a[100], b[100];

cout << "Size of the first array: " << endl;;


cin >> n1;

cout << "Size of the second array: " << endl;


cin >> n2;

if (n1 != n2) {
cout << "Arrays are not equal!" << endl;
} else {
cout << "Elements of the first array:" << endl;
for (i = 0; i < n1; i++) {
cout << "a[" << i << "] = ";
cin >> a[i];
}

cout << "Elements of the second array:" << endl;


for (i = 0; i < n2; i++) {
cout << "b[" << i << "] = ";
cin >> b[i];
}

// check if arrays are equal:


for (i = 0; i < n1; i++) {
if (a[i] != b[i]) {
break;
}
}

if (i == n1) {
cout << "Arrays are equal!" << endl;
} else {
cout << "Arrays are not equal!" << endl;
}
}

return 0;
}

https://finki-mk.github.io/SP_2023/en/7.html 2/11
11/30/23, 9:58 AM 7

2.2. Task 2

Write a program that, for an array whose elements are entered from the keyboard, calculates the
sum of even elements, the sum of odd elements, and the ratio between the number of even and
odd elements. Print the results to the screen.

Example:

For the array: 3 2 7 6 2 5 1, the output should be:

Sum even: 8
Sum odd: 16
Rel: 0.75

#include<iostream>
using namespace std;

int main() {
int n, a[100], n_odd = 0, n_even = 0, sum_odd = 0, sum_even = 0;

cin >> n;

for (int i = 0; i < n; i++)


cin >> a[i];

for (int i = 0; i < n; i++) {


if (a[i] % 2) {
n_odd++;
sum_odd += a[i];
} else {
n_even++;
sum_even += a[i];
}
}

cout << "Sum even: " << sum_even << endl;


cout << "Sum odd: " << sum_odd << endl;
cout << "Rel: " << (float)n_even / n_odd << endl;

return 0;
}

2.3. Task 3

https://finki-mk.github.io/SP_2023/en/7.html 3/11
11/30/23, 9:58 AM 7

Write a program that calculates the scalar product of two vectors with n coordinates. The number
of coordinates n, as well as the coordinates of the vectors, are entered from the standard input.
Print the result to the screen.

#include<iostream>
using namespace std;

int main() {
int a[100], b[100], n, scalar = 0;

cin >> n;

for (int i = 0; i < n; i++){


cin >> a[i];
}
for (int i = 0; i < n; i++){
cin >> b[i];
}
for (int i = 0; i < n; i++){
scalar += a[i] * b[i];
}
cout << "The scalar product is: " << scalar << endl;

return 0;
}

2.4. Task 4

Write a program that checks if a given array of n elements, read from the standard input, is strictly
increasing, strictly decreasing, or neither strictly increasing nor strictly decreasing. Print the result
to the screen.

#include<iostream>
using namespace std;

int main() {
int n, element, a[100];
short increasing = 1, decreasing = 1;

cin >> n;

for (int i = 0; i < n; i++){


cin >> a[i];
}

https://finki-mk.github.io/SP_2023/en/7.html 4/11
11/30/23, 9:58 AM 7

for (int i = 0; i < n - 1; i++) {


if (a[i] >= a[i + 1]) {
increasing = 0;
break;
}
}

for (int i = 0; i < n - 1; i++) {


if (a[i] <= a[i + 1]) {
decreasing = 0;
break;
}
}

if (!decreasing && !increasing) {


cout << "The array is neither strictly increasing nor strictly decreasing" << endl;
} else if (decreasing) {
cout << "The array is strictly decreasing" << endl;
} else if (increasing) {
cout << "The array is strictly increasing" << endl;
}

return 0;
}

2.5. Task 5

Write a program to rotate the elements of an array one place to the right. Finally, print the rotated
array to the screen. Read the elements of the array from the standard input.

#include<iostream>
using namespace std;

int main() {
int n, temp;
int a[100];

cout << "Size of the array: ";


cin >> n;

for(int i = 0; i < n; i++) {


cin >> a[i];
}

temp = a[n-1];
https://finki-mk.github.io/SP_2023/en/7.html 5/11
11/30/23, 9:58 AM 7

for(int i = n - 1; i > 0; i--) {


a[i] = a[i-1];
}

a[0] = temp;

for(int i = 0; i < n; i++) {


cout << a[i] << " ";
}

return 0;
}

2.6. Task 6

Write a program to rotate the elements of an array m places to the right. Finally, print the rotated
array

to the screen. Read the elements of the array and the number of rotations from the standard input.

#include<iostream>
using namespace std;

int main() {
int n, temp, m;
int a[100];

cout << "Size of the array: ";


cin >> n;

cout << "Number of rotations: ";


cin >> m;

for(int i = 0; i < n; i++) {


cin >> a[i];
}

for(int i = 0; i < n; i++) {


cout << a[i] << " ";
}
cout << endl;

for(int j = 0; j < m; j++) {


temp = a[n-1];

https://finki-mk.github.io/SP_2023/en/7.html 6/11
11/30/23, 9:58 AM 7

for(int i = n - 1; i > 0; i--) {


a[i] = a[i-1];
}
a[0] = temp;
}

for(int i = 0; i < n; i++) {


cout << a[i] << " ";
}

return 0;
}

2.7. Task 7
Write a program that removes duplicates from an array. Finally, print the obtained array to the
screen. Read the elements of the array from the standard input.

#include<iostream>
using namespace std;

int main() {
int a[100], n, j, k, deleted = 0;

cin >> n;

for (int i = 0; i < n; i++){


cin >> a[i];
}
for (int i = 0; i < n - deleted; i++) {
for (int j = i + 1; j < n - deleted; j++) {
if (a[i] == a[j]) {
for (int k = j; k < n - 1 - deleted; k++) {
a[k] = a[k + 1];
}
deleted++;
--j;
}
}
}

n -= deleted;

for (int i = 0; i < n; i++) {


cout << a[i] << " ";
}
https://finki-mk.github.io/SP_2023/en/7.html 7/11
11/30/23, 9:58 AM 7

return 0;
}

3. Matrices (Two-Dimensional Arrays)


3.1. Task 1

Write a program that, for a matrix entered from the keyboard, calculates the difference between the
sum of elements in odd columns and the sum of elements in even rows. The matrix does not have
to be square.

#include<iostream>
using namespace std;

int main() {
int a[100][100], n, m, sumCols = 0, sumRows = 0;

cin >> n >> m;

for (int i = 0; i < n; i++){


for (int j = 0; j < m; j++){
cin >> a[i][j];
}
}

for (int i = 0; i < n; i++){


for (int j = 0; j < m; j++) {
if ((j + 1) % 2){
sumCols += a[i][j];
}
if (!((i + 1) % 2)){
sumRows += a[i][j];
}
}
}

cout << sumCols - sumRows;

return 0;
}

3.2. Task 2

https://finki-mk.github.io/SP_2023/en/7.html 8/11
11/30/23, 9:58 AM 7

Write a program that, for a matrix entered from the keyboard, replaces the elements of the main
diagonal with the difference between the maximum and minimum element in the matrix. Print the
resulting matrix to the screen.

#include<iostream>
using namespace std;

int main() {
int a[100][100], n, max, min;
cin >> n;

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
cin >> a[i][j];
if (i == 0 && j == 0) {
min = max = a[i][j];
} else if (a[i][j] > max) {
max = a[i][j];
} else if (a[i][j] < min) {
min = a[i][j];
}
}
}

for (int i = 0; i < n; i++) {


a[i][i] = max - min;
}

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}

return 0;
}

3.3. Task 3

Write a program that, for a square matrix entered from the keyboard, prints to the screen whether it
is symmetric with respect to the main diagonal.

https://finki-mk.github.io/SP_2023/en/7.html 9/11
11/30/23, 9:58 AM 7

#include<iostream>
using namespace std;

int main() {
int a[100][100], n, symmetric = 1;

cout << "Enter the dimension of the square matrix:" << endl;
cin >> n;

cout << "Enter the elements:" << endl;

for (int i = 0; i < n; i++){


for (int j = 0; j < n; j++){
cin >> a[i][j];
}
}

for (int i = 0; i < n - 1; i++) {


for (int j = i + 1; j < n; j++) {
if (a[i][j] != a[j][i]) {
symmetric = 0;
break;
}
}
if (!symmetric){
break;
}
}

if (symmetric){
cout << "The matrix is SYMMETRIC with respect to the main diagonal" << endl;
}
else{
cout << "The matrix is not SYMMETRIC with respect to the main diagonal" << endl;
}
return 0;
}

3.4. Homework

3.4.1. Task 1

It is necessary to find and count all occurrences of the “X” shape composed only of elements with a
value of 1. The “X” shape consists of 5 elements with a value of 1 that are appropriately distributed

https://finki-mk.github.io/SP_2023/en/7.html 10/11
11/30/23, 9:58 AM 7

in the matrix (an element with a value of 1 that has elements with a value of 1 as its diagonal
neighbors).

The dimensions of a matrix and its elements are entered from the standard input. Count all
occurrences of the “X” shape within the matrix. Assume that there should be no overlap of
elements from two “X” shapes (example 2). Occurrences of the “X” shape are sought from left to
right and top to bottom.

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

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