Bracketing data

Hello, I'm new to the forums but I've stumbled upon a problem I hope you guys can help me solve. I want to take a .txt file I have full of numbers, multiply them by ten, convert them into integers, and eventually construct a histogram with them. My biggest problem right now is that I want to limit my data coming in so as to only include numbers between 0 and 20.49. The file I have contains numbers outside those ranges and I am not allowed to modify it. Like I said, I am relatively new to c++ and as such, I will be unable to understand many of the technical terms that may arise. If you can give me examples of what a solution might be, that would be so much better. Thank you in advance.

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
//
  // Infile.cpp : Defines th

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

#define ARRAYSIZE 20


int _tmain(int argc, _TCHAR* argv[])
{

	ifstream infile;
	ofstream outfile;

	int myArray[ARRAYSIZE];
	double input;
	int intinput;
	

	double the_max = 20.49;
	double the_min = 0; 
	
	infile.open("rdata.txt");
	if (infile.fail()){
		cout << "can't open rdata.txt\n";
		exit(1);
	}
	outfile.open("rdata_histogram.txt");
	if (outfile.fail()){
		cout << "can't open rdata_histogram.txt\n";
		exit(2);
	}


	double foundMin = 0;
	double foundMax = 20.49;

	for (int i = 0; i<10000 && !infile.eof(); i++){
		infile >> input;
		if (input < foundMin) foundMin = input;
		if (input > foundMax) foundMax = input;
	}
	cout << "Min Found is " << foundMin << endl;
	cout << "Max Found is " << foundMax << endl;


	infile.close();
	outfile.close();


	return 0;
}

I think you confused the starting value of foundMin and foundMax - shouldn't they be the other way round at the begining?
1
2
3
4
5
6
7
8
9
10
11
12
13
double foundMin = 20.50; // intentionally larger than anything acceptable
double foundMax = 0;
for (int i = 0; i<10000 && !infile.eof(); i++){
	infile >> input;
	if(input > 20.49) continue; // Ignore numbers over 20.49
	if (input < foundMin) foundMin = input;
	if (input >= foundMax) foundMax = input;
}
if (foundMin == 20.50) cout << "All numbers are over 20.49";
else{
	cout << "Min Found is " << foundMin << endl;
	cout << "Max Found is " << foundMax << endl;
}


You may consider handling negative numbers in similar manner too.
Last edited on
Topic archived. No new replies allowed.