Max value

Hey! I had to write a code to display 20 temps in an array and then get the max and min values. My min value is correct but max isnt even close what could the problem be?

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
#include <iostream>
using namespace std;
 
#include <iomanip>
using std::setw;
 
int main () {

   int n[20];
   int temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, temp9, temp10; 
   int temp11, temp12, temp13, temp14, temp15, temp16, temp17, temp18, temp19, temp20;
   int mx, mn;
   
   cout << "Please enter 20 temperatures" << endl;
   cin >> temp1 >> temp2 >> temp3 >> temp4 >> temp5 >> temp6 >> temp7 >> temp8;
   cin >> temp9 >> temp10 >> temp11 >> temp12 >> temp13 >> temp14 >> temp15;
   cin >> temp16 >> temp17 >> temp18 >> temp19 >> temp20;
          
   for ( int i = 1; i < 21; i++ ) {
      n[ 1 ] = temp1; n[ 2 ] = temp2; n[ 3 ] = temp3; n[ 4 ] = temp4; n[ 5 ] = temp5;
      n[ 6 ] = temp6; n[ 7 ] = temp7; n[ 8 ] = temp8; n[ 9 ] = temp9; n[ 10 ] = temp10;
      n[ 11 ] = temp11; n[ 12 ] = temp12; n[ 13 ] = temp13; n[ 14 ] = temp14; n[ 15 ] = temp15;
      n[ 16 ] = temp16; n[ 17 ] = temp17; n[ 18 ] = temp18; n[ 19 ] = temp19; n[ 20 ] = temp20;    
       
   }
   cout << "Temperature Sample" << setw( 15 ) << "Temperature" << endl;
   cout << "------------------|-----------------" << endl;
 
                        
   for ( int j = 1; j < 21; j++ ) {
      cout << setw( 13 )<< j << setw( 13 ) << n[ j ] << endl;
   }

mn=n[0];
mx=n[0];
for(int i=1;i<20;i++)
	{
		if(mn>n[i])
		{
			mn=n[i];
		}
		else if(mx<n[i])
		{
			mx = n[i];
		}
	}
	
cout << "" << endl;
cout<<"Maximum number is: "<< mx << endl;
cout<<"Minimum number is: "<< mn << endl;



system("pause");
return 0;
}
What is the value of n[0]?


PS. What is utterly wrong with element n[20]?
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
#include <iostream>
using namespace std;
 
#include <iomanip>
using std::setw;
 
int main () {

   int n[20];
   int mn, mx;
  
   
   cout << "Please enter 20 temperatures" << endl;
  
          
   for ( int i = 0; i < 20; i++ ) {
     cin >> n[i];
   }
   cout << "Temperature Sample" << setw( 15 ) << "Temperature" << endl;
   cout << "------------------|-----------------" << endl;
 
                        
   for ( int j = 0; j < 20; j++ ) {
      cout << setw( 13 )<< j+1 << setw( 13 ) << n[ j ] << endl;
   }

mn=n[0];
mx=n[0];
for(int i=0;i<20;i++)
	{
		if(mn>n[i])
		{
			mn=n[i];
		}
		if(mx<n[i])
		{
			mx = n[i];
		}
	}
	
cout << "" << endl;
cout<<"Maximum number is: "<< mx << endl;
cout<<"Minimum number is: "<< mn << endl;



system("pause");
return 0;
}
Thank you!

Also I individually wrote each because when I did just n[20] it wouldnt work so I got frustrated and just went for it haha.
Last edited on
Topic archived. No new replies allowed.