// Final //============================================================================= // Question 1. // Problem: // Consider the following program. // Explain the significance of the following items in the context of the program. // #include // include headers for functions in stido.h #define LETTER 'C' // preprocessor search and replace LETTER with letter 'C' int main(void) // header for main. takes nothing, returns an int. { // begin main block char x; // delcare variable named x that can hold a character. printf("\nAre you having fun with %c?(y/n): ", LETTER); // Prompt user for input // Print new line, and text between "" // In putput replcae %c with letter 'C' x = getchar(); // read from stdin // assign to variable x if( x=='y') // does X contain the the character 'y' // if yes, do block. // if no, skip block { // enter 'if' block puts("You should be, you worked hard enough"); // Print content between quotes puts("Enjoy demonstrating mastery..."); // Print content between quotes } // end 'if' block puts("Sorry, Hope the rest is better!"); // Print content between quotes // NOTE: this block gets printed no matter what. return 0; // return number 0 to calling function/shell } // end main block //============================================================================= // Question 2. // Problem: // Short Answer. What do each of the follwoing mean? a=6; // assign value of 6 to variable a // This is a complete statement. a==6 // compare if variable a contains the value of 6. If so, result is TRUE, if not resule is FALSE or 0. // This is used in an evaluation sturucte, like if() or while(), and does not appear by it self. x=&a // Assign the address of variable a to value inside variable x. x must be a pointer. *xx // looks at value stored at memory address stored in variable xx. aa[6] // the 7th element of an array. int calc(int); // prototype for function calc. The ';' at the end is the give away. // Takes an int and returns an int. calc(37) // Call the function calc, feeding it number 37. // Will result in an int returned from funciton. int calc(int y) // header for function calc. // assing int passed to the function to local int variable y // function returns an int. void calc( int * y); // prototype for function calc, The ';' at the end is the give away. // takes an int pointer (which may or may not be assigned to local variable named y. // returns nothing. if (abc % 2 == 0 ) // % has priority over ==, so // divide value stored in abc by 2, // ask if divistion remainder is zero. // If so, if() evaluates to true. // If not, if() evaluates to FALSE or 0. c=(float) a/b; // Cast a to a float // divide a by b // Assign the resulting float to c float ave(int, int); // prototype for function ave. The ';' at the end is the give away. // Takes 2 ints, returns a float fprintf( xp, "%s %s", class building); // xp is a pointer to a file // Replace first %s with contents of array class, up to element containing \0 // Replace second %s with ontents of array building, up to element containing \0 // write to the file pointed to by xp while((c=getchar()) != EOF) // read one character from stdin, // assign to character to variable c // evaluate if variable c does not contains end of file // If not, while evaluates to TRUE or 1 // If it does, while evaluates to FALSE or false. if(x>=y) // evaluate if contents of x is greater than, or equal to, value stored in y // If yes, if evaluates to TRUE or 1. // If not, if evaluates to FALSE or 0. a=b=c=d=f=0; // initialize content of variables a, b, c, d, and f the number 0. break; // exit loop, and do not iterate again. continue; // exit this iteration of the loop, and go to the next iteration of the loop. if(d >= 'a' && d <= 'z') // evaluate if d contains is an ascii value greater than or equal to 'a' // if not, if() evaluates to FALSE // if true, valuate if d contains is an ascii value less than or equal to 'z' // if not, f() evaluates to FALSE // if ture, f() evaluates to TRUE for(x=20; x<60; x+=2) // initialize x a value of 20. // evaluate if x contains a value less than 60 // if ture, due block // if false, skip block // After block incremnt value stored in x by 2 (i ? getc() : bye()) // evaluate if contents of variable i is non-zero or zero. // If non-zero, run function get(), and outer () contains the value returned from getc() // If zero, run bye(), nd outer () contains the value returned from bye() if( ( fp=fopen(bbbb,"w") )==NULL) // fopen tries to open file named in variable bbbb for writing // if file can be opened for writing, return address of beginning of file. // if not fopen returns NULL // Assign result of fopen to pointer fp. // Evaluate if pointer fp contains NULL // if true, if() evalutates to TRUE, and do block // if false, if() evaluates to FALSE, and skip block fclose(fp); // close the file pointer fp. File handle is closed abcd(&x) // call function abcd, passing address of variable x. *y=60 // assign value of 60 to address pointed to by pointer variable y *y=*y+*y+*y // Start from the right and work our way to the left. // multipy thing at address stored in y by it self 3 times. // Assign the result to address stored y. // Result is cubing value stored at address pointed to by y. //====================================================================== // Question 3. // Problem: // Consider the following program: #include float int_average(int, int); // Prototype for function int_average // takes 2 ints // returns a float. int main(void) { int a=5; int b=3; float avrg; printf("%d\n", a); puts("This program doesn't take the average two numbers!"); avrg=int_average(a,b); printf("The result is %f.\n", avrg); return 0; } float int_average(int a, int b) // Header for function int_average // assign first number passed to local variable a // assign second number passed to local variable b // Returns a float { a=a+2; return(a+b)/2.0; } Question3a. Identify which lines in the above program are function prototypes and which are headers. Answer3a. prototypes: float int_average(int, int); header: float int_average(int a, int b) Question3b. Extract what is passed to the user-defined function int_average()? Answer3b. The line in question: avrg=int_average(a,b); The value stored in a and b are passed to the funciton. In the function header these values are assigne to varaibles a and b which are local to the funciton. The variables a and b in main are different entities from a and b in int_average. Question3c. What is returned by the user-defined function int_average()? What does main do with whatever is returned? Answer3c. The user-defined function int_average() returns the average between the value of a and b in the form of a floating point number. Two int's (a and b) are summed, then divided by a float, which casts the result to a float. The resulting float is returned to main. In main the result of the function int_average() is assigned to floating point variable avrg, and printed. Question3d. Give all lines of output that the program generates. Answer3d. 5 This program doesn't take the average two numbers! The result is 5.000000. //====================================================================== Question4. Write a program that computes the age of the user. An approximate answer, based on the year of the person's birth, is required. The program should: a) Ask user to input the year of his/her birth. b) Compare the input year with the current year. If the input year is later than the current year, responly with ONLY an error message. If the input is valid, print out the user's age to the screen. c) Use a preprocessor command to define the current year such that the program can easily modify from year to year.