Snippets Collections
#include <stdio.h>
#include <string.h>

struct Product
{
    char name [20];
    char brand [20];
    float price ;
    int quantity;

};

void print_product(struct Product a_product);

int main(void)
{
    struct Product a_product[3];
    
    sprintf(a_product[0].name,"Xbox One S");
    sprintf(a_product[0].brand,"Microsoft");
    a_product[0].price= 395.50;
    a_product[0].quantity = 35;
    
    sprintf(a_product[1].name,"PlayStation 4 Pro");
    sprintf(a_product[1].brand,"Sony");
    a_product[1].price= 538.00;
    a_product[1].quantity = 71;
    
    sprintf(a_product[2].name,"Switch");
    sprintf(a_product[2].brand,"Nintendo");
    a_product[2].price= 529.95;
    a_product[2].quantity = 8;
    
    for(int i = 0; i < 3; i++)
    {
        print_product(a_product[i]);
    }
	return 0;
}

void print_product(struct Product a_product)
{
	printf("Item name: %s\n", a_product.name);
	printf("Brand:     %s\n", a_product.brand);
	printf("Price:     $%.2f\n", a_product.price);
	printf("Quantity:  %d\n\n", a_product.quantity);
}
#include <stdio.h>
#include <math.h>

struct Circle
{
	float radius;
	float x_position;
	float y_position;
	
};

float distance_between(struct Circle c1, struct Circle c2);

int main(void)
{
	struct Circle c1;
	struct Circle c2;

    float r1 = 0;
    float r2 = 0;
    scanf("%f", &r1);
    scanf("%f", &r2);
	c1.radius = r1;
	c2.radius = r2;
	c1.x_position = 11;
	c1.y_position = 22;
	c2.x_position = 4;
	c2.y_position = 6;

	float distance = distance_between(c1, c2);
	printf("distance between two circle is %f.", distance);

	return 0;
}

float distance_between(struct Circle c1, struct Circle c2)
{
    float distance;
    distance = sqrt( pow((c1.x_position - c2.x_position),2) + pow((c1.y_position - c2.y_position),2)) - c1.radius - c2.radius;
    return distance;
}

#include <stdio.h>

struct SoftDrink
{
    char name[17];
    int size;
    int energy;
    float caffeine;
    int intake;

};

void print_soft_drink(struct SoftDrink* drink);

int main(void)
{
    struct SoftDrink drink = { "Life Modulus",250,529,80.50,500};
    
    print_soft_drink(&drink);
    
    return 0;
    
}

void print_soft_drink(struct SoftDrink *drink)
{   
    printf("A soft drink...\n\n");
    printf("Name: %s\n",drink->name);
    printf("Serving size: %d mL\n",drink->size);
    printf("Energy content: %d kJ\n",drink->energy);
    printf("Caffeine content: %f mg\n",drink->caffeine);
    printf("Maximum daily intake: %d mL\n",drink->intake);

}
#include <stdio.h>

// Function declarations
void draw_top_bottom_border(char corner_char, char horizontal_char, int width);
void draw_vertical_sides(char vertical_char, int width, int height);
void draw_ascii_box(char horizontal_char, char vertical_char, char corner_char, int width, int height);

// Function to draw the top and bottom borders
void draw_top_bottom_border(char corner_char, char horizontal_char, int width) 
{
    printf("%c", corner_char);
    for (int i = 0; i < width - 2; i++) 
    {
        printf("%c", horizontal_char);
    }
    printf("%c\n", corner_char);
}

// Function to draw the vertical sides
void draw_vertical_sides(char vertical_char, int width, int height) 
{
    for (int i = 0; i < height - 2; i++) 
    {
        printf("%c", vertical_char);
        for (int j = 0; j < width - 2; j++) 
        {
            printf(" ");
        }
        printf("%c\n", vertical_char);
    }
}

// Function to draw the ASCII box
void draw_ascii_box(char horizontal_char, char vertical_char, char corner_char, int width, int height) {

    
    // Draw top border
    draw_top_bottom_border(corner_char, horizontal_char, width);
    
    // Draw vertical sides
    draw_vertical_sides(vertical_char, width, height);
    
    // Draw bottom border
    draw_top_bottom_border(corner_char, horizontal_char, width);
}

// Main function to get user inputs and test the draw_ascii_box function
int main() {
    char horizontal_char;
    char vertical_char;
    char corner_char;
    int width;
    int height;

    // Get user inputs
    
    scanf(" %c", &horizontal_char);

    scanf(" %c", &vertical_char);

    scanf(" %c", &corner_char);

    scanf("%d", &width);

    scanf("%d", &height);

    // Draw the ASCII box based on user inputs
    draw_ascii_box(horizontal_char, vertical_char, corner_char, width, height);

    return 0;
}
#include <stdio.h>

