K. N. King C Programming - Chapter 4. Exercises, Programming Projects

[Week 3] Hacking Cat’s Weekly Study


Exercises

Section 4.1 (1)

  • (a) 1 2
  • (b) 0
  • (c) 1
  • (d) 0

Section 4.1 (2)

  • (-i)/j, -(i/j) 계산 결과는 항상 동일하다. (계산 전 -1 곱하는 것과 계산 결과에 -1 곱하는 것은 동일한 결과를 출력한다.)

Section 4.1 (3)

C89 표현식에서는 / % 연산 시 피연산자(operand)에 음수 값이 할당될 경우 올림 혹은 내림이 일어날 수 있다.

  • (a) 1
  • (b) -1 or -2 (-1.6)
  • (c) -1 or -2 (-1.6)
  • (d) 1 or 2 (1.6)

Section 4.1 (4)

C99 표현식에서는 나눗셈(/ %)의 결과가 언제나 0과 가장 가까운 값을 갖는다.

  • (a) 1
  • (b) -1
  • (c) -1
  • (d) 1

Section 4.1 (5)

  • 5번 - C89
    • (a) 3
    • (b) -3 or 2 (몫 -2, 나머지 2 혹은 몫 -1, 나머지 -3)
    • (c) -3 or 2 (상동)
    • (d) -3 or 2 (상동)

Section 4.1 (6)

  • 6번 - C99 ( C99에서 나누기의 결과는 항상 0을 향해 버림을 수행하며, 결과는 제수의 부호를 따른다. )
    • (a) 3
    • (b) -3
    • (c) 3
    • (d) -3

Section 4.1 (3,4,5,6) 참고자료


Section 4.2 (9)

  • (a) 63 (i *= j + 1 i * (j+1))
  • (b) 3 2 1 (i += j += kj = j+k; i = i+j;)
  • (c) 2 -1 3 (i -= j -= k;j = j-k; i = i-j;)
  • (d) 0 0 0 (i *= j *= k;j = j*k; i = i*j;)

Section 4.2 (10)

  • (a) 12 12
  • (b) 3 4
  • (c) 2 8
  • (d) 6 9

Section 4.3 (11)

  • (a) 01
  • (b) 411 6
  • (c) 08 7
  • (d) 34 5 4

Section 4.3 (12)

  • (a) 6 16
  • (b) 6 -7
  • (c) 6 23
  • (d) 6 15

Section 4.3 (13)

  • ++i 표현식이 (i += 1); 과 동일하다. (i++ 는 값의 증가 시점이 다르다.)

Section 4.4 (14)

  • (a) ((a * b) + ((-1) * (c * d)) + e)
  • (b) (((a / b) % c) / d)
  • (c) (((-1) * a) + ((-1) * b)) + c + ((-1) * d)
  • (d) (a * + ((-1) * b) / c) + (-1) * d)

Section 4.5 (15)

  • i, j 의 값이 각각 1, 2일 때 (a~d) 표현식 실행 후 i, j 의 값을 구해라
    • (a) i: 3, j: 2
    • (b) i:2, j: 2
    • (c) i: 2, j: 2
    • (d) i: 2, j: 3

Programming Projects

Programming Projects (1)


Programming Projects (2)


Programming Projects (3)


Programming Projects (4)


Programming Projects (5)

#include <stdio.h> 
 
int main(void) { 
	int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; 
	printf("Enter the first (single) digit: "); 
	scanf("%1d", &d); 
	printf("Enter first group of five digits: "); 
	scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); 
	printf("Enter second group of five digits: "); 
	scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); 
	first_sum = d + i2 + i4 + j1 + j3 + j5; 
	second_sum = i1 + i3 + i5 + j2 + j4; 
	total = 3 * first_sum + second_sum; 
	printf("Check digit: %d\n", 9 - ((total - 1) % 10)); 
	
	return 0; 
}


Programming Projects (6)


🐛.. 🐛.. 🐛..