Posts

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; }