/* * Assignment * Chapter 7 * John Stile */ 1. Explain what the follwoing accomplishes and how it works. while (getchar() != '\n' ); // read from stdin // evalue if not newline // do again 2. Using English phrases rather than C code, sketch out the logic of a program that reads from stdin until it finds an EOF, coutning all instances of the digit 4, the character s, and the character = in the input. However, if it finds a letter Q, processing stops and the program exits. 3) Compare and contrast the follwing: while, do/while, switch, for, if, break, and continue. Answer: while tests first, executes block, reruns test. do/while executes bock, runs test, returns to block. switch tests, executes block, exists switch with break. for sets a counter, runs a test, executes block, then increments, and repeat. if tests first, executes block, and finishes. block only runs once. break is used to go to the end of a while, do/while, case/switch 4) In the SELF TEST 2, you wrote a program called factor.c that calcuated the factorsials from 1 to 10. Explain how each aspect of the code works. Ansewr: #include // include stdio.h functions when compiling #define NUM 10 // define a macro, where NUM will be reaplced by 10 int main(void) // header for function main { int xx,yy ; // declare 2 intigers float factorial; // declare a float for ( xx=1; xx<=NUM; xx=(xx+1) ) // outer loop, count from 1 to 10 { factorial=1; // zero out factorial printf("%d!=\t",xx); // first part of output, current number for (yy=xx; yy>0; yy=(yy-1) ) // cycle through 1 to current xx { printf("%d*", yy); // print each yy with a * after the digit factorial=(factorial*yy); // calcuate the factoria by adding another mutilplyer each iteration } printf("\b"); // print the backspace, this removes the * from the final yy for this iteration. printf(" = %.0f\n", factorial); // print the final result } return 0; // return 0 to calling function }