Commit cfb8b7ec by unknown

changed folder name to homework1

parent 10e5ccf6
Showing with 88 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isDate (int input);
int largestNumber ();
int smallestNumber ();
int toBinary(int input);
int main() {
// Custom changable variables ----------------------------------
int numberToBinary = 1023;
int date = 29022100;
// End -----------------------------------------------
printf("Homework Lab 2 (10 - September - 2018)\n");
printf("Largest number in array:\t%d", largestNumber());
printf("\nSmallest number in array:\t%d", smallestNumber());
printf("\nIs valid date:\t\t\t%s", isDate(date) == 1? "True" : "False");
printf("\nBinary Homework:\t\t%d", toBinary(numberToBinary));
return (EXIT_SUCCESS);
}
int largestNumber () {
int size = 10;
int numbers[] = {15, 6, 7, 98, -8, 65, 777, -87, 213, 5};
int i;
int largest = numbers[0];
for (i = 0; i < size; i++) {
if (numbers[i] > largest) largest = numbers[i];
}
return largest;
}
int smallestNumber () {
int size = 10;
int numbers[] = {15, 6, 7, 98, -8, 65, 777, -87, 213, 5};
int i;
int smallest = numbers[0];
for (i = 0; i < size; i++) {
if (numbers[i] < smallest) smallest = numbers[i];
}
return smallest;
}
int isDate (int input) {
int day = input / 1000000;
int month = input / 10000 % 100;
int year = input % 10000;
if (year < 0) return 0;
if (month > 12) return 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) return 0;
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) return 0;
}
if (month == 2) {
if (year % 4 || (year % 100 == 0 && year % 400)) {
if (day > 28) return 0;
} else {
if (day > 29) return 0;
}
}
return 1;
}
int toBinary(int input) {
if (input > 1023) return 0; // Ran out of memory error
int binLength = log(input) / log(2) + 1;
int a[binLength];
int i;
for (i = 0; i < binLength; i++) {
a[i] = input % 2;
input /= 2;
}
unsigned int result = 0;
for (i = 0; i < binLength; i++) {
result = 10 * result + a[binLength - i - 1];
}
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment