Posts

Showing posts from October, 2022

C: Using realloc() To Resize An Array Of Ints.

Image
 https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/realloc/realloc.c // Dynamic Memory Allocation - realloc // ----------------------------------- // This program accepts user input to create a dynamic array of ints. // calloc zeroes the space first! #include <stdio.h> #include <stdlib.h> // calloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = calloc(num_ints, sizeof(int)); // Displays zeroed data first: printf("Using calloc zeroes all the data first:\n"); for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i, a[i]); printf("\n"); // new line! // Use for loop to a

C: Using calloc() For Dynamic Memory Allocation.

Image
Video demonstration:  https://youtu.be/xhnOzx4TzH4  https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/calloc/calloc.c // Dynamic Memory Allocation - calloc // ---------------------------------- // This program accepts user input to create a dynamic array of ints. // calloc zeroes the space first! #include <stdio.h> #include <stdlib.h> // calloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = calloc(num_ints, sizeof(int)); // Displays zeroed data first: printf("Using calloc zeroes all the data first:\n"); for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i, a[i]); printf("\n

C: Using malloc() For Dynamic Memory Allocation.

Image
Video demonstration:  https://youtu.be/DUt2YFVxk5g https://github.com/pereiradaniel/c_programs/blob/master/dyn_mem/malloc/malloc.c // Dynamic Memory Allocation - malloc // ---------------------------------- // This program accepts user input to create a dynamic array of ints. #include <stdio.h> #include <stdlib.h> // malloc! int main(int argc, char* argv[]) { // Prompt user for number of ints for dynamic memory allocation: int num_ints = 0; printf("This program will dynamically allocate memory for an array of ints.\nHow many ints would you like? "); scanf(" %d", &num_ints); // Create a dynamically allocated array of integers using user input: int *a = malloc(sizeof(int) * num_ints); // Use for loop to assign values to the int array a. for (int i=0; i<num_ints; ++i) a[i] = i; // Use for loop to print contents of array a. for (int i=0; i<num_ints; ++i) printf("a[%d] = %d\n", i

C: One rep max calculator.

Image
// Based on user input, calculate and output a scale of 50%-90% of 1RM #include <stdio.h> int main(int argc, int argv[]) { double onerm = 0; // stores user input for one rep max printf("Enter your one rep max: \n"); scanf("%lf", &onerm); for (int i=5; i <= 9; ++i) // Loop from 50% to 90% of one rep max printf("%.2f%% = %.2f lbs\n", i*0.1*100, i*0.1*onerm); return 0;  }  

8 Bit: It's Magic 2! - Commodore 64, Final Boss Battle.

Image

8 Bit: Portal for Commodore 64 [50hz Longplay]

Image
Longplay of Portal for the Commodore 64 at 50hz. Played on a THEC64 from Retro Games.

C: Constant pointers and pointer to a constant.

  https://github.com/pereiradaniel/c_programs/blob/master/consts_ptrs.c // Illustrate the differences between constant ptr and ptr to a constant #include <stdio.h> int main(int argc, char* argv[]) { char a = 'a'; char b = 'b'; char*const const_ptr = &a; // constant pointer // can't change what it points to // can change the value *const_ptr = 'x'; printf("a: %c\n", a); char const *ptr_to_const = &a; // pointer to constant printf("*ptr_to_const: %c\n", *ptr_to_const); // dereference ptr ptr_to_const = &b; printf("*ptr_to_const: %c\n", *ptr_to_const); // dereference ptr const char *const const_ptr_to_const = &a; // can't dereference this pointer because it is a pointer to a constant! // can't change what the pointer points to! return 0; }

C: Simple Swap Function Using Pointers

https://github.com/pereiradaniel/c_programs/blob/master/pointers2.c #include <stdio.h> // Pass by reference swap function void swap(int *a, int*b) { printf("a: %p\nb: %p\n", a, b); printf("*a: %d\n*b: %d\n", *a, *b); int temp = 0; temp = *a; *a = *b; *b = temp; } int main(int argc, char* argv[]) { // int a,b,c; // a=b=c=0; // printf("Enter 3 numbers: "); // scanf("%d %d %d", &a, &b, &c); // pass by reference // printf("Result: %d\n", a+b+c); int x, y; x = 5; y = 10; printf("x: %d, y: %d.\n", x, y); printf("&x: %p\n&y: %p\n", &x, &y); swap(&x,&y); printf("swap! x: %d, y: %d.\n", x, y); return 0; }

C: Pointers and Dynamic Allocation

https://github.com/pereiradaniel/c_programs/blob/master/pointers3.c   #include <stdio.h> #include <stdlib.h> // malloc int main() { int *a; // declare a pointer to a dynamically allocated array int length = 0; printf("Enter a length: "); scanf("%d", &length); // allocate space dynamically a = malloc(length * sizeof(int)); // allocates space on the heap printf("a: %p\n", a); for (int i=0; i<length; ++i) a[i] = i; for (int i=0; i<length; ++i) printf("a[%d]=%d\n", i, a[i]); free(a); // deallocate memory return 0; } // ==320== Memcheck, a memory error detector // ==320== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. // ==320== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info // ==320== Command: ./a.out // ==320== // Enter a length: 5 // a: 0x4a4e8c0 // a[0]=0 // a[1]=1 // a[2]=2 // a[3]=3 // a[4]=4 // ==320== // =

C: Calculate the perimeter of a rectangle.

  https://github.com/pereiradaniel/c_programs/blob/master/rect_perim.c // Calculate the perimeter of a rectangle using user input. #include <stdio.h> // Returns the perimeter of a rectangle. double rect_perim(double l, double w) { return 2*(l+w); } int main(int arg, char* argv[]) { double length, width; // perimeter; printf("Length: "); scanf("%lf", &length); printf("Width: "); scanf("%lf", &width); // perimeter = rect_perim(length, width); printf("Perimeter: %.2f\n", rect_perim(length, width)); return 0; } // ==132== Memcheck, a memory error detector // ==132== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. // ==132== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info // ==132== Command: ./a.out // ==132== // Length: 25 // Width: 15 // Perimeter: 80.00 // ==132== // ==132== HEAP SUMMARY: // ==132== in use at exit: 0 bytes in 0 blocks // ==13

C: length.h module.

Usage: int array[] = { 9 , 4 , 8 , 1 , 7 , 0 , 3 , 2 , 5 , 6 }; // test array int length = LENGTH (array);  https://github.com/pereiradaniel/c_programs/blob/master/length.h #define LENGTH(a) sizeof(a) / sizeof(a[0]) // function-like macro

C: Implement merge sort.

https://github.com/pereiradaniel/c_programs/blob/master/merge_sort.c // Implement merge sort algorithm. #include <stdio.h> #include "length.h" void merge_sort(int a[], int length); void merge_sort_recursion(int a[], int l, int r); void merge_sorted_arrays(int a[], int l, int m, int r); int main(int argc, char* argv[]) { int array[] = {9,4,8,1,7,0,3,2,5,6}; // test array int length = LENGTH(array); // Sort array using merge_sort: merge_sort(array, length); // Print array: for(int i=0; i < length; ++i) printf("%d", array[i]); printf("\n"); return 0; } // Perform a merge sort of the array using the given length. void merge_sort(int a[], int length) { // Call the merge_sort_recursion function to sort array: // - Initially we will want to use the whole array. // - Use left index of 0 and right index of -1. merge_sort_recursion(a, 0, length - 1); } // Recursive portion of the algorithm: