One dimensional arrays

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <math.h>
using namespace 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 = new double[n];
	double* p1 = new double[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;
	
	
}
Last edited on
number of paired elements standing in even places in the array,


Can you explain please. What is 'paired elements standing in even places'??
instead of the word "paired", there are even numbers
It looks like you're checking if the element position is even but not checking if the contents of said position is even.
yes, I don't know how to do it yet
It would look something like...

1
2
3
4
5

if (i % 2== 0 && p[i] % 2 ==0) {
// do stuff
}
Last edited on
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.
still don't understand how to do it,the sum of the elements of the array, located after the first maximum element in the array


1
2
   double* p1 = new double[n];
ZeroMemory(p1,n*sizeof(double));


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.
Last edited on
can you write the entire code? For me to understand,and do you know how to do the second?
Lol no. You ain't never going to learn that way.

theres usually already function available for most array manipulation pre-written in the algorithm.

1
2
3
4
#include <algorithm>
using namespace std;
auto max=max_element(begin(p1),end(p1));


As a side note everything im writing is untested and not guaranteed to be error free.
Last edited on
It's <algorithm> in standard C++, not <algorithm.h>.
Edited.
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <numeric>
#include <limits>
using namespace 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 {new int[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";

	const int 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;
}

Topic archived. No new replies allowed.