Hi,
I've put together this code that processes a 2d array:
Does any body know how I can adapt it to do the same for a 2d array? Also; the histogram must create a 2d output, so that if numbers in the first and second columns in the same line fit within certain bounds, a counter is incremented. e.g.Code:#include <iostream.h> #include <stdlib.h> #include <string> #include <fstream> using namespace std; int main() { string fileName; // create a filename storage for a string of characters 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 readings (0<n<=" << MAXSIZE << "): "; cin >> noOfReadings; cout << endl; // enter name of file with input data cout << "Enter the name of the file with readings data: "; cin >> fileName; ifstream myDataFile; myDataFile.open(fileName.c_str()); // input actual readings from file for (int i=0; i< noOfReadings; i++) { myDataFile >> readings[i]; } // 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]; } } cout << "Enter actual number of intervals for a histogram (0<k<=" << noOfReadings << "): "; cin >> noOfIntervals; cout << endl; // 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()
For the raw data that follows:
1 1
1 2
1 3
There are conditions like:
if arrayName[i]=1 || arrayName[j]=1
then hist offset++
Thanks,
Will