diff --git a/assignment01/Makefile b/assignment01/Makefile new file mode 100644 index 0000000..761c99c --- /dev/null +++ b/assignment01/Makefile @@ -0,0 +1,37 @@ +# Makefile +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +# Output +TARGETS = pro1.out pro2.out pro3.out pro4.out + +all: $(TARGETS) + +pro1: project1.c + $(CC) $(CFLAGS) -o $@.out $< + +pro2: project2.c + $(CC) $(CFLAGS) -o $@.out $< + +pro3: project3.c + $(CC) $(CFLAGS) -o $@.out $< + +pro4: project4.c + $(CC) $(CFLAGS) -o $@.out $< + +# Run targets +run1: pro1 + ./pro1.out + +run2: pro2 + ./pro2.out + +run3: pro3 + ./pro3.out + +run4: pro4 + ./pro4.out + +clean: + rm -f $(TARGETS) diff --git a/assignment01/project1.c b/assignment01/project1.c index e69de29..b9a272e 100644 --- a/assignment01/project1.c +++ b/assignment01/project1.c @@ -0,0 +1,26 @@ +#include + +#define MEGABYTES_TO_MEGABITS 8 + +int main(void) { + float downloadSpeedMbs; + float fileSizeMB; + float downloadTimeSeconds; + + printf("Enter download speed (Mbs): "); + scanf("%f", &downloadSpeedMbs); + + printf("Enter file size (MB): "); + scanf("%f", &fileSizeMB); + + downloadTimeSeconds = (fileSizeMB * MEGABYTES_TO_MEGABITS) / downloadSpeedMbs; + + printf( + "At %.2f megabits per second, a file of %.2f megabytes downloads in %.2f seconds.\n", + downloadSpeedMbs, + fileSizeMB, + downloadTimeSeconds + ); + + return 0; +} diff --git a/assignment01/project2.c b/assignment01/project2.c index e69de29..3022ab6 100644 --- a/assignment01/project2.c +++ b/assignment01/project2.c @@ -0,0 +1,38 @@ +#include + +#define CENTIMETERS_PER_FOOT 30.48 +#define CENTIMETERS_PER_INCH 2.54 +#define INCHES_PER_FOOT 12 + +int main(void) { + + float heightCm; + int heightFeet; + float heightInches; + + do { + + printf("Enter a height in centimeters (<=0 to quit): "); + scanf("%f", &heightCm); + + if (heightCm <= 0) { + break; + } + + heightFeet = (heightCm / CENTIMETERS_PER_FOOT); + heightInches = (heightCm / CENTIMETERS_PER_INCH) - + (INCHES_PER_FOOT * heightFeet); + + printf( + "%.1f cm = %d feet, %.1f inches\n", + heightCm, + heightFeet, + heightInches + ); + + } while (heightCm > 0); + + printf("Bye\n"); + + return 0; +} \ No newline at end of file diff --git a/assignment01/project3.c b/assignment01/project3.c index e69de29..78e6bb5 100644 --- a/assignment01/project3.c +++ b/assignment01/project3.c @@ -0,0 +1,36 @@ +#include + +int main(void) { + + int max; + int min; + int sumOfSquares; + + do { + + sumOfSquares = 0; + + printf("Enter a lower and upper limit: "); + scanf("%d %d", &min, &max); + + if (max <= min) { + break; + } + + for (int i = min; i <= max; i++) { + sumOfSquares += i * i; + } + + printf( + "The sums of the squares from %d to %d is %d\n", + min * min, + max * max, + sumOfSquares + ); + + } while (max >= min); + + printf("Done\n"); + + return 0; +} \ No newline at end of file diff --git a/assignment01/project4.c b/assignment01/project4.c index e69de29..21768cc 100644 --- a/assignment01/project4.c +++ b/assignment01/project4.c @@ -0,0 +1,50 @@ +#include + +#define ARRAY_SIZE 5 + +void printArray(double arr[]) { + for (int i = 0; i < 5; i++) { + printf("%.1f, ", arr[i]); + } + printf("\n"); +} + +void copy_arr(double target[], double source[], int size) { + for (int i = 0; i < size; i++) { + target[i] = source[i]; + } + + printArray(target); +} + +void copy_ptr(double *target, double *source, int size) { + for (int i = 0; i < size; i++) { + *(target + i) = *(source + i); + } + + printArray(target); +} + +void copy_ptrs(double *target, double *source, double *lastElement) { + while (source <= lastElement) { + target = source; + source++; + target++; + } + + printArray(target); +} + +int main(void) { + + double source[ARRAY_SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5}; + double target1[ARRAY_SIZE]; + double target2[ARRAY_SIZE]; + double target3[ARRAY_SIZE]; + + copy_arr(target1, source, ARRAY_SIZE); + copy_ptr(target2, source, ARRAY_SIZE); + copy_ptrs(target3, source, source + ARRAY_SIZE); + + return 0; +} \ No newline at end of file