For loop with max/min

I know that this is a simple request. But I've struggled with it for some time.
if someone could give me some direction or help, that would be great.
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

fstream secondfile;
secondfile.open("a3.txt");
 if (!secondfile)
		{
			cout << "Cannot open file" << endl; 
			return 1;
		}
		
	int i;
	int a;
	int b;
	int max;
	int min;
	
	
	for(i = 0; i <= count; i++)
	{
          secondfile >> values;
           if(!secondfile)
           break;
           
          cout << values << endl;
          
          values >> a, b;
           if(values < a)
           max = a;
           else
           if (values > a)
           min = a;
          
          }
	
           cout << max << endl;
           cout << min << endl;
           
    system("Pause");
}

		

	


Last edited on
What is the problem? What is your data? What do expect to happen, and what actually does happen?
well the problem is not know how and what values I have to compare to get the max and min.
my data is a pety six numbers
96 200 196 19 974 99
all I have to do is open the file and run it through a for loop, finding the sets max and min.

1
2
3
4
5
6
values >> a, b;
           if(values < a)
           max = a;
           else
           if (values > a)
           min = a;


i know that values >> a, b; is wrong, but it was worth a shot to try it.
now i am posting here as it "seems" i have exhausted all known resources.

it gives me either all zeros, or one zero and a rather large number

1
2
3
4
5
6
7
8
9
10
11
12
13
for(i = 0; i <= count; i++)
	{
          secondfile >> a, b;
           if(!secondfile)
           break;
          
           if(a > b)
           max = a;
           else
          
           if (a < b)
           min = a;
           


i know have this code which actually gives me the value of the max correctly but gives me 99 for the min when it should be 19, ideas?
I think the basic problem is that you do not initialize min and max. I don't have your file, or the rest of your code, so it is hard to tell exactly what is going on. I would try something like this:


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>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	fstream secondfile;
	secondfile.open("a3.txt");

	if (!secondfile)
	{
		cout << "Cannot open file" << endl; 
		return 0;
	}

	int a, b, max, min;

	secondfile >> a ; // Need at least 1 number in file.
		
	if (!secondfile.eof())
		secondfile >> b; 
	else
		b = a;

	(a<b) ? (max = b,min = a) : (max = a,min = b);
	cout << "The first two numbers are: "<< a << " " << b << endl;

	while (!secondfile.eof())
	{
		secondfile >> a;
		cout << "The next number is: "<< a << endl;

		if(a<min)
		{
			min = a;
		}
		else if(a>max)
		{
			max = a;
		}
	}

	secondfile.close();

	cout << endl << "The max is: " << max << endl;
	cout <<  "The min is: " << min << endl;
	return 0;
}
Last edited on
well thank you very much, i really appreciate it.
Even though it helped me with my code, i still need it in the for loop format.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	for(i = 0; i <= count; i++)
		{
              secondfile >> a;
             
		

		if(a > max)
		{
             max = a;
             }
             else if(a < min)
             {
                  min = a;
                  }
                  }


this is what i have right now.
the outputs for the max yield 974 which is correct
but 0 for the min.

i don't understand what i am doing wrong.
Last edited on
and if you can't get the gist of the code,
here is the whole code for you.
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
90
91
#include <iostream>                                                                  
#include <fstream>                                                                  
#include <string>

using namespace std;

int main ( ) 
{
	fstream myfile;
	myfile.open ("a3.txt");
		if (!myfile)
		{
			cout << "Cannot open file" << endl; 
			return 1;
		}
		
	int values;
	int average;
	int count;
	int total;
	
	
	total = 0;
	count =  0;	

    
    	cout << "The first reading of the file, using a while loop, reads:\n";
	
	while(myfile >> values)
{
                
                 cout << values << endl;
                 total = total + values;
                 count++;
                 
               
          
}
cout << "The total sum of these numbers are: " << total << endl;
average = total / count;
cout << "The average of this number set is: " << average << endl;
myfile.close();

fstream secondfile;
secondfile.open("a3.txt");
 if (!secondfile)
		{
			cout << "Cannot open file" << endl; 
			return 1;
		}
		
	int i;
        int a;

	int max;
	int min;
	
	
  
		
	
		
		for(i = 0; i <= count; i++)
		{
              secondfile >> a;
              
		

		if(a > max)
		{
             max = a;
             }
             else if(a < min)
             {
                  min = a;
                  }
                  }




	secondfile.close();
           cout << max << endl;
           cout << min << endl;
           
    system("Pause");
}

		

	

Last edited on
How can you compare a to max when max is not initialized? This will result in a runtime error. Did you look at the code I posted?
Problem is that you use max and min in expressions without formal initialization. So whatever data is in the space it allocates for the variable is used. It could be garbage.

Just set max and min to -10000, and 10000 respectively.

int max = -10000, min = 10000;
wolfgang yer a monster
thanks so much.
never occurred to me that then needed to me initialized to a VALUE. sheesh.
but thank you so much.
crazy how that one thing can trip the whole thing up.
saying int max; is not initialization. You need to assign a value. max = 0; <-- thats initialization. Saying int max; is declaration.
Or, perhaps better for general use would be to initialize

1
2
int max = -2147483648;
iint min = 2147483647;


(Is there a way to programatically call these numbers?)

Note there is a subtle logical error in your code if you initialize this way instead of the way I did. You will need to change your else if to an if.
You're thing of the std::numeric_limits<int>::max() and std::numeric_limits<int>::min().
This works on VC++. Is it standard?
1
2
	int max = INT_MIN;
	int min = INT_MAX;
@cppmatt: yes, for C. wolfgang's reply was the C++ solution.
@jsmith,

Thanks, I guess I should be careful of my sources and try to stick with c++.
Topic archived. No new replies allowed.