// Define the structure Triangle
struct Triangle 
{
    int height;
    char inner_symbol;
};

// Function to print the inverted triangle
void print_inverted(struct Triangle inverted_triangle) {
    int height = inverted_triangle.height;
    char symbol = inverted_triangle.inner_symbol;

    // Print the top line
    for (int i = 0; i < 2 * height; i++) 
    {
        printf("_");
    }
    printf("\n");

    // Print the inverted triangle
    for (int i = 0; i < height; i++) 
    {
        // Print leading spaces
        for (int j = 0; j < i; j++) 
        {
            printf(" ");
        }
        
        // Print the left side of the triangle
        printf("\\");
        
        // Print the inner symbols
        for (int j = 0; j < 2 * (height - i - 1); j++) 
        {
            printf("%c", symbol);
        }
        
        // Print the right side of the triangle
        printf("/\n");
    }
}

int main() 
{
    struct Triangle my_triangle;
    
    // Query the user for the desired inverted triangle height and symbol
    printf("Inverted triangle's height?\n");
    scanf("%d", &my_triangle.height);
    
    printf("Inverted triangle's symbol?\n");
    scanf(" %c", &my_triangle.inner_symbol);
    
    // Call the print_inverted function
    print_inverted(my_triangle);
    
    return 0;
}
#include <stdio.h>

// Define the structure Triangle
struct Triangle {
    int height;
    char inner_symbol;
};

// Function to print the inverted triangle
void print_inverted(struct Triangle inverted_triangle) {
    int height = inverted_triangle.height;
    char symbol = inverted_triangle.inner_symbol;

    // Print the top line
    for (int i = 0; i < 2 * height; i++) {
        printf("_");
    }
    printf("\n");

    // Print the inverted triangle
    for (int i = 0; i < height; i++) {
        // Print leading spaces
        for (int j = 0; j < i; j++) {
            printf(" ");
        }
        
        // Print the left side of the triangle
        printf("\\");
        
        // Print the inner symbols
        for (int j = 0; j < 2 * (height - i - 1); j++) {
            printf("%c", symbol);
        }
        
        // Print the right side of the triangle
        printf("/\n");
    }
}

int main() 
{
    struct Triangle my_triangle;
    
    // Query the user for the desired inverted triangle height and symbol
    printf("Inverted triangle's height?\n");
    scanf("%d", &my_triangle.height);
    
    printf("Inverted triangle's symbol?\n");
    scanf(" %c", &my_triangle.inner_symbol);
    
    // Call the print_inverted function
    print_inverted(my_triangle);
    
    return 0;
}
#include <stdio.h>
#include <math.h>

struct Point3D
{
    float x;
    float y;
    float z;
};

float compute_distance3d(struct Point3D p1, struct Point3D p2);

int main(void)
{
    struct Point3D p1;
    struct Point3D p2;

    // Initialize the structures with test values
    p1.x = 1.0;
    p1.y = 2.0;
    p1.z = 3.0;

    p2.x = 4.0;
    p2.y = 5.0;
    p2.z = 6.0;

    // Calculate the distance between the test values
    float distance = compute_distance3d(p1, p2);

    // Query user for input coordinates
    printf("Enter coordinates for Point1 (x y z): ");
    scanf("%f %f %f", &p1.x, &p1.y, &p1.z);

    printf("Enter coordinates for Point2 (x y z): ");
    scanf("%f %f %f", &p2.x, &p2.y, &p2.z);

    // Calculate and print the distance based on user input
    distance = compute_distance3d(p1, p2);
    printf("Distance between the two points is %f.\n", distance);

    return 0;
}

float compute_distance3d(struct Point3D p1, struct Point3D p2)
{
    return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2) + pow(p2.z - p1.z, 2));
}
#include <stdio.h>
#include <string.h>

struct Student
{
    char name [50]; //declare as character array with space for 50 characters
    float lecture_attendance;
    float lab_attendance;
};

int main(void)
{
    struct Student student;
    
    sprintf(student.name,"Jane Doe"); //use sprintf to format and store the string into the name array of the student structure.
    student.lecture_attendance = 0.33f;
    student.lab_attendance = 1.00f;
    printf("Successful!!!");
    return 0;
}
#include <stdio.h>

struct Movie
{
    char title [50];
    int minutes;
    float tomatometer;
};

void print_movie(struct Movie a_super_hero);

