Function of the Max and Min elements of an Array proble

Hello everyone, I'm pretty new to the whole c++ deal and I would like to know what is the problem with this code.
The reading and writing function of the array alongside the sum are working well.
But it seems the function to find the min and max numbers in the given array isn't working. Can you please help me with this problem?

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
73
74
75
76
77
78
79
80
81
82
83
84
85
 #include <iostream>
using namespace std;

void ArrayRead(int A[200], int lengthA){
	for (int i = 0; i < lengthA ; i++)
	{
		cin >> A[i];
	}
}
void ArrayWrite(int A[200], int lengthA) {
	cout << "A:" << endl;
	for (int i = 0; i < lengthA; i++)
	{
		cout << A[i] << endl;
	}

}

void ArraySum(int A[200], int lengthA, int sum)
{

	sum = 0;
	cout << "Sum:" << endl;
	for (int i = 0; i < lengthA; i++)
	{
		sum = A[i] + sum;
		
	}
	cout << sum << endl;




}

void Arrayminmax(int A[200], int lengthA, int min, int max) {
	

	cout << "max and min:" << endl;

	max = A[0];

	for (int i = 0; i < lengthA; i++)
	{
		if (max < A[i])
			max = A[i];
cout << max;
	}
	
	min = A[0];
	for (int i = 0; i < lengthA; i++)
	{
		if (min > A[i])
			min = A[i];
		cout << min;
	}
	


}



int main ()
{
	int A[200];
	int lengthA;
	int min = A[0];
	int max = A[0];
	int sum = 0;
	cout << "Enter the length of the Array." << endl;
	cin >> lengthA;
	cout << "Enter the elements of the Array according to it's size." << endl;
	ArrayRead(A, lengthA);
	ArrayWrite(A, lengthA);
	ArraySum(A, lengthA, sum);
	Arrayminmax(A, lengthA, min, max);
	



	system("pause");
	return 0;

}


Hope I cleared any confusion..
Take out that system("pause"), or force the output to screen before you get there with a std::endl
I'm pretty sure the system("pause") isn't really causing the problem because I made it work.

I just placed the "cout << min;" and "cout << max;" outside the (for) loop and made it more presentable.

I'll post the code incase someone else runs into this problem..

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;

void ArrayRead(int A[200], int lengthA){
	for (int i = 0; i < lengthA ; i++)
	{
		cin >> A[i];
	}
}
void ArrayWrite(int A[200], int lengthA) {
	cout << "A:" << endl;
	for (int i = 0; i < lengthA; i++)
	{
		cout << A[i] << endl;
	}

}

void ArraySum(int A[200], int lengthA, int sum)
{

	sum = 0;
	cout << "Sum:" << endl;
	for (int i = 0; i < lengthA; i++)
	{
		sum = A[i] + sum;
		
	}
	cout << sum << endl;




}

void Arrayminmax(int A[200], int lengthA, int min, int max) {
	

	

	max = A[0];

	for (int i = 0; i < lengthA; i++)
	{
		if (max < A[i])
			max = A[i];
		

		
	}
	
	cout << "max is: " << endl << max << endl;
	
	min = A[0];
	for (int i = 0; i < lengthA; i++)
	{
		if (min > A[i])
			min = A[i];
			
		
	}
	cout << "min is: " << endl << min << endl;


}



int main ()
{
	int A[200];
	int lengthA;
	int min = A[0];
	int max = A[0];
	int sum = 0;
	cout << "Enter the length of the Array." << endl;
	cin >> lengthA;
	cout << "Enter the elements of the Array according to it's size." << endl;
	ArrayRead(A, lengthA);
	ArrayWrite(A, lengthA);
	ArraySum(A, lengthA, sum);
	Arrayminmax(A, lengthA, min, max);
	



	system("pause");
	return 0;
 

}
I'm pretty sure the system("pause") isn't really causing the problem because I made it work.


It's a terrible thing to have in your programme, but that's beside the point.

The point is that your previous code worked fine, but the output didn't get to the screen because it was being held in a buffer, and would be output at some point, but then your programme called system("pause") which effectively surrenders control entirely to the shell, where anything could happen. Your program stopped running while a different program entirely took over.

Your new code contains endl, which flushes the buffer immediately to screen, before you get to the system("pause"). Which, as I said, is simply a bad thing to do and you shouldn't do it.

If you were more experienced, there would be more nuance, but as a beginner, simple rules are best,. Simple rule; don't use system.
Last edited on
Topic archived. No new replies allowed.