Start Lecture #2
We are hindered in our examples because we don't know how to input anything other than characters and don't want to write the program to convert a string of characters into an integer or (worse) a floating point number.
#include <stdio.h> #define N 10 /* imagine you read in N */ #define MAXN 1000 main() { int i; float x, sum=0, mu; for (i=0; i<N; i++) { x = i; /* imagine you read in X[i] */ sum += x; } mu = sum / N; printf("The mean is %f\n", mu); }
#include <stdio.h> #define N 10 /* imagine you read in N */ #define MAXN 1000 main() { int i; float x[MAXN], sum=0, mu; for (i=0; i<N; i++) { x[i] = i; /* imagine you read in X[i] */ } for (i=0; i<N; i++) { sum += x[i]; } mu = sum / N; printf("The mean is %f\n", mu); }
#include <stdio.h> #include <math.h> #define N 5 /* imagine you read in N */ #define MAXN 1000 main() { int i; double x[MAXN], sum=0, mu, sigma; for (i=0; i<N; i++) { x[i] = i; /* imagine you read in x[i] */ sum += x[i]; } mu = sum / N; printf("The mean is %f\n", mu); sum = 0; for (i=0; i<N; i++) { sum += pow(x[i]-mu,2); } sigma = sqrt(sum/N); printf("The standard deviation is %f\n", sigma); }
I am sure you know the formula for the mean (average) of N numbers: Add the number and divide by N. Call the mean μ
The standard deviation is the RMS of the deviations-from-the-mean, it is called σ. That is, σ = sqrt(Σ((Xi-&mu)2)/N). (For technical reasons, sometimes we divide by N-1 not N.)
The first program on the right is the natural program to read in N then read N number and compute the mean of the latter, with one problem. We don't know how to read numbers.
So I faked it by having N a symbolic constant and making x[i]=i.
<soapbox>
You would presumably not write the second version, with a
gratuitous array.
It takes more space, is (a little) longer, is slower, and is more
complicated.
However, in 202 students want to write it when doing my linker lab.
There is an instinct to use a three step procedure for all
assignments:
But that is silly if you only need the value once as in this
example.
</soapbox>.
The last example is a good use of arrays for computing the standard deviation using the RMS formula above. We do need to keep the values around after computing the mean so that we can compute the deviations and from them the standard deviation.
Note that, unlike Java, no use of new (or the C analogue malloc()) appears. Arrays declared as in this program have a lifetime of the routine in which they are declared. Specifically sum and x are both allocated when main is called and are both freed when main is finished.
Note the declaration int x[N]. In C, to declare something complicated, you say what has to be done to get one of the primitive types.
In C if we have int X[10]; then writing X in you
program is the same as writing &A[0].
& is the address of
operator.
More on this later when we discuss pointers.
#include <stdio.h> // Determine letter grade from score // Demonstration of functions char letter_grade (int score) { if (score >= 90) return 'A'; else if (score >= 80) return 'B'; else if (score >= 70) return 'C'; else if (score >= 60) return 'D'; else return 'F'; } // end function letter_grade char letter_grade2 ( int score ) { switch ( score ) { // yes, this is an awful case statement! case 90 : case 91 : case 92 : case 93 : case 94 : case 95 : case 96 : case 97 : case 98 : case 99 : case 100 : return 'A'; break; default : return 'F'; break; } // end switch score } // end function letter_grade2 main() { int quiz; char grade; quiz = 95; grade = letter_grade( quiz ); printf(" \n for a score of %3d the grade is %c\n ", quiz, grade); quiz = 75; grade = letter_grade2( quiz ); printf(" \n for a score of %3d the grade is %c\n ", quiz, grade); } // end main
#include <stdio.h> // Demo of Arrays // Create random numbers, average and sort them #define maxelements 50 void sort ( int stuff [], int max ) { int temp; int x, for (x=0; x<(max-1); x++) { for (y=0; y<(max-(x+1)); y++) { if (stuff[y] < stuff[y+1]) { temp = stuff[y]; stuff[y] = stuff[y+1]; stuff[y+1] = temp; } // end if stuff } // end for y } // end for x } // end function sort float avg ( int stuff [], int max ) { int sum; int x; sum = 0; for (x = 0; x < max; x++) sum = sum + stuff[x]; return (sum / max); } main() { int table[maxelements]; int x; float average; for (x = 0; x < maxelements; x++) table[x] = rand(); for (x = 0; x < maxelements; x++) printf(" the element in position %3d is %3d \n", x, table[x]); average = avg( table, maxelements ); printf(" the average value is %5.1f ", average); sort( table, maxelements ); for (x = 0; x < maxelements; x++) printf(" the element in position %3d is %3d \n", x, table[x]); } // end function main