/* * File: hw2-histogram.c * Author: rpruim * Class: cs113 fall 97 * Assignment: hw2 * Last modified: 16 sep 97 * ----------------------------------------------------- * * OVERVIEW: * ========= * This program requests the user to input a sequence of integers * (terminated by a sentinel value of the user's choice) and then produces * a histogram of the data. The user can specify the range for the * histogram as well as the size of the subdivisions ("step size"). * * Algorithm Notes: * ================ * This program makes use of utility functions from hw2-util and hw2-stats. * */ #include #include #include "genlib.h" #include "simpio.h" #include "hw2-util.h" #include "hw2-stats.h" void DisplayInstructions(void); int GetHistVals(int sentinel, int *loPtr, int *hiPtr, int *stepPtr); /* Main Program */ int main() { int sentinel; /* used to indicate end of data */ int *scores; /* array of scores for histogram */ int size; /* size of array */ int lo, hi, step; /* data for histogram shape */ int error; /* indicates histogram values */ DisplayInstructions(); printf("Enter sentinel value: "); sentinel = GetInteger(); printf("\nNow enter values separated by returns and ended with %d.\n\n", sentinel); scores = GetDynamicIntArray(sentinel, "", &size); while(TRUE) { error = GetHistVals(sentinel, &lo, &hi, &step); if (error != 0) {break;} printf("\n\n"); PrintHistogram(scores, size, lo, hi, step); printf("\n\n"); } exit(0); /* let the OS know that the program terminated normally */ } /* end main */ /* * Function: DisplayInstructions * Usage: DisplayInstructions(); * ----------------------------- * This function prints to standard output instructions for the user. */ void DisplayInstructions() { printf("WELCOME TO HW2-HISTOGRAM!!\n\n"); printf("This program will display a histogram for a list of integers\n"); printf("which you provide.\n\n"); printf("You will be asked to enter a sentinel value. This should be\n"); printf("some integer which does not occur in the data set, and is used\n"); printf("to indicate the end of the data. Then you will enter scores.\n"); printf("Once the sentinel value has been entered, you will be asked to\n"); printf("provide the upper and lower bounds for the histogram as well as\n"); printf("the size of each subinterval. Then the program will generate\n"); printf("the desired histogram. Multiple histograms can be displayed for\n"); printf("the same data.\n\n"); } /* end DisplayInstructions() */ int GetHistVals(int sentinel, int *loPtr, int *hiPtr, int *stepPtr) { printf("Low value in histogram (%d to quit): ", sentinel); *loPtr = GetInteger(); if (*loPtr == sentinel) {return (-1);} printf("High value in histogram (%d to quit): ", sentinel); *hiPtr = GetInteger(); if (*hiPtr == sentinel || *hiPtr < *loPtr) {return (-2);} printf("Step size: "); *stepPtr = GetInteger(); if (*stepPtr < 1) {return (-5);} return(0); }