initial commit

master
Peter Babič 4 years ago
commit 2960058ed9
Signed by: peter.babic
GPG Key ID: 4BB075BC1884BA40
  1. BIN
      02_C_Fundamentals/Debug/02_C_Fundamentals
  2. BIN
      02_C_Fundamentals/Debug/main.o
  3. 43
      02_C_Fundamentals/Debug/makefile
  4. 17
      02_C_Fundamentals/Debug/sources.mk
  5. 24
      02_C_Fundamentals/Debug/subdir.mk
  6. 227
      02_C_Fundamentals/main.c
  7. BIN
      03_Formatted_IO/Debug/03_Formatted_IO
  8. BIN
      03_Formatted_IO/Debug/main.o
  9. 43
      03_Formatted_IO/Debug/makefile
  10. 171
      03_Formatted_IO/main.c
  11. BIN
      04_Expressions/Debug/04_Expressions
  12. BIN
      04_Expressions/Debug/main.o
  13. 43
      04_Expressions/Debug/makefile
  14. 248
      04_Expressions/main.c
  15. BIN
      05_Selection_Statements/Debug/05_Selection_Statements
  16. BIN
      05_Selection_Statements/Debug/main.o
  17. 43
      05_Selection_Statements/Debug/makefile
  18. 619
      05_Selection_Statements/main.c
  19. BIN
      06_Loops/Debug/06_Loops
  20. BIN
      06_Loops/Debug/main.o
  21. 43
      06_Loops/Debug/makefile
  22. 523
      06_Loops/main.c
  23. 43
      07_Basic_Types/Debug/makefile
  24. 637
      07_Basic_Types/main.c
  25. 43
      08_Arrays/Debug/makefile
  26. 834
      08_Arrays/main.c
  27. 24
      09_Functions/Debug/Examples/countdown/subdir.mk
  28. 24
      09_Functions/Debug/Examples/subdir.mk
  29. 24
      09_Functions/Debug/Exercises/subdir.mk
  30. 24
      09_Functions/Debug/Program/subdir.mk
  31. 24
      09_Functions/Debug/Projects/subdir.mk
  32. 24
      09_Functions/Debug/average/subdir.mk
  33. 24
      09_Functions/Debug/countdown/subdir.mk
  34. 44
      09_Functions/Debug/makefile
  35. 17
      09_Functions/Debug/sources.mk
  36. 17
      09_Functions/Examples/average.c
  37. 14
      09_Functions/Examples/countdown.c
  38. 26
      09_Functions/Examples/prime.c
  39. 11
      09_Functions/Examples/pun2.c
  40. 51
      09_Functions/Examples/qsort.c
  41. 241
      09_Functions/Exercises/main.c
  42. 18
      09_Functions/Exercises/swap.c
  43. 46
      09_Functions/Projects/pr_01.c
  44. 37
      09_Functions/Projects/pr_02.c
  45. 84
      09_Functions/Projects/pr_03.c
  46. 52
      09_Functions/Projects/pr_04.c
  47. 62
      09_Functions/Projects/pr_05.c
  48. 30
      09_Functions/Projects/pr_06.c
  49. 28
      09_Functions/Projects/pr_07.c
  50. 79
      09_Functions/Projects/pr_08.c
  51. BIN
      10_Program_Organization/Debug/10_Program_Organization
  52. 24
      10_Program_Organization/Debug/Examples/subdir.mk
  53. 1
      10_Program_Organization/Debug/Projects/pr_07.d
  54. BIN
      10_Program_Organization/Debug/Projects/pr_07.o
  55. 24
      10_Program_Organization/Debug/Projects/subdir.mk
  56. 44
      10_Program_Organization/Debug/makefile
  57. 83
      10_Program_Organization/Examples/guess.c
  58. 163
      10_Program_Organization/Examples/poker.c
  59. 35
      10_Program_Organization/Examples/stack.c
  60. 80
      10_Program_Organization/Projects/pr_01.c
  61. 164
      10_Program_Organization/Projects/pr_02.c
  62. 191
      10_Program_Organization/Projects/pr_03.c
  63. 169
      10_Program_Organization/Projects/pr_04.c
  64. 173
      10_Program_Organization/Projects/pr_05.c
  65. 96
      10_Program_Organization/Projects/pr_06.c
  66. BIN
      10_Program_Organization/Projects/pr_07
  67. 87
      10_Program_Organization/Projects/pr_07.c
  68. 65
      11_Pointers/.settings/org.eclipse.cdt.codan.core.prefs
  69. BIN
      11_Pointers/Debug/11_Pointers
  70. 24
      11_Pointers/Debug/Examples/subdir.mk
  71. 1
      11_Pointers/Debug/Projects/pr_01.d
  72. BIN
      11_Pointers/Debug/Projects/pr_01.o
  73. 24
      11_Pointers/Debug/Projects/subdir.mk
  74. 44
      11_Pointers/Debug/makefile
  75. 41
      11_Pointers/Examples/maxmin.c
  76. 106
      11_Pointers/Exercises/main.c
  77. 106
      11_Pointers/Exercises/main.c~
  78. 26
      11_Pointers/Projects/pr_01.c
  79. 101
      11_Pointers/Projects/pr_02.c
  80. 46
      11_Pointers/Projects/pr_03.c
  81. 169
      11_Pointers/Projects/pr_04.c
  82. 24
      12_Pointers_And_Arrays/Debug/Examples/subdir.mk
  83. 44
      12_Pointers_And_Arrays/Debug/makefile
  84. 26
      12_Pointers_And_Arrays/Examples/reverse3.c
  85. 75
      12_Pointers_And_Arrays/Exercises/main.c
  86. 53
      12_Pointers_And_Arrays/Exercises/ptr_stack.c
  87. 26
      12_Pointers_And_Arrays/Projects/pr_01.c
  88. 33
      12_Pointers_And_Arrays/Projects/pr_02.c
  89. 41
      12_Pointers_And_Arrays/Projects/pr_05.c
  90. 59
      12_Pointers_And_Arrays/Projects/pr_06.c
  91. 43
      12_Pointers_And_Arrays/Projects/pr_07.c
  92. BIN
      13_Strings/Debug/13_Strings
  93. 24
      13_Strings/Debug/Examples/subdir.mk
  94. 1
      13_Strings/Debug/Projects/pr_18.d
  95. BIN
      13_Strings/Debug/Projects/pr_18.o
  96. 24
      13_Strings/Debug/Projects/subdir.mk
  97. 44
      13_Strings/Debug/makefile
  98. 32
      13_Strings/Examples/planet.c
  99. 64
      13_Strings/Examples/remind.c
  100. 223
      13_Strings/Exercises/main.c
  101. Some files were not shown because too many files have changed in this diff Show More

