/* You'll need this file for an assignment. Read it and run it and become familiar with it so that I can refer to it in class. Bring it along to class. from "C Programming: A Modern Approach, Study Guide". */ /* --------- ch9/plot.c :-------------------------*/ #include #include #define MAX_COLUMNS 60 float xl, xu, delta, ymin, ymax; void print_header(); void get_domain_parameters(); void calculate_min_max(); void print_plot_header(); void print_plot_footer(); void print_plot(); main() { print_header(); get_domain_parameters(); calculate_min_max(); print_plot(); return 0; } void print_header() { printf("\n==============================================\n"); printf(" Sine Function Plotting Program\n"); printf("==============================================\n"); printf("\n"); } void get_domain_parameters() { printf("Please enter lower bound for domain: "); scanf("%f", &xl); printf("Please enter upper bound for domain: "); scanf("%f", &xu); printf("Please enter interval over domain: "); scanf("%f", &delta); printf("\nYou entered the following domain parameters:\n"); printf(" ===========================================\n"); printf(" Lower bound for domain: %f\n", xl); printf(" Upper bound for domain: %f\n", xu); printf(" Increment value : %f\n", delta); printf(" ===========================================\n\n"); } void calculate_min_max() { float x, y; ymin = sin(xl); ymax = sin(xl); for (x = xl; x <= xu; x += delta) { y = sin(x); if (y < ymin) ymin = y; if (y > ymax) ymax = y; } } void print_plot_header() { printf(" %3.1f", ymin); printf(" %3.1f", (ymin + ymax) / 2); printf(" %3.1f\n", ymax); printf(" +"); printf("-----------------------------+"); printf("------------------------------+\n"); } void print_plot_footer() { printf(" +"); printf("-----------------------------+"); printf("------------------------------+\n"); printf(" %3.1f", ymin); printf(" %3.1f", (ymin + ymax) / 2); printf(" %3.1f\n", ymax); } void print_plot() { int scale_index, i, previous; float x; char A[MAX_COLUMNS]; for (i = 0; i < MAX_COLUMNS; i++) A[i] = ' '; x = xl; previous = 0; print_plot_header(); /*************************************************************/ /* Loop through values of x in the domain, in incs of delta */ /*************************************************************/ while (x <= xu) { /***********************************************************/ /* Calculate scale_index: number of spaces to the left of */ /* the asterisk. Keep track of previous location of the */ /* asterisk. In each iteration, place a space in the */ /* previous location, and an asterisk in the current one. */ /***********************************************************/ scale_index = (sin(x) - ymin) * MAX_COLUMNS / (ymax - ymin) - 0.5; A[previous] = ' '; A[scale_index] = '*'; previous = scale_index; printf("%5.2f |", x); for (i = 0; i < MAX_COLUMNS; i++) putchar(A[i]); printf("|\n"); x = x + delta; } print_plot_footer(); }