// Mitchell Hashimoto // I certify that the following represents my own independent work and conforms // with the guidelines of academic honesty described in the course syllabus. // hashimot in DIS 1B // 7/24/06 // mhashimoto@ucla.edu #include #include using namespace std; /* * Constants for symbols available to graph */ const int BASE_CHAR = 97; // Begin menu choices at 'a' const int SYMBOL_COUNT = 4; // Number of symbols const char SYMBOLS[SYMBOL_COUNT] = {'X','O','*','+'}; // Symbols available /* * Prototypes */ int lowVal(const int[], int); int highVal(const int[], int); double calcAvg(const int[], int); void outputLine(); int main() { const int MAX_DATA_COUNT = 15, MAX_DATA_VALUE = 30; int dataCount = 0, data[MAX_DATA_COUNT], limits[2] = {0,0}, symbolChoice = 0; string xUnit = "", yUnit = ""; bool b_error = false; char graph[MAX_DATA_VALUE][MAX_DATA_COUNT], finalSymbol; cout << "*** Mitchell's Graphing Program ***" << endl << endl; // Query for amount of data points // Continue while data is not 1 to 15 do { if (b_error) cout << "You can only have between 1 and " << MAX_DATA_COUNT << " data points. Please try again." << endl; cout << "How many data points: "; cin >> dataCount; } while ((dataCount <= 0 || dataCount > MAX_DATA_COUNT) && (b_error = true)); // Reset error flag b_error = false; // Query for lowest value and highest value // Continue while highest val is less than lowest val do { if (b_error) cout << endl << "The lowest value must be lower than the highest value. Please try again." << endl; cout << "What is the lowest value for the graph: "; cin >> limits[0]; // Another loop to check that highest val is less than MAX_DATA_VALUE bool b_high_error = false; do { if (b_high_error) cout << "The highest value can not be greater than " << MAX_DATA_VALUE << ". Please try again." << endl; cout << "What is the highest value for the graph: "; cin >> limits[1]; } while (limits[1] > MAX_DATA_VALUE && (b_high_error = true)); } while ((limits[1] <= limits[0]) && (b_error = true)); // Ignore previous input and ask for units cin.ignore(100, '\n'); cout << "What is the y-axis unit: "; getline(cin, yUnit); cout << "What is the x-axis unit: "; getline(cin, xUnit); b_error = false; // Query for symbol type to use // Check to make sure char entered is within the range available, which // is defined by the constants at the head of this file do { char choice; if (b_error) cout << endl << "You may only enter the letter choices given. Please try again." << endl; cout << "Here are the possible graph symbols: " << endl << "\t"; for (int i = 0; i < SYMBOL_COUNT; i++) { cout << static_cast(i + BASE_CHAR) << ") " << SYMBOLS[i] << "\t"; } cout << endl << "Which symbol option do you prefer: "; cin >> choice; // Must subtract BASE_CHAR to retrieve a valid array index symbolChoice = static_cast(choice) - BASE_CHAR; } while ((symbolChoice < 0 || symbolChoice >= SYMBOL_COUNT) && (b_error = true)); finalSymbol = SYMBOLS[symbolChoice]; // Query for each data point, up to the amount of points, entered earlier cout << endl << "Okay, please enter your " << dataCount << " data points: " << endl; for (int i = 0; i < dataCount; i++) { b_error = false; // Loop while data point is not within the bounds entered earlier do { if (b_error) cout << "\tData point must lie between upper and lower bound. Please try again." << endl; cout << "Enter Point " << (i + 1) << ": "; cin >> data[i]; } while ((data[i] < limits[0] || data[i] > limits[1]) && (b_error = true)); } // Populate MD array: graph // Do so by looping from the top to the bottom and then through each data for (int i = limits[1]; i >= limits[0]; i--) { int index = limits[1] - i; for (int j = 0; j < dataCount; j++) { graph[index][j] = data[j] >= i ? finalSymbol : ' '; } } cout << endl << "Okay, I will now draw your graph:" << endl << endl; outputLine(); // Draw graph by looping backwards and drawing symbol for (int i = limits[1]; i >= limits[0]; i--) { cout << i << "\t"; int index = limits[1] - i; for (int j = 0; j < dataCount; j++) { cout << graph[index][j] << "\t"; } cout << endl; } // Draw data numbers and legend outputLine(); for (int j = 0; j <= dataCount; j++) { cout << j << "\t"; } cout << endl; outputLine(); cout << "Legend: y-axis = " << yUnit << ", and x-axis = " << xUnit << endl; cout << endl; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1); cout << "Highest Value: " << highVal(data, dataCount) << endl; cout << "Lowest Value: " << lowVal(data, dataCount) << endl; cout << "Average Value: " << calcAvg(data, dataCount) << endl; cout << endl << "Thank you and goodbye!" << endl; system("pause"); return 0; } /* * Finds and returns the lowest value in an integer array */ int lowVal(const int data[], int size) { int low = data[0]; for (int i = 1; i < size; i++) { if (data[i] < low) low = data[i]; } return low; } /* * Finds and returns the highest value in an integer array */ int highVal(const int data[], int size) { int high = data[0]; for (int i = 1; i < size; i++) { if (data[i] > high) high = data[i]; } return high; } /* * Calculate the average value of an integer array, * returned as a double */ double calcAvg(const int data[], int size) { double sum = 0; // Add all values in the array for (int i = 0; i < size; sum += data[i], i++); return sum / size; } /* * Draws a line of dashes across the screen */ void outputLine() { cout << "---------------------------------------------------------------------------" << endl; }