From 1934b4af88fe8a9d02a4a7fa244b09d3654ded4d Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Mon, 12 Oct 2020 21:58:21 -0400 Subject: [PATCH] Left off on 2.5 Arithmetic Operators --- ch_02/arithmetic_operations-leap_year/main.c | 95 ++++++++++++++++++++ ch_02/declarations/main.c | 42 +++++++++ 2 files changed, 137 insertions(+) create mode 100644 ch_02/arithmetic_operations-leap_year/main.c create mode 100644 ch_02/declarations/main.c diff --git a/ch_02/arithmetic_operations-leap_year/main.c b/ch_02/arithmetic_operations-leap_year/main.c new file mode 100644 index 0000000..72bc7a4 --- /dev/null +++ b/ch_02/arithmetic_operations-leap_year/main.c @@ -0,0 +1,95 @@ +/* + * + * Demonstrating the use of arithmetic operators + * + * Author: Kun Deng + */ + + +#include +#include + +// Parameter target_type is the valid target type +// Should match the symbolic constants of HEX or NUMBER +// +// Return values +// 0 - Invalid +// 1 - Valid +int is_valid_source(char *value); + + +// Checks to see if the provided year is a leap year. +// Returns 1 if true and 0 if not +int is_leap_year(const int year); + + +int main(int argc, char **argv) +{ + if (argc != 2) + { + printf("Provide a year as an argument\n"); + printf("./program.out 2000\n"); + + return -1; + } + + if (is_valid_source(argv[1]) != 1) + { + printf("%s is not a number\n", argv[1]); + + return -1; + } + + const int year_value = atoi(argv[1]); + + if (is_leap_year(year_value) == 1) + { + printf("%d is a leap year\n", year_value); + } + else + { + printf("%d is not a leap year\n", year_value); + } + + return 0; +} + + +int is_valid_source(char *value) +{ + int result = 1; + + for (int i = 0; value[i] != '\0'; ++i) + { + char val = value[i]; + + // ASCII values 48 - 57 are numbers + // Checks to see if there are non-numbers + if (val <= 47 || val >= 58) + { + result = 0; + break; + } + } + + return result; +} + +int is_leap_year(const int year) +{ + int result; + int aa = year % 4; + int ab = year % 100; + int b = year % 400; + + if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + { + result = 1; + } + else + { + result = 0; + } + + return result; +} diff --git a/ch_02/declarations/main.c b/ch_02/declarations/main.c new file mode 100644 index 0000000..5e80928 --- /dev/null +++ b/ch_02/declarations/main.c @@ -0,0 +1,42 @@ +/* + * + * This program is an example of declarations + * + * Author: Kun Deng + */ + + +#include + +// The const in the parameter field ensures that the variable value cannot be modified +// within the function +void faux_change(const int value); + +int main() +{ + // Single variable declarations + int s_low; + int s_high; + int s_mid; + + // Multi-variable declarations + int m_low, m_high, m_mid; + + + // Declaring and initializing variables + char a = 'a'; + int k = 5; + + printf("k is %d before calling faux_change()\n", k); + faux_change(k); + printf("After: %d\n", k); + + return 0; +} + + +void faux_change(const int value) +{ + // If uncommented, will not compile + // value = value * 2; +}