Week 7 Code Examples
#include <stdio.h>
void increment_all(int nums[], int length) {
for (int i = 0; i < length; i++) {
nums[i]++;
}
}
int main(void) {
int array[5] = {1, 2, 3, 4, 5};
printf("%d\n", array[3]);
increment_all(array, 5);
printf("%d\n", array[3]);
return 0;
}
#include <stdio.h>
int main(void) {
int* number_ptr;
char *letter_ptr;
int number = 6;
char letter = 'c';
number_ptr = &number;
letter_ptr = &letter;
printf("number is %d and is stored at %p\n", number, number_ptr);
printf("letter is %c and is stored at %p\n", letter, letter_ptr);
return 0;
}
#include <stdio.h>
int main(void) {
int number = 6;
int *number_ptr = &number;
printf("number_ptr deferences to %d, and is stored at %p\n", *number_ptr, number_ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_node(int data) {
struct node *new_node = malloc(sizeof(struct node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void free_linked_list(struct node *head) {
struct node *current = head;
struct node *prev = head;
while (current != NULL) {
current = current->next;
free(prev);
prev = current;
}
}
void print_linked_list(struct node *head) {
struct node *current = head;
while (current != NULL) {
printf("%d->", current->data);
current = current->next;
}
printf("X\n");
return;
}
struct node *insert_at_beginning(struct node *node_to_insert, struct node *head) {
if (head == NULL) {
head = node_to_insert;
return head;
}
node_to_insert->next = head;
head = node_to_insert;
return head;
}
struct node *insert_at_end(struct node *node_to_insert, struct node *head) {
struct node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = node_to_insert;
return head;
}
struct node *insert_after_value(struct node *node_to_insert, struct node *head, int value) {
struct node *current = head;
if (head == NULL) {
printf("List is empty, cannot insert after value\n");
return head;
}
while (current != NULL) {
if (current->data == value) {
node_to_insert->next = current->next;
current->next = node_to_insert;
return head;
}
current = current->next;
}
printf("Entire list traversed, %d not found, cannot insert\n", value);
return head;
}
struct node *insert_before_value(struct node *node_to_insert, struct node *head, int value) {
if (head == NULL) {
printf("List is empty, cannot insert after value\n");
return head;
}
struct node *current = head;
while (current != NULL) {
if (current->next->data == value) {
node_to_insert->next = current->next;
current->next = node_to_insert;
return head;
}
current = current->next;
}
printf("Entire list traversed, %d not found, cannot insert\n", value);
return head;
}
int main(void) {
struct node *my_node = create_node(5);
struct node *second_node = create_node(67);
struct node *third_node = create_node(100);
struct node *fourth_node = create_node(151);
struct node *fifth_node = create_node(1000);
struct node *sixth_node = create_node(17);
struct node *seventh_node = create_node(180);
struct node *head = insert_at_beginning(my_node, NULL);
head = insert_at_beginning(second_node, head);
head = insert_at_beginning(third_node, head);
head = insert_at_end(fourth_node, head);
head = insert_at_end(fifth_node, head);
head = insert_after_value(sixth_node, head, 5);
head = insert_before_value(seventh_node, head, 5);
print_linked_list(head);
free_linked_list(head);
return 0;
}
#include <stdio.h>
int sum(void) {
return x + y;
}
int main(void) {
int x = 12;
int y = 13;
sum();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x = 10;
int *ptr = malloc(x * sizeof(int));
printf("%p\n", ptr);
return 0;
}
#include <stdio.h>
#include "maths.h"
int main(void) {
int area = rect_area();
printf("The area is: %d\n", area);
double c_area = circle_area();
printf("The area is: %lf\n", c_area);
double t_area = triangle_area();
printf("The area is: %lf\n", t_area);
return 0;
}
#include <stdio.h>
#include "maths.h"
int rect_area(void) {
int height;
int width;
int area;
printf("Enter the width and height of the rectangle: ");
scanf("%d %d", &height, &width);
area = height * width;
return area;
}
double circle_area(void) {
double radius;
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
double area = PI * (radius * radius);
return area;
}
double triangle_area(void) {
int base;
int height;
printf("Enter the height of your triangle: ");
scanf("%d", &height);
printf("Enter the base of your triangle: ");
scanf("%d", &base);
double area = 0.5 * base * height;
return area;
}
#include <stdio.h>
#define PI 3.141592653589
// This function calculates the area of a rectangle
// it scans in a height and width and returns the result
int rect_area(void);
// THis function calculates teh area of a circle
// in scans in a radius and resturns the result
double circle_area(void);
// This function calculates the area of a triangle
// it scans in a base and height and returns the result
double triangle_area(void);
#include <stdio.h>
int main(void) {
int box = 12;
int *box_add = &box;
int area = *box_add * box;
printf("%d\n", area);
return 0;
}
#include <stdio.h>
int sum(int x, int y) {
return x+y;
}
int main(void) {
int x;
int *x_ptr;
x = 17;
x_ptr = &x;
x = 20;
printf("x * 12 = %d", *x_ptr * 12);
int number4 = sum(12, 13);
return 0;
}
#include <stdio.h>
int main(void) {
int array[10] = {0};
// Display the size of various data types and variables.
printf("The size of an int is %lu bytes\n", sizeof(int));
printf("The size of an array of int is %lu bytes\n", sizeof(array));
printf("The size of 10 ints is %lu bytes\n", 10 * sizeof(int));
printf("The size of a double is %lu bytes\n", sizeof(double));
printf("The size of a char is %lu bytes\n", sizeof(char));
return 0;
}
#include <stdio.h>
struct movie {
int runtime;
int stars;
};
int main(void) {
struct movie ht;
struct movie *httyd = &ht;
(*httyd).runtime = 120;
(*httyd).stars = 5;
printf("How to Train Your Dragon is %d minutes and a rating of %d stars\n", httyd->runtime, httyd->stars);
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX 35
// 1. Define the struct
struct pet {
char name[MAX];
char species[MAX];
int age;
double weight;
};
int main(void) {
struct pet my_pet;
struct pet *pet_ptr = &my_pet;
strcpy(pet_ptr->name, "Buddy");
strcpy(pet_ptr->species, "Dog");
pet_ptr->age = 3;
pet_ptr->weight = 12.50;
printf("--- Pet Record ---\n");
printf("Name: %s\n", pet_ptr->name);
printf("Species: %s\n", pet_ptr->species);
printf("Age: %d years old\n", pet_ptr->age);
printf("Weight: %.2lf kg\n", pet_ptr->weight);
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX 35
// 1. Define the struct
struct pet {
char name[MAX];
char species[MAX]; // "Dog" or "Cat" or ???
int age;
double weight;
};
int main(void) {
struct pet my_pet;
struct pet *pet_ptr = &my_pet;
strcpy((*pet_ptr).name, "Buddy");
strcpy((*pet_ptr).species, "Dog");
(*pet_ptr).age = 3;
(*pet_ptr).weight = 12.50;
printf("--- Pet Record ---\n");
printf("Name: %s\n", (*pet_ptr).name);
printf("Species: %s\n", (*pet_ptr).species);
printf("Age: %d years old\n", (*pet_ptr).age);
printf("Weight: %.2lf kg\n", (*pet_ptr).weight);
return 0;
}
#include <stdio.h>
void swapsies(int* x, int* y) {
int temp = *x;
*x = *y;
*y = temp;
return;
}
int main(void) {
int x = 12;
int y = 13;
printf("x = %d and y = %d\n", x, y);
swapsies(&x, &y);
printf("x = %d and y = %d\n", x, y);
return 0;
}
#include <stdio.h>
// malloc() and free() are functions in the <stdlib.h> library
#include <stdlib.h>
void read_array(int *numbers, int size);
void reverse_array(int *numbers, int size);
int main (void) {
int size;
printf("How many numbers would you like to scan: ");
scanf("%d", &size);
// Allocate some memory space for my array and return a pointer
// to the first element
int *numbers = malloc(size * sizeof(int));
// Check if there is actually enough space to allocate
// memory, exit the program if there is not enough memory
// to allocate.
if (numbers == NULL) {
printf("Malloc failed, not enough space to allocate memomry\n");
return 1;
}
// Perform some functions here
read_array(numbers, size);
reverse_array(numbers, size);
// Free the allocated memory
// In this case, it would happen on program exit anyway
free(numbers);
return 0;
}
void read_array(int *numbers, int size) {
for (int i = 0; i < size; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
}
void reverse_array(int *numbers, int size) {
int start = 0;
int end = size - 1;
while (start < end) {
int temp = numbers[start];
numbers[start] = numbers[end];
numbers[end] = temp;
start++;
end--;
}
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
}