c

exercises

exercises.c🔧
/*
 * ============================================================================
 * STRUCTURE OF A C PROGRAM - EXERCISES
 * ============================================================================
 * Complete the exercises below to practice C program structure.
 * 
 * Instructions:
 * 1. Read each exercise carefully
 * 2. Write your code in the designated area
 * 3. Compile and test: gcc exercises.c -o exercises -lm && ./exercises
 * 4. Compare your output with the expected output
 * ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/*
 * ============================================================================
 * EXERCISE 1: Preprocessor Directives
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task: 
 * 1. Define a constant named MAX_STUDENTS with value 30
 * 2. Define a constant named SCHOOL_NAME with value "C Programming Academy"
 * 3. Define a macro named DOUBLE(x) that returns x*2
 * 4. Use these in the function below
 * 
 * Expected Output:
 *   School: C Programming Academy
 *   Maximum students: 30
 *   Double of 15: 30
 */

// TODO: Define MAX_STUDENTS here


// TODO: Define SCHOOL_NAME here


// TODO: Define DOUBLE(x) macro here


void exercise_1() {
    printf("\n=== Exercise 1: Preprocessor Directives ===\n");
    
    // TODO: Print the school name using SCHOOL_NAME
    
    
    // TODO: Print the maximum students using MAX_STUDENTS
    
    
    // TODO: Print the double of 15 using DOUBLE macro
    
    
}

/*
 * ============================================================================
 * EXERCISE 2: Global Declarations
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task:
 * 1. Declare a global integer variable named 'programRunCount' initialized to 0
 * 2. Declare a global constant string named 'VERSION' set to "1.0.0"
 * 3. Create a function prototype for 'incrementRunCount'
 * 4. Increment and display the counter in the function
 * 
 * Expected Output:
 *   Program Version: 1.0.0
 *   Run count before: 0
 *   Run count after: 1
 *   Run count after: 2
 */

// TODO: Declare global variable 'programRunCount' here


// TODO: Declare global constant 'VERSION' here


// TODO: Declare function prototype for 'incrementRunCount' here


void exercise_2() {
    printf("\n=== Exercise 2: Global Declarations ===\n");
    
    // TODO: Print the VERSION
    
    
    // TODO: Print "Run count before: " and programRunCount
    
    
    // TODO: Call incrementRunCount() twice and print after each call
    
    
}

// TODO: Define the incrementRunCount function here
// It should increment programRunCount and print the new value


/*
 * ============================================================================
 * EXERCISE 3: Structure Definition
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task:
 * 1. Define a structure named 'Book' with the following members:
 *    - title (char array of 100 characters)
 *    - author (char array of 50 characters)
 *    - price (float)
 *    - pages (int)
 * 2. Create a typedef for the structure
 * 3. Write a function 'printBook' that takes a Book and displays its info
 * 
 * Expected Output:
 *   Book Information:
 *   Title: The C Programming Language
 *   Author: Kernighan & Ritchie
 *   Price: $45.99
 *   Pages: 272
 */

// TODO: Define the Book structure here


// TODO: Create typedef for Book here (or combine with struct definition)


// TODO: Function prototype for printBook


void exercise_3() {
    printf("\n=== Exercise 3: Structure Definition ===\n");
    
    // TODO: Create a Book variable and initialize it with:
    // Title: "The C Programming Language"
    // Author: "Kernighan & Ritchie"
    // Price: 45.99
    // Pages: 272
    
    
    // TODO: Call printBook function
    
    
}

// TODO: Define the printBook function here


/*
 * ============================================================================
 * EXERCISE 4: Enumeration
 * ============================================================================
 * Difficulty: Easy
 * 
 * Task:
 * 1. Define an enumeration named 'Day' with all 7 days of the week
 *    (SUNDAY=0, MONDAY=1, ... SATURDAY=6)
 * 2. Write a function 'getDayName' that takes a Day and returns the name as string
 * 3. Use it to print the names of a few days
 * 
 * Expected Output:
 *   Day 0: Sunday
 *   Day 1: Monday
 *   Day 5: Friday
 *   Today is: Wednesday
 */

// TODO: Define the Day enumeration here


// TODO: Function prototype for getDayName


void exercise_4() {
    printf("\n=== Exercise 4: Enumeration ===\n");
    
    // TODO: Print day names for SUNDAY, MONDAY, and FRIDAY using getDayName
    
    
    // TODO: Create a variable 'today' of type Day, set it to WEDNESDAY
    // Then print "Today is: " followed by the day name
    
    
}

// TODO: Define getDayName function here
// Hint: Use a switch statement or array of strings


/*
 * ============================================================================
 * EXERCISE 5: Complete Program Structure
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Create a mini calculator that follows proper C program structure:
 * 1. Define constants for operation symbols (+, -, *, /)
 * 2. Create a function 'calculate' that takes two numbers and an operation
 * 3. Create a function 'printResult' that displays the result nicely
 * 4. Use proper function prototypes
 * 
 * Expected Output:
 *   Simple Calculator
 *   10 + 5 = 15
 *   10 - 5 = 5
 *   10 * 5 = 50
 *   10 / 5 = 2
 */

// TODO: Define operation constants
#define OP_ADD '+'
#define OP_SUB '-'
// Add more...

// TODO: Function prototypes for calculate and printResult


void exercise_5() {
    printf("\n=== Exercise 5: Complete Program Structure ===\n");
    printf("Simple Calculator\n");
    
    int a = 10, b = 5;
    
    // TODO: Call calculate for each operation and use printResult to display
    // Example: printResult(a, b, OP_ADD, calculate(a, b, OP_ADD));
    
    
}

