Starting from:

$25

COMP1400-Assignment 2 Solved

1.   Modify the addfrac.c program of Section 3.2 so that the user enters both fractions at the same time, separated by a plus sign. Save the program in a file named as a2 addfrac.c, and submit the file as your solution to this question.

Enter two fractions separated by a plus sign: 5/6+3/4 The sum is 38/24

2.    Show the output produced by each of the following program fragments. Assume that i, j, and k are int variables.

(a)    i = 5; j = 3; printf("%d %d", i / j, i % j);

(b)   i = 2; j = 3;

printf("%d", (i + 10) % j);
(c)    i = 7; j = 8; k = 9; printf("%d", (i + 10) % k / j);

(d)   i = 1; j = 2; k = 3; printf("%d", (i + 5) % (j + 2) / k);
3. Write a program to read an integer entered by the user and display it in octal (base 8). Save the program in a file named as a2 octal.c, and submit the file as your solution to this question.

Enter a number between 0 and 32767: 1953

In octal, your number is: 03641

The output should be display using five digits, even if fewer digits are sufficient. Hint: To convert the number to octal, first divide it by 8; the remainder is the last digit of the octal number (1, in this case). Then divide the original number by 8 and repeat the process to arrived at the next-to-last digit. (printf is capable of displaying numbers in base 8 as we will see in Chapter 7, so there is actually an easier way to write this program.)

4. The following program fragments illustrate the short-circuit behavior of logical expressions. Show the output produced by each, assuming that i, j, and k are int variables.

(a)    i = 3; j = 4; k = 5; printf("%d ", i < j || ++j < k); printf("%d %d %d", i, j, k);

(b)   i = 7; j = 8; k = 9; printf("%d ", i - 7 && j++ < k); printf("%d %d %d", i, j, k);
(c)    i = 7; j = 8; k = 9; printf("%d ", (i = j) || (j = k); printf("%d %d %d", i, j, k);

(d)   i = 1; j = 1; k = 1;

printf("%d ", ++i || ++j && ++k); printf("%d %d %d", i, j, k);
5. Create a RAPTOR flowchart that finds the largest and smallest of four integers entered by the user. Save the flowchart in a file named as a2 4integers.rap, and submit the file online as your solution to this question.

Enter 1st integer: 21

Enter 2nd integer: 43

Enter 3rd integer: 10

Enter 4th integer: 35

Largest: 43

Smallest: 10

Use as few Selection symbols as possible. Hint: Four Selection symbols are sufficient.

6.   Write an equivalent C program that accomplishes what the a2 4integers.rap flowchart does, with a small modification to combine the four input statement into one as below:

Enter four integers: 21 43 10 35

More products