Binary file not shown.

@ -0,0 +1,43 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: 02_C_Fundamentals
# Tool invocations
02_C_Fundamentals: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "02_C_Fundamentals" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) 02_C_Fundamentals
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

@ -0,0 +1,17 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
O_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
OBJS :=
C_DEPS :=
EXECUTABLES :=
# Every subdirectory with source files must be described here
SUBDIRS := \
. \

@ -0,0 +1,24 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
# Add inputs and outputs from these tool invocations to the build variables
C_SRCS += \
../main.c
OBJS += \
./main.o
C_DEPS += \
./main.d
# Each subdirectory must supply rules for building sources it contributes
%.o: ../%.c
@echo 'Building file: $<'
@echo 'Invoking: GCC C Compiler'
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
@echo 'Finished building: $<'
@echo ' '

@ -0,0 +1,227 @@
#include <stdio.h>
#define INCHES_PER_POUND 166
#define FREEZING_PT 32.0f
#define SCALE_FACTOR (5.0f / 9.0f)
#define PI 3.141592f
void dweight(void);
void dweight2(void);
void celsius(void);
void ex_01(void);
void ex_02(void);
void ex_03(void);
void ex_04(void);
void ex_05(void);
void ex_06(void);
void ex_07(void);
void pr_01(void);
void pr_02(void);
void pr_03(void);
void pr_04(void);
void pr_05(void);
void pr_06(void);
void pr_07(void);
void pr_08(void);
int main(void) {
// dweight();
// dweight2();
// celsius();
// ex_04();
pr_08();
return 0;
}
/* Calculates the dimensional weight of the box,
* constant size */
void dweight(void) {
int height, lenght, width, volume, weight;
height = 8;
lenght = 12;
width = 10;
volume = height * lenght * width;
weight = (volume + INCHES_PER_POUND - 1) / INCHES_PER_POUND;
printf("Dimensions: %dx%dx%d\n", lenght, width, height);
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n", weight);
}
/* Calculates the dimensional weight of the box,
* user defined size */
void dweight2(void) {
int height, lenght, width, volume, weight;
printf("enter height: ");
scanf("%d", &height);
printf("enter lenght: ");
scanf("%d", &lenght);
printf("enter widht: ");
scanf("%d", &width);
volume = height * lenght * width;
weight = (volume + INCHES_PER_POUND - 1) / INCHES_PER_POUND;
printf("Dimensions: %dx%dx%d\n", lenght, width, height);
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n", weight);
}
/* Converts tempereture from Fahrenheit to Celsius */
void celsius(void) {
float fahrenheit, celsius;
printf("Enter Fahrenheit temperature: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR;
printf("Celsius equivalent: %.1f\n", celsius);
}
/* EXERCISES */
void ex_01(void) {
printf("Hello world\n");
}
void ex_02(void) {
printf("Parkinson's Law:\nWork expands so as to ");
printf("fill the time\n");
printf("available for its completion.\n");
}
void ex_03(void) {
int height = 8, lenght = 12, width = 10, volume;
volume = height * lenght * width;
printf("Dimensions: %dx%dx%d\n", lenght, width, height);
printf("Volume (cubic inches): %d\n", volume);
printf("Dimensional weight (pounds): %d\n",
(volume + INCHES_PER_POUND - 1) / INCHES_PER_POUND);
}
void ex_04(void) {
int a, b, c;
float d, e, f;
printf("%d %d %d %f %f %f", a, b, c, d, e, f);
}
void ex_05(void) {
int _100_bot, bott_by_hun;
}
void ex_06(void) {
// Reserved
}
void ex_07(void) {
// for
}
/* PROJECTS */
void pr_01(void) {
printf("%8c\n", '*');
printf("%7c\n", '*');
printf("%6c\n", '*');
printf("%1c%4c\n", '*', '*');
printf("%2c%2c\n", '*', '*');
printf("%3c\n", '*');
}
void pr_02(void) {
int r = 10;
printf("Volume of sphere of radius %dm is %fm^3.", r,
(4.0f / 3.0f) * PI * (r * r * r));
}
void pr_03(void) {
int r = 0;
printf("Please enter radius of the sphere (m): ");
scanf("%d", &r);
printf("Volume of sphere of radius %dm is %fm^3.", r,
(4.0f / 3.0f) * PI * (r * r * r));
}
void pr_04(void) {
float amount;
printf("Enter an amount: ");
scanf("%f", &amount);
printf("With tax added: $%.2f", amount * 1.05f);
}
void pr_05(void) {
float x;
printf("Enter x: ");
scanf("%f", &x);
printf("3x^5 + 2x^4 - 5x^3 -x^2 - 6 = %f",
+ 3 * (x * x * x * x * x)
+ 2 * (x * x * x * x)
- 5 * (x * x * x)
- 1 * (x * x)
+ 7 * (x)
- 6 * (1));
}
void pr_06(void) {
float x;
printf("Enter x: ");
scanf("%f", &x);
printf("3x^5 + 2x^4 - 5x^3 -x^2 - 6 = %f",
((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6);
}
void pr_07(void) {
int amount, b20, b10, b5;
printf("Enter a dollar amount: ");
scanf("%d", &amount);
b20 = amount / 20;
amount -= b20 * 20;
b10 = amount / 10;
amount -= b10 * 10;
b5 = amount / 5;
amount -= b5 * 5;
printf("\n$20 bills: %d\n$10 bills: %d\n $5 bills: %d\n $1 bills: %d",
b20, b10, b5, amount);
}
void pr_08(void) {
float balance, interest, payment;
printf("Enter amount of loan: ");
scanf("%f", &balance);
printf("Enter amount of interest: ");
scanf("%f", &interest);
printf("Enter amount of payment: ");
scanf("%f", &payment);
float monthly = interest / 100 / 12;
balance -= payment;
balance += (balance * monthly);
printf("\nBalance remaining after fist payment: $%.2f", balance);
balance -= payment;
balance += (balance * monthly);
printf("\nBalance remaining after second payment: $%.2f", balance);
balance -= payment;
balance += (balance * monthly);
printf("\nBalance remaining after third payment: $%.2f", balance);
}

Binary file not shown.

@ -0,0 +1,43 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: 03_Formatted_IO
# Tool invocations
03_Formatted_IO: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "03_Formatted_IO" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) 03_Formatted_IO
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

@ -0,0 +1,171 @@
/* CHAPTER 3 */
#include <stdio.h>
void tprintf(void);
void ex_01(void);
void ex_02(void);
void ex_04(void);
void ex_05(void);
void pr_01(void);
void pr_02(void);
void pr_03(void);
void pr_04(void);
void pr_05(void);
void pr_06(void);
int main(void) {
// tprintf();
// addfrac();
// ex_06();
pr_06();
return 0;
}
/* Ilustrates the use of printf */
void tprintf(void) {
int i;
float x;
i = 40;
x = 839.21f;
printf("|%d|%5d|%-5d|%5.3d|\n", i, i, i, i);
printf("|%10.3f|%10.3e|%-10g|\n", x, x, x);
}
/* Adds two fractions */
void addfrac(void) {
int num1, denom1, num2, denom2, result_num, result_denom;
printf("Enter first fraction: ");
scanf("%d / %d", &num1, &denom1);
printf("Enter second fraction: ");
scanf("%d / %d", &num2, &denom2);
result_num = num1 * denom2 + num2 * denom1;
result_denom = denom1 * denom2;
printf("The sum is %d/%d\n", result_num, result_denom);
}
/* EXERCISES */
void ex_01(void) {
printf("%6d,%4d\n", 86, 1040);
printf("%12.5e\n", 30.253);
printf("%.4f\n", 83.162);
printf("%-6.2g\n", .0000009979);
}
void ex_02(void) {
// a) %-8.1e
// b) %10.6e
// c) %-8.3f
// d) %6.0f
}
void ex_04(void) {
// Enter 10.3 5 6
int i, j;
float x;
scanf("%d%f%d", &i, &x, &j);
printf("i = %d, x = %.1f, j = %d", i, x, j);
}
void ex_05(void) {
// Enter 12.3 45.6 789
int i;
float x, y;
scanf("%f%d%f", &x, &i, &y);
printf("x = %f, i = %d, y = %f", x, i, y);
}
/* PROJECTS */
void pr_01(void) {
int dd, mm, yyyy;
printf("Enter a date (dd/mm/yyyy): ");
scanf("%d/%d/%d", &dd, &mm, &yyyy);
printf("You entered date: %.4d%.2d%.2d", yyyy, mm, dd);
}
void pr_02(void) {
int number, dd, mm, yyyy;
float price;
printf("Enter item number: ");
scanf("%d", &number);
printf("Enter item price: ");
scanf("%f", &price);
printf("Enter purchase date (dd, mm, yyyy): ");
scanf("%d/%d/%d", &dd, &mm, &yyyy);
printf("\nItem\t\tUnit\t\tPurchase\n");
printf(" \t\tPrice\t\tDate\n");
printf("%d\t\t$ %.2f\t%.2d/%.2d/%.4d\n",
number, price, dd, mm, yyyy);
}
void pr_03(void) {
int prefix, groupID, publisher, number, check;
printf("Enter ISBN: ");
scanf("%d-%d-%d-%d-%d", &prefix, &groupID,
&publisher, &number, &check);
printf("GS1 prefix: %d\n", prefix);
printf("Group identifier: %d\n", groupID);
printf("Publisher code: %d\n", publisher);
printf("Item number: %d\n", number);
printf("Check digit: %d\n", check);
}
void pr_04(void) {
int a, b, c;
printf("Enter phone number [(xxx) xxx-xxxx]: ");
scanf("(%d) %d-%d", &a, &b, &c);
printf("You entered: %d.%d.%d", a, b, c);
}
void pr_05(void) {
int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16;
// Enter 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 for Magic Square
printf("Enter numbers from 1 to 16 in any order:\n");
scanf("%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&i1, &i2, &i3, &i4, &i5, &i6, &i7, &i8, &i9, &i10, &i11, &i12, &i13, &i14, &i15, &i16);
printf("\n");
printf("%6d %2d %2d %2d |%2d\n", i1, i2, i3, i4, (i1 + i2 + i3 + i4));
printf("%6d %2d %2d %2d |%2d\n", i5, i6, i7, i8, (i5 + i6 + i7 + i8));
printf("%6d %2d %2d %2d |%2d\n", i9, i10, i11, i12, (i9 + i10 + i11 + i12));
printf("%6d %2d %2d %2d |%2d\n", i13, i14, i15, i16, (i13 + i14 + i15 + i16));
printf("--------------- | --\n");
printf("%2d |%2d %2d %2d %2d |%2d\n", (i4 + i7 + i10 + i13), (i1 + i5 + i9 + i13), (i2 + i6 + i10 + i14),
(i3 + i7 + i11 + i15), (i4 + i8 + i12 + i16), (i1 + i6 + i11 + i16));
printf("\n");
}
void pr_06(void) {
int num1, denom1, num2, denom2, result_num, result_denom;
printf("Enter two fractions separated by a plus sign: ");
scanf("%d / %d + %d / %d", &num1, &denom1, &num2, &denom2);
result_num = num1 * denom2 + num2 * denom1;
result_denom = denom1 * denom2;
printf("The sum is %d/%d\n", result_num, result_denom);
}

Binary file not shown.

@ -0,0 +1,43 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: 04_Expressions
# Tool invocations
04_Expressions: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "04_Expressions" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) 04_Expressions
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

@ -0,0 +1,248 @@
/* CHAPTER 4 */
#include <stdio.h>
void upc(void);
void ex_01(void);
void ex_02(void);
void ex_09(void);
void ex_10(void);
void ex_11(void);
void ex_12(void);
void ex_13(void);
void ex_14(void);
void ex_15(void);
void pr_02(void);
void pr_03(void);
void pr_04(void);
void pr_05(void);
void pr_06(void);
int main(void) {
// upc();
// ex_12();
pr_06();
return 0;
}
/* Computes a universal product code check digit */
void upc(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));
}
/* EXERCISES */
void ex_01(void) {
int i, j, k;
i = 5; j = 3;
printf("%d %d\n", i / j, i % j);
i = 2; j = 3;
printf("%d\n", (i + 10) % j);
i = 7; j = 8; k = 9;
printf("%d\n", (i + 10) % k / j);
i = 1; j = 2; k = 3;
printf("%d\n", (i + 5) % (j + 2) / k);
}
void ex_02(void) {
int i = 9, j = 1;
printf("%d\n", (-i) / j);
printf("%d\n", -(i / j));
}
void ex_09(void) {
int i, j, k;
i = 7; j = 8;
i *= j + 1;
printf("%d %d\n", i, j);
i = j = k = 1;
i += j += k;
printf("%d %d %d\n", i, j, k);
i = 1; j = 2; k = 3;
i -= j -= k;
printf("%d %d %d\n", i, j, k);
i = 2; j = 1; k = 0;
i *= j *= k;
printf("%d %d %d\n", i, j, k);
}
void ex_10(void) {
int i, j;
i = 6;
j = i += i;
printf("%d %d\n", i, j);
i = 5;
j = (i -= 2) + 1;
printf("%d %d\n", i, j);
i = 7;
j = 6 + (i = 2.5);
printf("%d %d\n", i, j);
i = 2; j = 8;
j = (i = 6) + (j = 3);
printf("%d %d\n", i, j);
}
void ex_11(void) {
int i, j, k;
i = 1;
printf("%d ", i++ - 1);
printf("%d\n", i);
i = 10; j = 5;
printf("%d ", i++ - ++j);
printf("%d %d\n", i, j);
i = 7; j = 8;
printf("%d ", i++ - --j);
printf("%d %d\n", i, j);
i = 3; j = 4; k = 5;
printf("%d ", i++ - j++ + --k);
printf("%d %d %d\n", i, j, k);
}
void ex_12(void) {
int i, j;
i = 5;
j = ++i * 3 - 2;
printf("%d %d\n", i, j);
i = 5;
j = 3 - 2 * i++;
printf("%d %d\n", i, j);
i = 7;
j = 3 * i-- + 2;
printf("%d %d\n", i, j);
i = 7;
j = 3 + --i * 2;
printf("%d %d\n", i, j);
}
void ex_13(void) {
// ++i == (i += 1)
}
void ex_14(void) {
// (a * b) - (c * d) + e
// ((a / b) % c) / d
// (((- a) - b) + c) - (+d)
// ((a * (- b)) / c) - d
}
void ex_15(void) {
//= 1, 2
//a 3, 2
//b 0, 2
//c 1, 2
//d 1, 3
}
/* PROJECTS */
void pr_01(void) {
int n;
printf("Enter two-digit number: ");
scanf("%d", &n);
printf("The reversal is: %d%d\n",
(n % 10), (n / 10));
}
// Reverse
void pr_02(void) {
int n;
printf("Enter three-digit n: ");
scanf("%d", &n);
printf("The reversal is: %d%d%d\n",
(n % 10), (n / 10) % 10, n / 100);
}
// Reverse
void pr_03(void) {
int hundreds, tens, ones;
printf("Enter three-digit number: ");
scanf("%1d%1d%1d", &hundreds, &tens, &ones);
printf("The reversal is: %d%d%d\n", ones, tens, hundreds);
}
// Octal
void pr_04(void) {
int n;
printf("Enter a number between 0 and 32767: ");
scanf("%d", &n);
printf("In octal, your number is: %d%d%d%d%d",
(n / 4096 % 8), (n / 512 % 8),
(n / 64 % 8), (n / 8 % 8), (n % 8));
}
// UPC rewritten
void pr_05(void) {
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total;
printf("Enter the first 11 digits of UPC: ");
scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",
&d, &i1, &i2, &i3, &i4, &i5, &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));
}
// EAN
void pr_06(void) {
int i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12,
first_sum, second_sum, total;
printf("Enter the first 12 digits of EAN: ");
scanf("%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d%1d",
&i1, &i2, &i3, &i4, &i5, &i6,
&i7, &i8, &i9, &i10, &i11, &i12);
first_sum = i2 + i4 + i6 + i8 + i10 + i12;
second_sum = i1 + i3 + i5 + i7 + i9 + i11;
total = 3 * first_sum + second_sum;
printf("Check digit: %d\n", 9 - ((total - 1) % 10));
}