// TODO: Define calculate function (returns float for division)


// TODO: Define printResult function


/*
 * ============================================================================
 * EXERCISE 6: Function Types
 * ============================================================================
 * Difficulty: Medium
 * 
 * Task: Create different types of functions:
 * 1. void noParamNoReturn() - prints "Hello from function!"
 * 2. int noParamWithReturn() - returns 42
 * 3. void withParamNoReturn(char* msg) - prints the message
 * 4. int withParamWithReturn(int a, int b) - returns sum
 * 
 * Expected Output:
 *   Function 1: Hello from function!
 *   Function 2 returned: 42
 *   Function 3: Custom message here
 *   Function 4 (5+3) returned: 8
 */

// TODO: Function prototypes


void exercise_6() {
    printf("\n=== Exercise 6: Function Types ===\n");
    
    // TODO: Call each function and display results
    
    
}

// TODO: Define all four functions


/*
 * ============================================================================
 * EXERCISE 7: Header File Simulation
 * ============================================================================
 * Difficulty: Hard
 * 
 * Task: Simulate what would be in a header file by:
 * 1. Creating a section with all constants at the top
 * 2. Creating a section with all structure definitions
 * 3. Creating a section with all function prototypes
 * 4. Then implementing a small "library" of math functions
 * 
 * Create these functions:
 * - int factorial(int n)
 * - int fibonacci(int n)
 * - int isPrime(int n)
 * - int gcd(int a, int b)
 * 
 * Expected Output:
 *   Math Library Demo:
 *   Factorial(5) = 120
 *   Fibonacci(7) = 13
 *   isPrime(17) = 1 (true)
 *   isPrime(15) = 0 (false)
 *   GCD(48, 18) = 6
 */

// ===== HEADER SECTION (normally in .h file) =====

// Constants


// Function prototypes (declarations)


// ===== END HEADER SECTION =====


void exercise_7() {
    printf("\n=== Exercise 7: Header File Simulation ===\n");
    printf("Math Library Demo:\n");
    
    // TODO: Test all math functions
    
    
}

// ===== IMPLEMENTATION SECTION (normally in .c file) =====

// TODO: Implement factorial function


// TODO: Implement fibonacci function


// TODO: Implement isPrime function


// TODO: Implement gcd function (use Euclidean algorithm)


// ===== END IMPLEMENTATION SECTION =====


/*
 * ============================================================================
 * BONUS EXERCISE: Multi-File Structure Simulation
 * ============================================================================
 * Difficulty: Hard
 * 
 * Task: In a real project, code is split across multiple files:
 * - main.c (contains main function)
 * - utils.h (header with declarations)
 * - utils.c (implementation)
 * 
 * Simulate this by organizing this exercise with clear sections:
 * 1. Create a "Point" structure for 2D coordinates
 * 2. Functions: createPoint, distance, midpoint, printPoint
 * 3. Use these to work with geometric points
 * 
 * Expected Output:
 *   Point A: (3, 4)
 *   Point B: (7, 1)
 *   Distance between A and B: 5.00
 *   Midpoint: (5, 2.5)
 */

// ===== UTILS.H SECTION =====

// TODO: Define Point structure


// TODO: Function prototypes


// ===== UTILS.C SECTION =====

// TODO: Implement all functions


// ===== MAIN SECTION =====

void bonus_exercise() {
    printf("\n=== BONUS: Multi-File Structure ===\n");
    
    // TODO: Create points A(3,4) and B(7,1)
    // TODO: Print both points
    // TODO: Calculate and print distance
    // TODO: Calculate and print midpoint
    
    
}


/*
 * ============================================================================
 * MAIN FUNCTION
 * ============================================================================
 */
int main() {
    printf("╔════════════════════════════════════════════════╗\n");
    printf("║    STRUCTURE OF C PROGRAM - EXERCISES          ║\n");
    printf("║    Complete each exercise to practice          ║\n");
    printf("╚════════════════════════════════════════════════╝\n");
    
    // Uncomment each exercise as you complete it
    
    // exercise_1();
    // exercise_2();
    // exercise_3();
    // exercise_4();
    // exercise_5();
    // exercise_6();
    // exercise_7();
    // bonus_exercise();
    
    printf("\n=== Uncomment exercises in main() to run them ===\n");
    
    return 0;
}

/*
 * ============================================================================
 * ANSWER KEY HINTS
 * ============================================================================
 * 
 * Exercise 1:
 *   #define MAX_STUDENTS 30
 *   #define SCHOOL_NAME "C Programming Academy"
 *   #define DOUBLE(x) ((x) * 2)
 * 
 * Exercise 3 (Book struct):
 *   typedef struct {
 *       char title[100];
 *       char author[50];
 *       float price;
 *       int pages;
 *   } Book;
 * 
 * Exercise 4 (Day enum):
 *   enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
 * 
 * Exercise 7 (isPrime):
 *   int isPrime(int n) {
 *       if (n <= 1) return 0;
 *       for (int i = 2; i * i <= n; i++) {
 *           if (n % i == 0) return 0;
 *       }
 *       return 1;
 *   }
 * 
 * Exercise 7 (gcd - Euclidean):
 *   int gcd(int a, int b) {
 *       while (b != 0) {
 *           int temp = b;
 *           b = a % b;
 *           a = temp;
 *       }
 *       return a;
 *   }
 * 
 * ============================================================================
 */
Exercises - C Programming Tutorial | DeepML