/* * SelfTest2 * Chapter8 * By John Stile */ 1. Consider the following program: #include // include functions in stdio.h float squareit(float); // prototype: takes float, returns float. int main() // header for funciton main { float aa, bb=8; // declare two floats, initialze bb to 8 aa=squareit(bb); // send value of bb to funciton squareit() // assign the result to mem location of aa printf("%.1f\n",aa); // output aa as a float return 0; } float squareit(float zz) { return zz*zz; } 1a. What is the return type of quareit()? Answer: float 1b. What is the type of squareit()'s formal argumnet? Answer: float 1c. What statements comprise the body of the squareit() function? Answer: return zz*zz; 1d. If the value 8.0 was passed to squareit();, what value would this function return? Answer: 64.0 2. Determine an appropriate function prototype for the following functions, given xx='A' yy=6.3 zz=48 2a. printf("The value is %d\n", functionsA(zz,yy,xx); Answer: int functionsA( int, float, char ); 2b. xx=functionB(x,y); Answer: char funtionB(char, float); 2c. functionC(); Answer: void functionC(void); 2d. functionD(functionB(32.0)); Answer: void functionD(char); 3a. Write a character function called tranq() that takes a character as it's argument, and returns a space if the character is a '?'. Otherwise it returns the origninal character. Answer: Prototype: char tranq(char); Function: char tranq(char cc) { if ( cc == '?' ) { return ' '; } else { return cc; } } 3b. Write a main function that tests the funciton tranq(). Have it request a letter, call the function rranq(), and then print the value. Answer: int main(void) { char cc; printf("pelase enter a character and press enter\n"); //prompt for input scanf("%c", &cc); // read stdin, interpret as char, assing to cc printf("%c\n", tranq(cc) ); return 0; } 4a. Write a function named maxnum() that takes threee integers xx, yy, zz and returns the maximum number. Answer: Prototype: int maxnum(int, int,int); Function: int maxnum(int xx, int yy,int zz) { int the_max; if ( xx > yy ) the_max=xx; else the_max=yy; if( zz > the_max ) the_max=zz; return the_max; } 4b. Write a main function that tests the function maxnum(). Have it request three integers. Call the funciton maximum(), pass the numbers to it and print the maximum value returned from the function. Answer: int main(void) { int xx=yy=zz=0; printf("Enter 3 digits separated by spaces: "); scanf("%d%d%d", xx, yy, zz); printf("The maximum number is: %d\n", maxnum(xx,yy,zz) ); return 0; } 5. Add all these functions to your myfunctions.c file and use them in a new program.