////////////////////////////////////////////////////////////////////////// // histogram.c // // // // Purpose // // ======= // // The program obtains a finite set of readings from the user // // and calculates the largest and smallest reading. // // It then obtains from the user the number of intervals // // for a histogram and calculates how many readings fall into // // each interval between Min and Max // // The readings are temporarily stored in an array. // // The reading counts in each interval are also stored in an array // // A C++ built-in array is used // // // // // // Author Creation Date // // ====== ============= // // AY 2 November 2004 // // // // Input/Output // // ============ // // The input is the number of readings (int), the readings (double) // // and the number of intervals between Min and Max // // The input is expected to be from the keyboard // // The output is the largest (double) and smallest reading (double) // // as well as the reading counts in each interval (histogram) // // // ////////////////////////////////////////////////////////////////////////// #include using namespace std; int main() { const int MAXSIZE=100; // absolute max size of the array double readings[MAXSIZE]; // array declaration int hist [MAXSIZE]; int noOfReadings; // actual number of readings int noOfIntervals; // actual number of intervals int carryon = 1; // initialise flag to continue calculations while (carryon) { cout << "Enter actual number of temperature readings (0> noOfReadings; cout << endl; cout << "Enter actual number of intervals for a histogram (0> noOfIntervals; cout << endl; // enter actual readings from keyboard cout << "Enter your readings, one at a time, following the prompt\n"; for (int i = 0; i < noOfReadings; i++) { cout << "reading(" << i+1 << ") = "; cin >> readings[i]; } cout << endl; // find max and min // initialise current max and min double maxRead = readings[0]; double minRead = readings[0]; for (int i = 1; i < noOfReadings; i++) { if (maxRead < readings[i]) { maxRead = readings[i]; } if (minRead > readings[i]) { minRead = readings[i]; } } // compute the histogram // size of interval double d = (maxRead - minRead)/noOfIntervals; cout << "d=" << d << endl; // initialise this histogram first for (int i = 0; i < noOfIntervals; i++) { hist[i] = 0; } // look through each reading and increment the appropriate interval count int offset; for (int i = 0; i < noOfReadings; i++) { offset = int((readings[i]-minRead)/d); if (offset < noOfIntervals) { hist[offset]=hist[offset]++; } else { hist[offset-1]=hist[offset-1]++; } cout << "offset=" << offset << endl; } // output the results cout << "The largest reading was " << maxRead << endl; cout << "The smallest reading was " << minRead << endl; cout << "Now the numbers of readings in each interval between max and min: " << endl; for(int i=0; i < noOfIntervals; i++) { cout << "Interval(" << i+1 << ")= " << hist[i] << endl; } cout << endl << "Do you want to continue? (type 1 if yes; 0 if not) "; cin >> carryon; } cout << "End of Readings" << endl; return 0; } // end main()