Max among the coordinates of one vector

From the vectors v1, v2, v3 choose the vectors (one or more) that have the largest max among the coordinates. In the selected vectors replace this max with the value 2010. Output max among the coordinates and the selected vectors, output the modified vectors.I wrote the code, but it does not work correctly and I still don’t know how to replace the maximum for 2010

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  #include <iostream>
using namespace std;
void input_size(int & x, const char * text) {
	cout << "Enter the size of the array" << text << " : ";
	cin >> x;
	cout << endl;
}
void input_vector(int * vector, const int size, const char * text) {
	cout << "Enter array elements" << text << " size " << size << " :\n";
	for (int i = 0; i < size; i++)
	{
		cout << "Enter [" << i << "] array element: ";
		cin >> vector[i];
		cout << endl;
	}
}
void output_vector(int * vector, const int size, const char * name) {
	cout << "Array " << name << " : {  ";
	for (int i = 0; i < size; i++)
	{
		cout << vector[i] << "  ";
	}
	cout << "}\n";
}
int max_element(int * vector, const int size, const char * text) {
	int max = vector[0];
	for (int i = 0; i > size; i++)
	{
		if (max > vector[i]) {
			max = vector[i];
		}
	}
	cout << "Maximum array element " << text << " : " << max << endl;
	return max;
}
int max(int a, int b) {
	return a > b ? a : b;
}
void show_vector_max_element(int max_vectors, int max_element_vector, int * vector, const int size, const char * text) {
	if (max_vectors == max_element_vector) {
		output_vector(vector, size, text);
	}
}
int main()
{
	setlocale(LC_ALL, "rus");
	int vector1_size = 0, vector2_size = 0, vector3_size = 0;
	input_size(vector1_size, "vector 1");
	input_size(vector2_size, "vector 2");
	input_size(vector3_size, "vector 3");
	int * vector1 = new int[vector1_size];
	int * vector2 = new int[vector2_size];
	int * vector3 = new int[vector3_size];
	input_vector(vector1, vector1_size, "vector 1");
	input_vector(vector2, vector2_size, "vector 2");
	input_vector(vector3, vector3_size, "vector 3");
	output_vector(vector1, vector1_size, "vector 1");
	output_vector(vector2, vector2_size, "vector 2");
	output_vector(vector3, vector3_size, "vector 3");
	int max_vector1 = max_element(vector1, vector1_size, "vector 1");
	int max_vector2 = max_element(vector2, vector2_size, "vector 2");
	int max_vector3 = max_element(vector3, vector3_size, "vector 3");
	int very_max = max(max_vector1, max(max_vector2, max_vector3));
	cout << "The maximum element of arrays : " << very_max << endl;
	cout << "The maximum element is present in the array : \n";
	show_vector_max_element(very_max, max_vector1, vector1, vector1_size, "vector 1");
	show_vector_max_element(very_max, max_vector2, vector2, vector2_size, "vector 2");
	show_vector_max_element(very_max, max_vector3, vector3, vector3_size, "vector 3");
	delete[]vector1;
	delete[]vector2;
	delete[]vector3;
}
Last edited on
> for (int i = 0; i > size; i++)
Well i > size is always going to be false, so your loop never does anything.

how to make it display 2010 instead of the maximum number?(In the selected vectors replace this max with the value 2010)
1
2
3
4
5
6
7
8
9
10
11
int max_element(int * vector, const int size, const char * text) {
	int max = vector[0];
	for (int i = 1; i < size; i++)  // 2 changes here
	{
		if (max < vector[i]) {  // Change comparison
			max = vector[i];
		}
	}
	cout << "Maximum array element " << text << " : " << max << endl;
	return max;
}


1
2
3
int max(int a, int b) {
	return a > b ? a : b;
}


Not required. Use std::max() when used. Also need to have #include <algorithm>

Rather than returning the max number, don't you need to return the vector index that is the max number? If you have the index, then you can set the element to 2010 as needed.

I swear this same question has been asked 8 different ways in the past week.

Also naming a variable vector is probably a bad idea. I was looking at the code and was very confused for a min. If you use an actual vector you don't need a separate variable to keep track of the size. It keeps track of its own size.

Topic archived. No new replies allowed.