int main(void)
{
    struct Movie movie;

	sprintf(movie.title, "Batman Returns");
	movie.minutes = 126;
	movie.tomatometer = 0.81f;

	print_movie(movie);
	
	return 0;
}

void print_movie(struct Movie a_movie)
{
    printf("Movie title:        %s\n",a_movie.title);
    printf("Runtime in minutes: %d\n",a_movie.minutes);
    printf("Tomatometer Score:  %.2f",a_movie.tomatometer);
}
#include <stdio.h>

enum Monster_Type
{
    ZOMBIE, WEREWOLF, VAMPIRE, MUMMY, CHANGELING
};

struct Monster
{
    char type [20];
    int age; 
    int power; 
    float speed;
    int stamina;
};

struct Monster create_monster(enum Monster_Type type);

void print_monster(struct Monster monster);

int main(void)
{
	struct Monster monster1 = create_monster(CHANGELING);
	print_monster(monster1);

	struct Monster monster2 = create_monster(MUMMY);
	print_monster(monster2);

	struct Monster monster3 = create_monster(WEREWOLF);
	print_monster(monster3);

	struct Monster monster4 = create_monster(VAMPIRE);
	print_monster(monster4);

	struct Monster monster5 = create_monster(ZOMBIE);
	print_monster(monster5);

	return 0;
}

// TODO: Define functions here:
struct Monster create_monster(enum Monster_Type type)
{   
    struct Monster monster;
    
    if(type == CHANGELING)
    {
        sprintf(monster.type,"Changeling");
        monster.age = 21;
        monster.power = 86;
        monster.speed = 6.77;
        monster.stamina = 4057;
    }
    if(type == MUMMY)
    {
        sprintf(monster.type,"Mummy");
        monster.age = 3793;
        monster.power = 97;
        monster.speed = 0.37;
        monster.stamina = 492;
    }
    if(type == WEREWOLF)
    {
        sprintf(monster.type,"Werewolf");
        monster.age = 70;
        monster.power = 144;
        monster.speed = 20.53;
        monster.stamina = 4628;
    }
    if(type==VAMPIRE)
    {
        sprintf(monster.type,"Vampire");
        monster.age = 89;
        monster.power = 260;
        monster.speed = 10.08;
        monster.stamina = 3926;
    }

    if(type==ZOMBIE)
    {
        sprintf(monster.type,"Zombie");
        monster.age = -1;
        monster.power = 1;
        monster.speed = 0.30;
        monster.stamina = 173;
    }
    return monster;
}

void print_monster(struct Monster monster)
{
    printf("Monster Type: %s\n",monster.type);
	printf("         Age: %d years\n",monster.age);
	printf("       Power: %d \n",monster.power);
	printf("       Speed: %.2f \n",monster.speed);
	printf("     Stamina: %d \n",monster.stamina);
}
struct MyStruct {
  isHappy: bool
}

trait Noisy {
  fn get_noise(&self) -> &str
}

impl Noisy for MyStruct {
  fn get_noise(&self) -> &str { "Meow?" }
}

****** DEFAULT TRAIT IMPL
trait Run {
  // no properties - use setter,getter
  fun run(&self) {
    // exe
  }
}
struct Robot {}
impl Run for Robot {}

fn main() {
  let robot = Robot {};
  robot.run();
}


Robot::Run::run() ??? this works ???

star

Wed Jun 12 2024 04:14:25 GMT+0000 (Coordinated Universal Time)

#c #function #struct
star

Wed Jun 12 2024 03:19:24 GMT+0000 (Coordinated Universal Time)

#c #function #struct
star

Wed Jun 12 2024 03:12:37 GMT+0000 (Coordinated Universal Time)

#c #function #struct
star

Tue Jun 11 2024 09:00:43 GMT+0000 (Coordinated Universal Time)

#c #struct
star

Tue Jun 11 2024 08:54:10 GMT+0000 (Coordinated Universal Time)

#c #struct
star

Tue Jun 11 2024 08:54:09 GMT+0000 (Coordinated Universal Time)

#c #struct
star

Tue Jun 11 2024 04:02:57 GMT+0000 (Coordinated Universal Time)

#c #struct
star

Mon Jun 10 2024 12:26:16 GMT+0000 (Coordinated Universal Time)

#c #struct
star

Mon Jun 10 2024 12:15:06 GMT+0000 (Coordinated Universal Time)

#c #enum #struct
star

Mon Jun 10 2024 12:06:37 GMT+0000 (Coordinated Universal Time)

#c #enum #struct
star

Fri Jul 22 2022 01:44:36 GMT+0000 (Coordinated Universal Time) https://www.udemy.com/course/ultimate-rust-crash-course/learn/lecture/17981939#overview

#struct

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension