1. Definitions can be found in the notes/book. 2. Assume: X = 2; Y = 7; Z = 9; A = 0; B = 1; What are the values of: X + Y * Z * 65 X * Y + Z * 23 X - Y - Z * -14 X / Y * Z * 0 Y % X * 1 sqrt(Z) * 3.0 pow(X,Y) * 128.0 rint(7.6) * 8.0 floor(7.6) * 7.0 ceil(7.6) * 8.0 X + Y - Z * X + Z - X % Y * -2 X && Y * 1 (true) A || B * 1 (true) !A * 1 (true) !A && B * 1 (true) !B || A * 0 (false) (X < Y) && Z > X * 1 (true) 3. A field specification may have width and precision values, how are these used? * In formatted output commands, the width value indicates how * many characters should be used in outputting the value. For * example, the specification %5d indicates that the value should * be printed as a whole number using a total of 5 characters. * If the minimum width needed is larger than the value specified * the minimum width is used. In formatted input commands the * width value indicates the maximum number of characters to be * read while reading the value. * * In formatted output, the precision value is used to indicate * how many digits should appear after the decimal point when * printing out floating-point values. 4. What is meant by the term prompting for input? What are characteristics of a good prompt. Show an example. * Prompting for input involves using an output command to inform * the user that the program needs to input 1 or more values. A * good prompt will tell the user precisely what type of value is * expected and what values are reasonable. A good prompt may also * repeat the prompt until the user enters a correct value. * Example: * * do { * printf("Please enter a calendar month to print (use integers 1 to 12): "); * scanf(&month); * if ((month < 1) || (month > 12)) * printf(" Please use a value between 1 and 12!\n"); * } while ((month < 1) || (month > 12)); 5. The logical AND (&&) and logical OR (||) operators are "short cicuit" operators. What does this mean? * This means that these operators will not evaluate their second * argument if the first argument determines the value of the * operator. For the && operator, if the first argument is 0 (false) * the second argument is not evaluated. For the || operator, if the * first argument is 1 (true) the second argument is not evaluated. 6. Write a function Factorial that takes an argument N and calculates (returns) 1 * 2 * 3 * 4 * ... * N. For example, Factorial(4) should calculate 24 (1 * 2 * 3 * 4). * int Factorial(int N) { * int I; * int fact = 1; * * for (I = 1; I <= N; I++) * fact *= I; * * return fact; * } 7. Rich wants to write a function that prompts a user for three pieces of information: an id number (integer), a salary (floating-point) and a favorite letter (character). Help Rich out and write this function. Your function should prompt the user for each value and the function should produce the three values. * void ReadData(int *id, float *salary, char *favoriteletter) { * printf("Please enter your id#: "); * scanf(id); * printf("Please enter your salary: "); * scanf(salary); * printf("Please enter your favorite letter: "); * scanf(favoriteletter); * } 8. Give an example of a switch statement with at least three cases and a default case. Now, rewrite your switch statement as a set of if-else statements. * switch (num) { * case 1: case 2: * printf("Case 1 or 2\n"); * break; * case 3: * printf("Case 3\n"); * break; * case 4: * printf("Case 4\n"); * break; * default: * printf("All other cases.\n"); * } * * if ((num == 1) || (num == 2)) * printf("Case 1 or 2\n"); * else if (num == 3) * printf("Case 3\n"); * else if (num == 4) * printf("Case 4\n"); * else * printf("All other cases.\n"); 9. Give an example of a multiway selection problem that would be difficult to do with a switch. * It is difficult to do multiway selection when the number of cases * corresponding to a single action is very large. For example, in the * following situation * * Grade Message * ------ ------- * 60-100 Good * 53-59 Borderline * 0-52 Problem * * the programmer would need to type 41 case labels for the first action * (showing the message Good), 7 case labels for the second action, * and 53 case labels for the last action. It would be much easier * to do this with nested if-else statements. 10. Translate the following for loop into a do-while loop: for (I = 0, tot = 0, cnt = 0; I < N; I += 2) { tot += I; cnt++; printf("%.2f\n",(float) tot / cnt); } * I = 0; * tot = 0; * cnt = 0; * if (I < N) { * do { * tot += I; * cnt++; * printf("%.2f\n",(float) tot / cnt); * I += 2; * } while (I < N); 11. Write a piece of code to produce the following output using at most one newline character in your print statements: * ** *** **** *** ** * * for (I = 0; I < 7; I++) { * for (J = 0; J < (3 - abs(I - 3)); J++) * printf(" "); * for (J = 0; J <= (3 - abs(I - 3)); J++) * printf("*"); * printf("\n"); * } 12. What is the output of the following piece of code: for (I = 0; I < 12; I++) { for (J = 4 - (I % 4); J > 0; J--) printf(" "); for (J = 0; J < (I % 4) + 1; J++) printf("X"); printf("\n"); } * X * XX * XXX * XXXX * X * XX * XXX * XXXX * X * XX * XXX * XXXX 13. Give a piece of code that will copy the characters from a text file file1.txt to a new file file2.txt. Furthermore, when copying the characters, any lower case characters in file1.txt should be written as a corresponding upper case character in file2.txt (if the character 'a' is read from file1.txt, the character 'A' should be written to file2.txt). * FILE *inf; * FILE *outf; * int ch; * * if ((inf = fopen("file1.txt","r")) == NULL) { * printf("Unable to open file file1.txt\n"); * exit(-1); * } * * if ((outf = fopen("file2.txt","w")) == NULL) { * printf("Unable to open file file2.txt\n"); * exit(-1); * } * * while ((ch = fgetc(inf)) != EOF) * if ((ch >= 'a') && (ch <= 'z')) * fputc(ch + 'A' - 'a',outf); * else * fputc(ch,outf); 14. How do the fgetc and ungetc functions work? Indicate the arguments each takes and what values they return. Also, give an example of how each function is used. * The fgetc function takes one argument, a FILE * and returns the * next available character in the file the FILE * is connected to * or the value EOF to indicate no character is available (the * End-Of-File has been reached). The return value of fgetc is an * integer value to allow the computer to represent not only any * legal character value but also to represent EOF. The ungetc * function takes an argument containing the ASCII value of a * character (an integer) and a FILE * and returns that character to * the stream (the FILE *) to which it is connected. ungetc returns * EOF if the command fails. The returned character would be the * next character read by an fgetc or getc command. Note that you * may not do two ungetc commands in a row. * * fopen sample: * if ((inf = fopen("file1.txt","r")) == NULL) { * printf("Unable to open file file1.txt\n"); * exit(-1); * } * * ungetc sample: * if ((ch >= '0') && (ch <= '9')) * ungetc(ch,inf); 15. Output: * 5 4 * 4 3 * 4 5 * 4 3 * 2 2 * 4 2 * 1 3 * 3 3 * 4 3 7 * 7 3 * 3 4 7 * 7 3 * 4 3 7 * 4 7 * 3 4 7 * 4 7 * 144 12 12 * 144 12 * 144 12 12 * 12 144 * 16 16 16 * 16 3 * 9 9 9 * 4 9 * 60 6 10 70 * 60 70 * 60 6 10 70 * 4 60 * 66 6 11 77 * 66 3 * 66 6 11 77 * 77 66 * 66 6 11 77 * 66 3 * 66 6 11 77 * 77 66 * 60 6 10 70 * 60 70 * 60 6 10 70 * 4 60