@ -0,0 +1,43 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: 05_Selection_Statements
# Tool invocations
05_Selection_Statements: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: GCC C Linker'
gcc -o "05_Selection_Statements" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(OBJS)$(C_DEPS)$(EXECUTABLES) 05_Selection_Statements
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets

@ -0,0 +1,619 @@
/* CHAPTER 5 */
#include <stdio.h>
#include <stdbool.h>
void broker(void);
void date(void);
void ex_01(void);
void ex_02(void);
void ex_03(void);
void ex_04(void);
void ex_05(void);
void ex_06(void);
void ex_07(void);
void ex_08(void);
void ex_09(void);
void ex_10(void);
void pr_01(void);
void pr_02(void);
void pr_03(void);
void pr_04(void);
void pr_05(void);
void pr_06(void);
void pr_07(void);
void pr_08(void);
void pr_09(void);
void pr_10(void);
void pr_11(void);
int main(void) {
// broker();
// date();
// ex_08();
pr_08();
return 0;
}
// Calculates a broker's commission
void broker(void) {
float commision, value;
printf("Enter value of trade: ");
scanf("%f", &value);
if (value < 2500.00f)
commision = 30.00f + .017f * value;
else if (value < 6250.00f)
commision = 56.00f + .0066f * value;
else if (value < 20000.00f)
commision = 76.00f + .0034f * value;
else if (value < 50000.00f)
commision = 100.00f + .0022f * value;
else if (value < 500000.00f)
commision = 155.00f + .0011f * value;
else
commision = 255.00f + .0009f * value;
if (commision < 39.00f)
commision = 39.00f;
printf("Commission: $%.2f\n", commision);
}
// Prints date in legal form
void date(void) {
int month, day, year;
printf("Enter date (dd/mm/yyyy): ");
scanf("%d /%d /%d", &day, &month, &year );
printf("Dated this %d", day);
switch (day) {
case 1: case 21: case 31:
printf("st"); break;
case 2: case 22:
printf("nd"); break;
case 3: case 23:
printf("rd"); break;
default:
printf("th"); break;
}
printf(" of ");
switch (month) {
case 1: printf("January"); break;
case 2: printf("February"); break;
case 3: printf("March"); break;
case 4: printf("April"); break;
case 5: printf("May"); break;
case 6: printf("June"); break;
case 7: printf("July"); break;
case 8: printf("August"); break;
case 9: printf("September"); break;
case 10: printf("October"); break;
case 11: printf("November"); break;
case 12: printf("December"); break;
}
printf(", %.4d.\n", year);
}
/* EXERCISES */
void ex_01(void) {
int i, j, k;
i = 2; j = 3;
k = i * j == 6;
printf("%d\n", k);
i = 5; j = 10; k = 1;
printf("%d\n", k > j < j);
i = 3; j = 2; k = 1;
printf("%d\n", i < j == j < k);
i = 3; j = 2; k = 1;
printf("%d\n", i % j + i < k);
}
void ex_02(void) {
int i, j, k;
i = 10; j = 5;
printf("%d\n", !i < j);
i = 2; j = 1;
printf("%d\n", !!i + !j);
i = 5; j = 0; k = -5;
printf("%d\n", i && j || k);
i = 1; j = 2; k = 3;
printf("%d\n", i < j || k);
}
void ex_03(void) {
int i, j, k;
i = 3; j = 4; k = 5;
printf("%d, ", i < j || ++j < k);
printf("%d %d %d\n", i, j, k);
i = 7; j = 8; k = 9;
printf("%d, ", i - 7 && j++ < k);
printf("%d %d %d\n", i, j, k);
i = 7; j = 8; k = 9;
printf("%d, ", (i = j) || (j = k));
printf("%d %d %d\n", i, j, k);
i = 1; j = 1; k = 1;
printf("%d, ", ++i || ++j && ++k);
printf("%d %d %d\n", i, j, k);
}
// Short -1, 0, 1 return
void ex_04(void) {
int i = 5, j = 5;
printf("%d", (i > j) - (i < j));
}
void ex_05(void) {
int n = -5;
if (n >= 1 <= 10)
printf("n is between 1 and 10\n");
}
void ex_06(void) {
int n = -9;
if (n == 1-10)
printf("n is between 1 and 10\n");
}
void ex_07(void) {
int i = -17;
printf("%d\n", i >= 0 ? i : -i);
}
void ex_08(void) {
int age = 22;
bool teenager = (age >= 13 && age <= 19);
printf("Folk of age %d is teenager: %d", age, teenager);
}
void ex_09(void) {
// they seems both equivalent to me
}
void ex_10(void) {
int i = 1;
switch (i % 3) {
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
}
void ex_11(void) {
int area_code = 404;
switch (area_code) {
case 229:
printf("Albany");
break;
case 404: case 470: case 678: case 770:
printf("Atlanta");
break;
case 478:
printf("Macon");
break;
case 706: case 762:
printf("Columbus");
break;
case 912:
printf("Savannah");
break;
default:
printf("Area code not recognized");
}
}
/* PROJECTS */
void pr_01(void) {
int n, digits = 0;
printf("Enter a number: ");
scanf("%d", &n);
printf("The number %d has ", n);
if (n > 0)
digits++;
if (n > 9)
digits++;
if (n > 99)
digits++;
if (n > 999)
digits++;
printf("%d digits.", digits);
}
// Convert 24h date to 12h
void pr_02(void) {
int hh, mm;
bool flag;
printf("Enter a 24-hour time: ");
scanf("%2d:%2d", &hh, &mm);
flag = hh / 12;
hh %= 12;
printf("Equivalent 12-hour time: %.2d:%.2d ", hh, mm);
if (flag)
printf("PM");
else
printf("AM");
}
// Broker modified
void pr_03(void) {
float commision, pricePerShare, value;
int shares;
printf("Enter number shares: ");
scanf("%d", &shares);
printf("Enter price per share: ");
scanf("%f", &pricePerShare);
value = (float)shares * pricePerShare;
if (value < 2500.00f)
commision = 30.00f + .017f * value;
else if (value < 6250.00f)
commision = 56.00f + .0066f * value;
else if (value < 20000.00f)
commision = 76.00f + .0034f * value;
else if (value < 50000.00f)
commision = 100.00f + .0022f * value;
else if (value < 500000.00f)
commision = 155.00f + .0011f * value;
else
commision = 255.00f + .0009f * value;
if (commision < 39.00f)
commision = 39.00f;
printf("\nCommission of broker #1: $%.2f\n", commision);
if (shares < 2000)
commision = 33.00f + .03f * shares;
else
commision = 33.00f + .02f * shares;
printf("Commission of broker #2: $%.2f\n", commision);
}
// Beaufort scale
void pr_04(void) {
int speed;
printf("Enter wind speed in knots: ");
scanf("%d", &speed);
if (speed < 1)
printf("Calm\n");
else if (speed <= 3)
printf("Light air\n");
else if (speed <= 27)
printf("Breeze\n");
else if (speed <= 47)
printf("Gale\n");
else if (speed <= 63)
printf("Storm\n");
else
printf("Hurricane\n");
}
// Tax
void pr_05(void) {
float income, tax;
printf("Enter income: ");
scanf("%f", &income);
if (income <= 750.0f)
tax = income * .01f;
else if (income <= 2250.0f)
tax = income * .02f + 7.50f;
else if (income <= 3750.0f)
tax = income * .03f + 37.50f;
else if (income <= 5250.0f)
tax = income * .04f + 82.50f;
else if (income <= 7000.0f)
tax = income * .05f + 142.50f;
else
tax = income * .06f + 230.00f;
printf("Tax due is: %.2f", tax);
}
// upc modified
void pr_06(void) {
int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5,
first_sum, second_sum, total, check_digit;
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);
printf("Enter the last (single) digit: ");
scanf("%1d", &check_digit);
first_sum = d + i2 + i4 + j1 + j3 + j5;
second_sum = i1 + i3 + i5 + j2 + j4;
total = 3 * first_sum + second_sum;
if (check_digit == (9 - ((total - 1) % 10)))
printf("VALID\n");
else
printf("NOT VALID\n");
}
// Smallest and largest out of 4 numbers
void pr_07(void) {
int i1, i2, i3, i4, smal1, smal2, larg1, larg2;
printf("Enter four integers: ");
scanf("%d %d %d %d", &i1, &i2, &i3, &i4);
if (i1 < i2) {
smal1 = i1;
larg1 = i2;
}
else {
smal1 = i2;
larg1 = i1;
}
if (i3 < i4) {
smal2 = i3;
larg2 = i4;
}
else {
smal2 = i4;
larg2 = i3;
}
printf("Smallest: %d\n", (smal1 < smal2) ? smal1 : smal2);
printf("Largest: %d\n", (larg1 > larg2) ? larg1 : larg2);
}
// Flight departures
void pr_08(void) {
int hh, mm, entered, closestToken = 0,
closestDistance, distance;
printf("Enter a 24-hour time: ");
scanf("%2d:%2d", &hh, &mm);
entered = hh * 60 + mm;
distance = 480 - entered;
distance = distance >= 0 ? distance : -distance;
closestDistance = distance;
closestToken = 480;
distance = 583 - entered;
distance = distance >= 0 ? distance : -distance;
if (distance < closestDistance) {
closestDistance = distance;
closestToken = 583;
}
distance = 679 - entered;
distance = distance >= 0 ? distance : -distance;
if (distance < closestDistance) {
closestDistance = distance;
closestToken = 679;
}
distance = 767- entered;
distance = distance >= 0 ? distance : -distance;
if (distance < closestDistance) {
closestDistance = distance;
closestToken = 767;
}
distance = 840 - entered;
distance = distance >= 0 ? distance : -distance;
if (distance < closestDistance) {
closestDistance = distance;
closestToken = 840;
}
distance = 945 - entered;
distance = distance >= 0 ? distance : -distance;
if (distance < closestDistance) {
closestDistance = distance;
closestToken = 945;
}
distance = 1140 - entered;
distance = distance >= 0 ? distance : -