C Exercise I 1. Write a program that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise. 2. Write a general and robust version of a program that prints the roots of the polynomial ax2 + bx + c, prints an appropriate error message if the discriminant is negative, and behaves appropriately (avoiding division by zero) if a is zero. 3. What (if anything) is wrong with each of the following statements? a. if (a > b) then c = 0; b. if a > b { c = 0; } c. if (a > b) c = 0; d. if (a > b) c = 0 else b = 0; 4. Write a code fragment that prints true if the double variables x and y are both strictly between 0 and 1 and false otherwise. 5. Suppose that i and j are both of type int. What is the value of j after each of the following statements is executed? a. for (i = 0, j = 0; i < 10; i++) j += i; b. for (i = 0, j = 1; i < 10; i++) j += j; c. for (j = 0; j < 10; j++) j += j; d. for (i = 0, j = 0; i < 10; i++) j += j++; 6. Write a program that takes the number of lines to print as a command-line argument. And, then output the following to the screen when the number is 4: 1st Hello 2nd Hello 3rd Hello 4th Hello You may assume that the argument is less than 1000. Hint: consider using i % 10 and i % 100 to determine whether to use "st", "nd", "rd", or "th" for printing the ith Hello. 7. Write a program that, using one for loop and one if statement, prints the integers from 1000 to 2000 with five integers per line. Hint: use the % operator. 8. Anagrams Given a 4-character string as a command line input, print all anagrams that can be generated from it. $ ./anagram abcd abcd abdc acbd acdb bacd ... Hint: argv[1][2] is the 2nd character in the input string. 1 9. Factorials Write two C programs fact1.c and fact2.c that calculate the factorial of a number that will be passed to your program as a command line argument. fact1.c should use recursive function calls to compute the answer, while fact2.c should not make any recursive calls and should use loops instead. After your programs are compiled, the user should be able to run them as follows: > ./fact1 0 1 > ./fact1 6 720 > ./fact2 4 24 > ./fact2 8 40320 10. Zipper zipper.c will take-in multiple command-line arguments and zip them together. The output of the program will be several strings printed onto the screen, one per line. The length of each output string will be equal to the number of command-line arguments passed to the program. The output strings are constructed as follows: • the first string is composed of the first characters of all the command-line arguments (in order). • the second string is composed of the second characters of all the command-line arguments (in order). . . . and so on. The number of output strings is equal to the length of the longest passed-in argument. If the passedin arguments are of different lengths, then after exhausting the letters of a shorter argument, an empty space will printed in its slot. The following examples should make things clearer: 2 > ./zipper aa bb ab ab > ./zipper abcd efgh ae bf cg dh > ./zipper hello green 12345 hg1 er2 le3 le4 on5 > ./zipper hi hello bye hhb iey le l o > ./zipper ThisIsOk T h i s I s O k 3
© Copyright 2024 ExpyDoc