07 Auditory Exercises Arrays
07 Auditory Exercises Arrays
07 Auditory Exercises Arrays
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];
array_name[element_index];
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];
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];
}
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:
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;
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;
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;
https://finki-mk.github.io/SP_2023/en/7.html 4/11
11/30/23, 9:58 AM 7
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];
temp = a[n-1];
https://finki-mk.github.io/SP_2023/en/7.html 5/11
11/30/23, 9:58 AM 7
a[0] = temp;
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];
https://finki-mk.github.io/SP_2023/en/7.html 6/11
11/30/23, 9:58 AM 7
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;
n -= deleted;
return 0;
}
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;
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;
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;
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