[SIZE];
type array_name
int a[10];
float x[99];
char c[5];
[element_index];
array_name
int a[10]; // declaring an array with 10 integers
[0] = 1; // assigning the value 1 to the first element
a<< a[9] << endl; // printing the value of the last element cout
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.
#include<iostream>
using namespace std;
int main() {
int n1, n2, element, i;
int a[100], b[100];
<< "Size of the first array: " << endl;;
cout >> n1;
cin
<< "Size of the second array: " << endl;
cout >> n2;
cin
if (n1 != n2) {
<< "Arrays are not equal!" << endl;
cout } else {
<< "Elements of the first array:" << endl;
cout for (i = 0; i < n1; i++) {
<< "a[" << i << "] = ";
cout >> a[i];
cin }
<< "Elements of the second array:" << endl;
cout for (i = 0; i < n2; i++) {
<< "b[" << i << "] = ";
cout >> b[i];
cin }
// check if arrays are equal:
for (i = 0; i < n1; i++) {
if (a[i] != b[i]) {
break;
}
}
if (i == n1) {
<< "Arrays are equal!" << endl;
cout } else {
<< "Arrays are not equal!" << endl;
cout }
}
return 0;
}
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;
>> n;
cin
for (int i = 0; i < n; i++)
>> a[i];
cin
for (int i = 0; i < n; i++) {
if (a[i] % 2) {
++;
n_odd+= a[i];
sum_odd } else {
++;
n_even+= a[i];
sum_even }
}
<< "Sum even: " << sum_even << endl;
cout << "Sum odd: " << sum_odd << endl;
cout << "Rel: " << (float)n_even / n_odd << endl;
cout
return 0;
}
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;
>> n;
cin
for (int i = 0; i < n; i++){
>> a[i];
cin }
for (int i = 0; i < n; i++){
>> b[i];
cin }
for (int i = 0; i < n; i++){
+= a[i] * b[i];
scalar }
<< "The scalar product is: " << scalar << endl;
cout
return 0;
}
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;
>> n;
cin
for (int i = 0; i < n; i++){
>> a[i];
cin }
for (int i = 0; i < n - 1; i++) {
if (a[i] >= a[i + 1]) {
= 0;
increasing break;
}
}
for (int i = 0; i < n - 1; i++) {
if (a[i] <= a[i + 1]) {
= 0;
decreasing break;
}
}
if (!decreasing && !increasing) {
<< "The array is neither strictly increasing nor strictly decreasing" << endl;
cout } else if (decreasing) {
<< "The array is strictly decreasing" << endl;
cout } else if (increasing) {
<< "The array is strictly increasing" << endl;
cout }
return 0;
}
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];
<< "Size of the array: ";
cout >> n;
cin
for(int i = 0; i < n; i++) {
>> a[i];
cin }
= a[n-1];
temp
for(int i = n - 1; i > 0; i--) {
[i] = a[i-1];
a}
[0] = temp;
a
for(int i = 0; i < n; i++) {
<< a[i] << " ";
cout }
return 0;
}
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];
<< "Size of the array: ";
cout >> n;
cin
<< "Number of rotations: ";
cout >> m;
cin
for(int i = 0; i < n; i++) {
>> a[i];
cin }
for(int i = 0; i < n; i++) {
<< a[i] << " ";
cout }
<< endl;
cout
for(int j = 0; j < m; j++) {
= a[n-1];
temp for(int i = n - 1; i > 0; i--) {
[i] = a[i-1];
a}
[0] = temp;
a}
for(int i = 0; i < n; i++) {
<< a[i] << " ";
cout }
return 0;
}
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;
>> n;
cin
for (int i = 0; i < n; i++){
>> a[i];
cin }
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++) {
[k] = a[k + 1];
a}
++;
deleted--j;
}
}
}
-= deleted;
n
for (int i = 0; i < n; i++) {
<< a[i] << " ";
cout }
return 0;
}