Hello! How do I make a program in C++,in a one-dimensional array consisting of n integer elements, calculate:
-number of paired elements standing in even places in the array,
-the sum of the elements of the array, located after the first maximum element in the array.
-Compress the array, removing from it all the elements whose value coincides with n (the number of elements in the array). Fill in the blanks at the end of the array with zeros.Does not always produce even numbers and I don’t know how to do the last
#include <iostream>
#include <math.h>
usingnamespace std;
int main()
{
int i, n, imax = 0, a = 0, b = 0;
double max = 0, crazy = 0, sum = 0, c = 0;
double sort = 1;
cout << "Enter the number of elements in the array:";
cin >> n;
cout << "Enter array elements:" << endl;
double* p = newdouble[n];
double* p1 = newdouble[n];
for (i = 0; i < n; i++)
cin >> p[i];
for (i = 0; i < n; i++)
{
if ((i % 2) == false)
{
p1[a] = p[i];
a++;
}
}
while(sort)
{
sort = 0;
for (i = 0; i < a - 1; i++)
{
if (p1[i] > p1[i + 1])
{
c = p1[i];
p1[i] = p1[i + 1];
p1[i + 1] = c;
sort = 1;
}
}
}
for (i = 0; i < a; i++)
cout << p1[i] << " ";
cout << endl;
}
do you know how to do it?.Compress the array, removing from it all the elements whose value coincides with n (the number of elements in the array). Fill in the blanks at the end of the array with zeros.
Sets all the elements to Zero b4 any work is done. If you want to delete the elements then that's a different story. The size is in bytes so that should be correct.
#include <iostream>
#include <numeric>
#include <limits>
usingnamespace std;
int main()
{
size_t n {}, even {}, maxpos {};
int maxno {numeric_limits<int>::min()};
cout << "Enter the number of elements in the array: ";
cin >> n;
int* p {newint[n] {}};
cout << "Enter array elements:\n";
for (size_t i = 0; i < n; ++i) {
cin >> p[i];
even += ((p[i] % 2 == 0) && (i % 2 == 0));
if (p[i] > maxno) {
maxno = p[i];
maxpos = i;
}
}
cout << "There are " << even << " paired evens\n";
constint sum {accumulate(p + maxpos + 1, p + n, 0)};
cout << "The sum of the numbers after the maximum element is " << sum << '\n';
// Remove elements == number of elements and set to 0
for (size_t i = 0, nn = n; i < nn; ++i)
if (p[i] == n) {
p[i] = p[--nn];
p[nn] = 0;
}
for (size_t i = 0; i < n; ++i)
cout << p[i] << ' ';
cout << '\n';
delete[] p;
}