/********************************************************************************
Treibergs March 6, 2006
Solution to HW4a. Read the file of teacher data by state & commpute the
national totals.
The first problem is reading the file. By viewing the file in a text editor
program, we see that the first 20 records (lines ending at \n) are description
and then the next 51 have the state name followed by eight data fields.
The state names may have one or two words, but all of the names have 10
characters. Following the pattern in the sample program from week 7 examples,
we read the record into a string, and then read the variables. The first
twenty lines are read and not used for anything. Then the instrings are
read into the variables by the sscanf. The variable "state" is a character
array of 10 characters. This is read by the format specifier "%10c"
which means that the first 10 characters are read into the 10 characters of
the variable "state," including blanks. (If we used %10s then a blank would
have caused sscanf to go to the next variable in the input list, "x1."
For convenience, we enter the remaining data fields into integers x1 to x8.
(To get this running, I read the file and printed the inputs until
they were being read correctly.) The low-tech solution would have been to
edit the data file in a text editor, throwing away the parts of the file
which are not read, and then let your program read in the modified file.
The population variables pop=x4, pop517=x5, pubk8=x7, pub912=x8 are simply
summed over all states. The sum variables are called tpop, t517, tk8 and t912.
Insch=x6=x7+x8 so that the total of insch is tinsch=tk8+t912. The other variables
cannot be summed. For example the national expenditure per pupil should be the
national total expenditure, texp, divided by total number of pupils, tinsch.
However, the expenditure on public schools in each state is the expenditure per
pupil times the number of pupils, or x1 * x6. These are summed to get texp.
The national average teacher salary is the national total spent on teacher wages
divided by the number of teachers. The number of teachers per state is the
number of pupils divided by the ratio of pupils per teacher, or x1/x6, which we
compute in double precision. The sum is the national number of teachers, t.
Similarly, the national total on teacher salary, tlab, is the sum of the money
spent on teacher wages in each state, or the average teachers wage times the
number of teachers, or x2 * x6 / x3, which is computed in double precision.
Then the national average teachers wage is t2 = tlab/t. Finally, the national
pupil per teacher ratio is the number of pupils divided by the number of teachers,
or t3 = tinsch/t.
*********************************************************************************/
# include
# include
int
main(void)
{
int n=0,icount=0, lines=0, m=0;
char instring[100], state[10];
long int x1,x2,x3,x4,x5,x6,x7,x8, t1, t2, t3;
long int tpop=0, t517=0, tinsch=0, tk8=0, t912=0;
double tlab=0.0, t=0.0, texp=0.0;
FILE *fileptr;
fileptr = fopen( "SchoolData.txt" , "r");
if ( fileptr == NULL )
{
printf ( "Unable to open file \"%s\" \n","SchoolData.txt" );
exit ( 8 );
}
printf( "Reading data from \"%s\"\n\n","SchoolData.txt");
printf( "State Exp/Pup TSal Pup/Teac Pop Pop15-17 InSch PubK-8 Pub9-12\n" );
while( !feof(fileptr) )
{
icount++;
fgets ( instring, 100, fileptr);
if ( icount <= 20)
continue;
m=sscanf ( instring, "%10c%ld %ld %ld %ld %ld %ld %ld %ld ",
state, &x1,&x2,&x3,&x4,&x5,&x6,&x7,&x8 );
if( m == 9)
{
printf ( "%.10s%7ld%7ld%7ld%7ld%7ld%7ld%7ld%7ld\n",
state, x1,x2,x3,x4,x5,x6,x7,x8 );
lines++;
tpop = tpop + x4;
t517 = t517 + x5;
tk8 = tk8 + x7;
t912 = t912 + x8;
texp = texp + (double)x1*x6;
t = t + (double)x6/x3;
tlab = tlab + (double)x2*x6/x3;
}
}
fclose(fileptr);
printf(" %d records read. %d scanned OK.\n", icount, lines);
tinsch = tk8 + t912;
t1 = texp/tinsch;
t2 = tlab/t;
t3 = (double)tinsch/t;
printf ( "%.10s%7ld%7ld%7ld%7ld%7ld%7ld%7ld%7ld\n", "Nation ",
t1,t2,t3,tpop,t517,tinsch,tk8,t912 );
return EXIT_SUCCESS;
}
/********************************************************************************
Treibergs March 6, 2006
Solution to HW4b. find the GCD of three numbers a,b,c.
One of the numbers has to be nonzero, say a. Then GCD(a,b,c)=GCD(GCD(a,b),c).
Let d=GCD(GCD(a,b),c). To see that d=GCD(a,b,c) we have to show both that
1) If e divides a, b and c then e divides d (hence d is greatest of all
divisors.)
2.) d divides a, b and c.
To see (1), suppose e divides a, b and c. As e divides both a and b it
must also divide GCD(a,b), which is the greatest of divisors of both. But as
e divides both numbers GCD(a,b) and c, it also divides their GCD, so e
divides GCD(GCD(a,b),c) = d, as desired.
To see (2), as d is a common divisor of GCD(a,b) and c, it divides both
numbers: d divides c and d divides GCD(a,b). Now, a number that divides
the GCD of two numbers is a factor of both numbers, in other words, d divides
a and d divides b, as to be shown.
We copy the GCD routine for two numbers, and employ it to get the GCD of three.
We need only check that one of the numbers is nonzero in calling gcd ( a, b ).
*********************************************************************************/
# include
# include
int
gcd( int q, int p )
{
int r;
do
{
r = p % q;
p = q;
q = r;
}
while ( r != 0);
return p;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int
main(void)
{
int a, b, c, g;
printf ( "\nGCD of Three Numbers\n\n Please enter three integers :" );
scanf ( "%d %d %d", &a, &b, &c );
a = abs ( a );
b = abs ( b );
c = abs ( c );
if( a + b + c != 0 )
{
if (a + b > 0)
g = gcd ( gcd (b, a), c);
else
g = gcd ( gcd (c, a), b);
printf ( " The GCD of %d, %d and %d is %d.\n", a, b, c, g );
if ( g == 1)
printf ( "The numbers are relatively prime.\n" );
}
else
printf ( "Bad Input: At least one of the numbers must be nonzero.\n" );
return EXIT_SUCCESS;
}