Left off on 2.5 Arithmetic Operators

This commit is contained in:
kdeng00
2020-10-12 21:58:21 -04:00
parent 60d4d83055
commit 1934b4af88
2 changed files with 137 additions and 0 deletions
@@ -0,0 +1,95 @@
/*
*
* Demonstrating the use of arithmetic operators
*
* Author: Kun Deng
*/
#include <stdio.h>
#include <stdlib.h>
// 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;
}
+42
View File
@@ -0,0 +1,42 @@
/*
*
* This program is an example of declarations
*
* Author: Kun Deng
*/
#include <stdio.h>
// 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;
}