CSC 101 Lecture Notes Week 4
Looping
Relevant Reading: Chapters 5 and 6
http://users.csc.calpoly.edu/~gfisher/classes/101/examples/week4
/**** * * This program computes simple statistics for numbers read from standard * input. The program first asks for the number of values that the statistics * will be computed for. The program then reads in that many values. * * The statistics computed are the sum of the numbers and the arithmetic mean. * The results are printed to standard output, in the following form: * * Sum = * Mean = * * The precise formula for the mean is defined here: * * http://www.gcseguide.co.uk/statistics_and_probability.htm * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 14apr11 * Last Modified: 14apr11 * */ #include <stdio.h> #include <math.h> int main () { int n; /* Number of values to compute stats for */ double x; /* Input value read from the terminal */ double sum; /* Computed sum */ int i; /* Loop counter variable */ /* * Input the number of values, and prompt for the rest of the data values. */ printf("Input the number of values you want to compute stats for: "); scanf("%d", &n); printf("Input the values, separated by whitespace: "); /* * Initialize the sum to 0. */ sum = 0; /* * Initialize the loop counter to the number of data points. */ i = n; /* * Loop until all the values are read in, accumulating the sum as we go. * Note that the loop will not go at all if the user enters a non-positive * value for the number of data points. */ while (i > 0) { /* * Input the next value. */ scanf("%lf", &x); /* * Increment the sum. */ sum = sum + x; /* * Decrement the loop counter, so we'll stop after n inputs. */ i = i - 1; } /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", sum / n); return 0; } /*** * * QUESTIONS: * * 1. Do we really need the loop counter variable i? Couldn't we just * decrement the value of variable n to know when to stop the loop? * * 2. How come we didn't use the function version of the stats program, i.e., * the version with compute_sum and compute_mean? * * 3. Why didn't we compute the standard deviation? What would it take to do * this? * */
/**** * * This program computes simple statistics for numbers read from standard * input. The program first asks for the number of values that the statistics * will be computed for. The program then reads in that many values. * * The statistics computed are the sum of the numbers and the arithmetic mean. * The results are printed to standard output, in the following form: * * Sum = * Mean = * * The precise formula for the mean is defined here: * * http://www.gcseguide.co.uk/statistics_and_probability.htm * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 14apr11 * Last Modified: 14apr11 * */ #include <stdio.h> #include <math.h> int main () { int n; /* Number of values to compute stats for */ double x; /* Input value read from the terminal */ double sum; /* Computed sum */ int i; /* Loop counter variable */ /* * Input the number of values, and prompt for the rest of the data values. */ printf("Input the number of values you want to compute stats for: "); scanf("%d", &n); printf("Input the values, separated by whitespace: "); /* * Initialize the sum to 0. */ sum = 0; /* * Initialize the loop counter to 0. */ i = 0; /* * Loop until all the values are read in, accumulating the sum as we go. * Note that the loop will not go at all if the user enters a non-positive * value for the number of data points. */ while (i < n ) { /* * Input the next value. */ scanf("%lf", &x); /* * Increment the sum. */ sum = sum + x; /* * Increment the loop counter, so we'll stop after n inputs. */ i = i + 1; } /* * Output the results. */ printf("Sum = %f0, sum); printf("Mean = %f0, sum / n); return 0; }
/**** * * This program makes change, given an amount of purchase and an amount * tendered. The output is a total amount of change, followed by change in five * denominations of money: dollars, quarters, dimes, nickels, and pennies. All * inputs and outputs are in integer cents. * * The program processes multiple transactions, until the user inputs a * negative value for the amount of purchase. * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 15apr11 * Modified: 19apr11 * */ #include <stdio.h> /** * Return the correct number of dollars in change for the given amount of * cents. */ int get_dollars(int cents) { return cents / 100; } /** * Return the correct number of quarters in change for the given amount of * cents. */ int get_quarters(int cents) { return cents % 100 / 25; } /** * Return the correct number of dimes in change for the given amount of * cents. */ int get_dimes(int cents) { return cents % 100 % 25 / 10; } /** * Return the correct number of nickels in change for the given amount of * cents. */ int get_nickels(int cents) { return cents % 100 % 25 % 10 / 5; } /** * Return the correct number of pennies in change for the given amount of * cents. */ int get_pennies(int cents) { return cents % 100 % 25 % 10 % 5; } int main() { /* * Declare program variables to hold the purchase amount, amount tendered, * and total amount of change. */ int purchase; /* Amount of purchase */ int tendered; /* Amount tendered */ int change; /* Total change due */ /* * Prompt the user for the amount of purchase, for the first transaction. */ printf("Input the amount of the purchase, in cents (negative value quits): "); /* * Input the amount of the first purchase. */ scanf("%d", &purchase); /* * Loop through transactions, while the user enters non-negative values for * the amount purchased. Note that the first time through the loop, the * purchase amount is entered before the loop starts. At the end of the * loop, the purchase amount is input again, and the loop continues. */ while (purchase >= 0) { /* * Prompt for the amount tendered. */ printf("Input the amount tendered, in cents: "); /* * Input the amount tendered. */ scanf("%d", &tendered); /* * Output a blank line, for nice formatting. */ printf("\n"); /* * Compute the total amount of change due, in cents. Assume that this * value is non-negative. */ change = tendered - purchase; /* * Output the total change amount, followed by a blank line. */ printf("Total change due = %d\n\n", change); /* * Compute and print the correct change for each denomination. */ printf("Change in dollars through pennies is:\n"); printf(" %d dollars\n", get_dollars(change)); printf(" %d quarters\n", get_quarters(change)); printf(" %d dimes\n", get_dimes(change)); printf(" %d nickels\n", get_nickels(change)); printf(" %d pennies\n", get_pennies(change)); /* * Input the amount of purchase again. */ printf("\nInput the amount of the purchase, in cents (negative value quits): "); scanf("%d", &purchase); } return 0; }
/**** * * This program makes change, given an amount of purchase and an amount * tendered. The output is a total amount of change, followed by change in five * denominations of money: dollars, quarters, dimes, nickels, and pennies. All * inputs and outputs are in integer cents. * * The program processes multiple transactions, until the user inputs 'q' to * quit. * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 15apr11 * Modified: 19apr11 * */ #include <stdio.h> /** * Return the correct number of dollars in change for the given amount of * cents. */ int get_dollars(int cents) { return cents / 100; } /** * Return the correct number of quarters in change for the given amount of * cents. */ int get_quarters(int cents) { return cents % 100 / 25; } /** * Return the correct number of dimes in change for the given amount of * cents. */ int get_dimes(int cents) { return cents % 100 % 25 / 10; } /** * Return the correct number of nickels in change for the given amount of * cents. */ int get_nickels(int cents) { return cents % 100 % 25 % 10 / 5; } /** * Return the correct number of pennies in change for the given amount of * cents. */ int get_pennies(int cents) { return cents % 100 % 25 % 10 % 5; } int main() { /* * Declare program variables to hold the purchase amount, amount tendered, * and total amount of change. */ int purchase; /* Amount of purchase */ int tendered; /* Amount tendered */ int change; /* Total change due */ char quit_char = ' '; /* Loop sentinel */ /* * Loop through transactions, while the user doesn't type 'q'. Compared to * make_change_with_loop.c, there is only once place in the code where the * purchase amount is entered -- at the top of the loop. Think about * what's going on here compared to make_change_with_loop.c */ while (quit_char != 'q') { /* * Prompt the user for the amount of purchase. */ printf("Input the amount of the purchase, in cents: "); /* * Input the amount of purchase. */ scanf("%d", &purchase); /* * Prompt for the amount tendered. */ printf("Input the amount tendered, in cents: "); /* * Input the amount tendered. */ scanf("%d", &tendered); /* * Output a blank line, for nice formatting. */ printf("\n"); /* * Compute the total amount of change due, in cents. Assume that this * value is non-negative. */ change = tendered - purchase; /* * Output the total change amount, followed by a blank line. */ printf("Total change due = %d\n\n", change); /* * Compute and print the correct change for each denomination. */ printf("Change in dollars through pennies is:\n"); printf(" %d dollars\n", get_dollars(change)); printf(" %d quarters\n", get_quarters(change)); printf(" %d dimes\n", get_dimes(change)); printf(" %d nickels\n", get_nickels(change)); printf(" %d pennies\n", get_pennies(change)); /* * Ask the user whether or not to quit. Note well that the message * tells the user to input a non-whitespace character to continue. * This means that the user cannot just type the Enter key on a blank * line to continue, but must type some non-blank character. It would * take some more work to write the program to allow a single press of * the Enter key to be considered an input. This is because scanf is a * rather finicky function. */ printf("Type 'q' to quit, any other non-whitespace character to continue: "); /* * Getting the following scanf to work properly is a bit tricky. The * rule for the scanf %c conversion is that it does not automatically * skip leading whitespace. The trouble is, the newline character that * the user typed to enter the most recent numeric input is still * sitting on the input stream. So, the scanf format string needs to * have the \n before the %c, in order to tell scanf to skip the * lingering newline char in the input stream. Again, scanf is quite * the finicky little bugger. */ scanf("\n%c", &quit_char); } return 0; }
/**** * * This program computes simple statistics for numbers read from standard * input. The program first asks for the number of values that the statistics * will be computed for. The program then reads in that many values. * * The statistics computed are the sum of the numbers and the arithmetic mean. * The results are printed to standard output, in the following form: * * Sum = * Mean = * * The precise formula for the mean is defined here: * * http://www.gcseguide.co.uk/statistics_and_probability.htm * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 14apr11 * Last Modified: 14apr11 * */ #include <stdio.h> #include <math.h> int main () { int n; /* Number of values to compute stats for */ double x; /* Input value read from the terminal */ double sum; /* Computed sum */ int i; /* Loop counter variable */ /* * Input the number of values, and prompt for the rest of the data values. */ printf("Input the number of values you want to compute stats for: "); scanf("%d", &n); printf("Input the values, separated by whitespace: "); /* * Initialize the sum to 0. */ sum = 0; /* * Compared to the while loop version of the program, the statement to * initialize the loop counter is missing here. This is because the * initialization happens in for loop initialization expression, i.e., the * first part of the for loop heading. */ /* * Loop until all the values are read in, accumulating the sum as we go. * Note that the loop will not go at all if the user enters a non-positive * value for the number of data points. */ for (i = 0; i < n; i++) { /* * Input the next value. */ scanf("%lf", &x); /* * Increment the sum. */ sum = sum + x; /* * Compared to the while loop version of the program, the statement to * increment the loop counter is missing here. This is because the * incrementing happens in for loop update expression, i.e., the third * part of the for loop heading. */ } /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", sum / n); return 0; }
/**** * * This program computes simple statistics for numbers read from standard * input. The program first asks for the number of values that the statistics * will be computed for. The program then reads in that many values. * * The statistics computed are the sum of the numbers and the arithmetic mean. * The results are printed to standard output, in the following form: * * Sum = * Mean = * * The precise formula for the mean is defined here: * * http://www.gcseguide.co.uk/statistics_and_probability.htm * * * Author: Gene Fisher (gfisher@calpoly.edu) * Created: 14apr11 * Last Modified: 14apr11 * */ #include <stdio.h> #include <math.h> int main () { int n; /* Number of values to compute stats for */ double x; /* Input value read from the terminal */ double sum; /* Computed sum */ int i; /* Loop counter variable */ /* * Input the number of values, and prompt for the rest of the data values. */ printf("Input the number of values you want to compute stats for: "); scanf("%d", &n); printf("Input the values, separated by whitespace: "); /* * Compared to the longer for loop version of the program, the statement to * initialize the sum is missing here. This is because this initialization * happens in for loop initialization expression, by separating multiple * initialization expressions with commas. */ /* * Compared to the while loop version of the program, the statement to * initialize the loop counter is missing here. This is because the * initialization happens in for loop initialization expression, i.e., the * first part of the for loop heading. */ /* * Loop until all the values are read in, accumulating the sum as we go. * Note that the loop will not go at all if the user enters a non-positive * value for the number of data points. */ for (i = 0, sum = 0; i < n; i++) { /* * Input the next value. */ scanf("%lf", &x); /* * Increment the sum, using the sweet little incrementing assignment * statement. */ sum += x; } /* * Output the results. */ printf("Sum = %f\n", sum); printf("Mean = %f\n", sum / n); return 0; }
#include <stdio.h> int main() { int n,i; double x,sum; printf("Input the number of values you want to compute stats for: "); scanf("%d",&n); printf("Input the values, separated by whitespace: "); for (i=0,sum=0; i<n && scanf("%lf",&x)!=EOF; i++,sum+=x) ; printf("Sum = %f\nMean = %f\n", sum, sum/n); }