///////////////////////////////////////////////////////// // // Assignment #1 - A Solution // Review of C (but used C++ comment style) // Author: Michael A. Redmond // Class: CSC 162 Section 01 // Date: 08/27/01 // // Description: // The Center for The Media in the Public Interest has // commissioned us to write a program to track the coverage // of the Levy / Condit story by TV News. Perhaps, they // believe that the stem cell research issue is perhaps // a tad more important. This program will ask the user // for the number of weeks to track, then for each of NBC, // ABC, CBS, and Fox, ask how many minutes were devoted // to the Levy / Condit story each week. Finally, it // displays summary info. // // Parts of this were removed from the assignment - the // determining which network had the most and least coverage. // // // Assumptions: // Study goes on for no more than 100 weeks // //////////////////////////////////////////////////////// #include #include //////////////////////////////////////////////////////// // // Function Name: GetValid // // Description: // Ask for a (double) value from user and validate it. // User is forced to enter a valid value // // Given: // a text prompt to use in asking user for info // smallest valid value // largest valid value // // Returns: // The first valid value entered by the user // // Side Effects: // None // //////////////////////////////////////////////////////// double GetValid(char prompt[], double min, double max) { double val; // prompt and get value printf(prompt); scanf("%lf",&val); // loop until valid value entered while ((val < min) || (val > max)) { printf("Response is not valid, please enter a number between %6.2f and %6.2f \n", min, max); printf(prompt); scanf("%lf", &val); } return val; } //////////////////////////////////////////////////////// // // Function Name: GetNetworkData // // Description: // Ask user for all info (coverage amounts for all weeks) // for a given TV network. // Uses GetValid to ensure valid values // // Given: // an array to be filled with coverage amounts over all weeks // number of weeks in the study // character string for the network name (e.g. NBC) // // Returns: // Nothing // // Side Effects: // Array of coverage amounts is filled. // //////////////////////////////////////////////////////// void GetNetworkData(double net[], int numWks, char network[4]) { double curr; int cnt; // give context (network) - since GetValid will not know // what network, so cannot customize prompt to help user printf("Now getting data for network: %s \n",network); // loop through all weeks, getting valid coverage amount for each for (cnt = 0; cnt < numWks; cnt++) { printf(" Week %d ",cnt+1); // call function to ensure valid amount entered - must be // positive and no more than 150 hours curr = GetValid("How much time on Levy / Condit story? ",0,150); // stick the valid amount into the network's array net[cnt] = curr; } return; } //////////////////////////////////////////////////////// // // Function Name: CalcTotal // // Description: // Calculate sum of amounts in an array // Here used for total amount of TV coverage time by a network // on the Levy/Condit case // // Given: // an array of coverage amounts over all weeks // number of weeks in the study // // Returns: // Total of all weeks coverage // // Side Effects: // None. // //////////////////////////////////////////////////////// double CalcTotal(double net[], int numWks) { double sum; int cnt; sum = 0; // loop through all weeks and update total for (cnt = 0; cnt < numWks; cnt++) { sum += net[cnt]; } return sum; } //////////////////////////////////////////////////////// // // Function Name: DisplayResults // // Description: // Display BODY of results table for all networks // // Given: // four arrays - containing the amounts of coverage each // week by each of the 4 networks // number of weeks in the study // // Returns: // Nothing // // Side Effects: // Display data to standard output. // //////////////////////////////////////////////////////// void DisplayResults(double net1[], double net2[], double net3[], double net4[], int numWks) { int cnt; // loop through all weeks, displaying each network's amount for each week for (cnt = 0; cnt < numWks; cnt++ ) { printf(" %2d %6.1f %6.1f %6.1f %6.1f \n", cnt,net1[cnt],net2[cnt],net3[cnt],net4[cnt]); } return; } // MAIN FUNCTION int main () { int weeks; double NBC[100]; // arrays for coverage amounts for each network double ABC[100]; double CBS[100]; double Fox[100]; double NBCtotal; // vars used to keep track of total coverage and double NBCave; // mean (ave) coverage for each network. double ABCtotal; double ABCave; double CBStotal; double CBSave; double Foxtotal; double Foxave; double totalTotal; // total coverage for all networds over all weeks double totalAve; // average coverage for double maxCov; // largest amount of coverage double minCov; // smallest amount of coverage char maxCovNet[4]; // network with the largest amount of coverage char minCovNet[4]; // network with the smallest amount of coverage // begin of executable // ask for and get number of weeks in the study printf("Welcome to the Ratings Grabbers Study \n"); printf("How many weeks are we following? "); scanf("%d", &weeks); // obtain all coverage amounts for all networks from user GetNetworkData(NBC, weeks, "NBC"); GetNetworkData(ABC, weeks, "ABC"); GetNetworkData(CBS, weeks, "CBS"); GetNetworkData(Fox, weeks, "Fox"); // calculate total and average coverage (per week) for // all networks NBCtotal = CalcTotal(NBC, weeks); NBCave = NBCtotal / weeks; ABCtotal = CalcTotal(ABC, weeks); ABCave = ABCtotal / weeks; CBStotal = CalcTotal(CBS, weeks); CBSave = CBStotal / weeks; Foxtotal = CalcTotal(Fox, weeks); Foxave = Foxtotal / weeks; // calculate overall total and average totalTotal = NBCtotal + ABCtotal + CBStotal + Foxtotal; totalAve = totalTotal / (4 * weeks); /////////////////////////////////////////////////////// // find network with highest and lowest coverage // starting by comparing NBC and ABC, then comparing // others in turn to max so far and min so far /////////////////////////////////////////////////////// if (NBCtotal > ABCtotal) { // indicate amounts for max and min, and who had them maxCov = NBCtotal; minCov = ABCtotal; strcpy(maxCovNet,"NBC"); strcpy(minCovNet,"ABC"); } else { // ABC had more coverage // indicate amounts for max and min, and who had them minCov = NBCtotal; maxCov = ABCtotal; strcpy(minCovNet,"NBC"); strcpy(maxCovNet,"ABC"); } // if CBS has more than max so far, update if (CBStotal > maxCov){ maxCov = CBStotal; strcpy(maxCovNet,"CBS"); } // if CBS has less than min so far, update if (CBStotal < minCov){ minCov = CBStotal; strcpy(minCovNet,"CBS"); } // if Fox has more than max so far, update if (Foxtotal > maxCov){ maxCov = Foxtotal; strcpy(maxCovNet,"Fox"); } // if Fox has less than min so far, update if (Foxtotal < minCov){ minCov = Foxtotal; strcpy(minCovNet,"Fox"); } // display report of coverage - header, body, then footer // body is displayed using function call printf("\n Coverage of the Levy / Condit Case \n"); printf("\n"); printf("Week NBC ABC CBS Fox \n"); printf("==== === === === === \n"); DisplayResults(NBC,ABC,CBS,Fox, weeks); printf("==== === === === === \n"); printf("Total %6.1f %6.1f %6.1f %6.1f \n \n", NBCtotal, ABCtotal,CBStotal,Foxtotal); printf("Ave %6.1f %6.1f %6.1f %6.1f \n \n", NBCave, ABCave,CBSave,Foxave); printf("Overall average per week: %7.2f \n ", totalAve); printf("Most Coverage: %s \n ",maxCovNet); printf("Least Coverage: %s \n ",minCovNet); return 0; }