CSC 101 Lecture Notes Week 6
Selected Topics Relevant to Programming Assignment 3
More on Arrays and Strings
Relevant Reading: Chapters 7, 8, and 9
typedef char Deck[DECK_SIZE][CARD_STR_LEN];
instead of the much bulkier version like thisvoid shuffle( Deck unshuffled_deck, /* Unshuffled input deck */ Deck shuffled_deck /* shuffled output deck */ );
void shuffle( char unshuffled_deck[DECK_SIZE][CARD_STR_LEN], /* Unshuffled input deck */ char shuffled_deck[DECK_SIZE][CARD_STR_LEN] /* shuffled output deck */ );
/****
*
* This program computes simple statistics for up to 1000 real numbers read
* from standard input. The numbers are read up to EOF or 1000 input values,
* which ever occurs first. The statistics computed are the sum of the
* numbers, the arithmetic mean, and the standard deviation. The results are
* output to standard output, in the following form:
*
* Sum =
* Mean =
* Standard Deviation =
*
* The precise formulae for mean and standard deviation are as defined here:
*
* http://www.gcseguide.co.uk/statistics_and_probability.htm
*
*
* Author: Gene Fisher (gfisher@calpoly.edu)
* Created: 31mar11
* Last Modified: 3apr11
*
*/
#include <stdio.h>
#include <math.h>
#define MAX_DATA_POINTS 1000
/*
* Declare the prototypes for functions used in the program.
*/
int read_values(double data[], int max);
double compute_sum(double data[], int n);
double compute_mean(double data[], int n);
double compute_std_dev(double data[], int n);
int main () {
/*
* Declare an array to hold the numbers, and an int for the number of
* values read in. Decclare a double to check if stdin is emmpty.
*/
double data[MAX_DATA_POINTS];
int n;
double datum;
/*
* Prompt the user for the data values. From the terminal, the user
* generates an EOF by typing control-D. If input is redirected from a
* file, EOF is produced after the last value is read from the file.
*/
printf(
"Enter up to %d numeric values, terminating input with control-D:\n",
MAX_DATA_POINTS);
/*
* Call the read_values function to read the numbers into the data array,
* and return the number of values read.
*/
n = read_values(data, MAX_DATA_POINTS);
/*
* Determine if there are any remaining data values on stdin, and tell the
* user that they will be ignored.
*/
if (scanf("%lf", &datum) != EOF) {
printf("\n NOTE: The program will use the first 1000 numbers only.\n\n");
}
/*
* Compute and output the results.
*/
printf("Sum = %f\n", compute_sum(data, n));
printf("Mean = %f\n", compute_mean(data, n));
printf("Standard Deviation = %f\n\n", compute_std_dev(data, n));
return 0;
}
/*
* Read up to max values from standard input, and put the values in the given
* data array. Return the number of values read, up to EOF or the given max,
* whichever occurs first. It is the caller's responsibility to ensure that
* the given data array has at least max elements.
*/
int read_values(double data[], int max) {
int i;
for (i = 0; i < max && scanf("%lf", &data[i]) != EOF; i++)
;
return i;
}
/*
* Return the sum of the first n values of the given data array.
*/
double compute_sum(double data[], int n) {
int i;
double sum;
for (i = 0, sum = 0; i < n; i++) {
sum += data[i];
}
return sum;
}
/*
* Return the arithmetic mean of the first n values of the given data array.
*/
double compute_mean(double data[], int n) {
return compute_sum(data, n) / n;
}
/*
* Return the standard deviation of the first n values of the given data array.
*/
double compute_std_dev(double data[], int n) {
int i;
double mean = compute_mean(data, n);
double sum_squares = 0;
for (i = 0; i < n; i++) {
sum_squares += pow(data[i] - mean, 2);
}
return sqrt(sum_squares / (n - 1 ));
}
Figure 1: Array parameter to read_values function.
/* * Read positive numbers from stdin, store them in an array. Print out the * array when done. !No bounds checking! */ #include <stdio.h> int main() { int i = 0; /* Array index for input. */ int j = 0; /* Array index for output. */ double x; /* Input value */ double a[5]; /* Array to hold numbers */ /* * Prompt and read first number. */ printf("Input positive numbers, negative to stop: "); scanf("%lf", &x); /* * Loop until negative number input. */ while (x > 0) { /* * Store input in next array element. */ a[i] = x; /* * Increment array index. */ i = i + 1; /* * Read in next number. */ scanf("%lf", &x); } /* * Print out the array. */ while (j < i) { printf("a[%d] = %f\n", j, a[j]); j++; } return 0; }
store Input positive numbers, negative to stop: 1 2 3 4 5 6 -1
Segmentation violation Bus error Illegal instruction
/* * Read positive numbers from stdin, store them in an array. Print out the * array when done. !With bounds checking! */ #include <stdio.h> int main() { int i = 0; /* Array index for input. */ int j = 0; /* Array index for output. */ double x; /* Input value */ double a[5]; /* Array to hold numbers */ /* * Prompt and read first number. */ printf("Input positive numbers, negative to stop: "); scanf("%lf", &x); /* * Loop until negative number input, or we run out of room in the array, * which ever occurs first. */ while (x > 0 && i < 5) { /* * Store input in next array element. */ a[i] = x; /* * Increment array index. */ i = i + 1; /* * Read in next number. */ scanf("%lf", &x); } /* * Print out the array. */ while (j < i) { printf("a[%d] = %f\n", j, a[j]); j++; } return 0; }