Assignment 3-A

Function Library

Create a collection of utility functions demonstrating various concepts including function definitions, parameter passing, return values, scope, and recursive algorithms in C programming.

4-5 hours
Intermediate
100 Points
Submit Assignment
What You'll Practice
  • Function declarations and definitions
  • Pass by value parameters
  • Return values and types
  • Recursive function design
  • Function prototypes and headers
Contents
01

Assignment Overview

Functions are the building blocks of modular programming in C. In this assignment, you will create a library of useful utility functions that can be reused across multiple programs. This comprehensive project requires you to apply ALL concepts from Module 3: function declarations, definitions, prototypes, parameter passing, return values, scope, storage classes, and recursion.

No Standard Library Shortcuts: For string utilities, you must NOT use <string.h> functions. Implement strlen, strcpy, strcmp, and strrev from scratch to demonstrate your understanding.
Skills Applied: This assignment tests your understanding of Functions (3.1), Storage Classes (3.2), and Scope and Lifetime (3.3) from Module 3.
Function Basics (3.1)

Declaration, definition, prototypes, calling conventions

Storage Classes (3.2)

auto, static, extern, register keywords

Scope and Lifetime (3.3)

Local, global, block scope, variable lifetime

Ready to submit? Already completed the assignment? Submit your work now!
Submit Now
02

Tasks

Your function library must implement ALL of the following modules. Each file will be tested individually.

1
Math Utilities

Create math_utils.c with the following functions:

  • int max(int a, int b) - Returns the larger of two numbers
  • int min(int a, int b) - Returns the smaller of two numbers
  • int abs_val(int n) - Returns absolute value
  • double power(double base, int exp) - Returns base raised to exp
  • int gcd(int a, int b) - Returns greatest common divisor
// math_utils.c - Example structure
#include <stdio.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}

int min(int a, int b) {
    // Your implementation
}

// ... more functions

int main() {
    // Test all functions
    printf("max(5, 3) = %d\n", max(5, 3));  // 5
    return 0;
}
2
Number Analysis

Create number_analysis.c with:

  • int is_prime(int n) - Returns 1 if prime, 0 otherwise
  • int is_even(int n) - Returns 1 if even, 0 if odd
  • int digit_count(int n) - Returns number of digits
  • int sum_of_digits(int n) - Returns sum of all digits
  • int reverse_number(int n) - Returns reversed number
// number_analysis.c - Example
int is_prime(int n) {
    if (n <= 1) return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}

int main() {
    printf("is_prime(17) = %d\n", is_prime(17));  // 1
    printf("is_prime(4) = %d\n", is_prime(4));    // 0
    return 0;
}
3
Recursive Functions

Create recursive.c with:

  • int factorial(int n) - Recursive factorial calculation
  • int fibonacci(int n) - Recursive nth Fibonacci number
  • int sum_to_n(int n) - Recursive sum from 1 to n
  • void print_binary(int n) - Print number in binary recursively
Important: Each function MUST use recursion, not iteration! You will receive zero points for iterative solutions.
// recursive.c - Example
int factorial(int n) {
    if (n <= 1) return 1;       // Base case
    return n * factorial(n - 1); // Recursive case
}

void print_binary(int n) {
    if (n > 1) print_binary(n / 2);
    printf("%d", n % 2);
}
4
String Utilities (without string.h)

Create string_utils.c with custom implementations:

  • int str_length(char str[]) - Returns string length
  • void str_reverse(char str[]) - Reverses string in place
  • int str_compare(char s1[], char s2[]) - Compares two strings
  • void str_copy(char dest[], char src[]) - Copies source to destination
Restriction: Do NOT use <string.h> - implement all string operations from scratch!
// string_utils.c - Example
int str_length(char str[]) {
    int len = 0;
    while (str[len] != '\0') {
        len++;
    }
    return len;
}

int main() {
    printf("Length: %d\n", str_length("hello"));  // 5
    return 0;
}
5
Main Driver Program

Create main.c that:

  • Demonstrates all functions from all utility files
  • Implements a menu system for user to test different utilities
  • Handles user input validation gracefully
  • Uses proper function prototypes in a header file or at top of main.c
// main.c - Menu system example
#include <stdio.h>

// Function prototypes
int max(int a, int b);
int is_prime(int n);
int factorial(int n);
int str_length(char str[]);

int main() {
    int choice;
    
    printf("=== Function Library ===\n");
    printf("1. Math Utilities\n");
    printf("2. Number Analysis\n");
    printf("3. Recursive Functions\n");
    printf("4. String Utilities\n");
    printf("5. Exit\n");
    printf("Choice: ");
    scanf("%d", &choice);
    
    // Handle menu selection...
    return 0;
}
03

Submission

Create a public GitHub repository with the exact name shown below:

Required Repository Name
c-function-library
github.com/<your-username>/c-function-library
Required Files
c-function-library/
├── math_utils.c        # 5 math utility functions
├── number_analysis.c   # 5 number analysis functions
├── recursive.c         # 4 recursive functions
├── string_utils.c      # 4 string utility functions
├── main.c              # Driver program with menu system
└── README.md           # REQUIRED - see contents below
README.md Must Include:
  • Your full name and submission date
  • Brief description of each utility file and its functions
  • Compilation instructions (gcc commands)
  • Any challenges faced and how you solved them
Do Include
  • All 18+ functions implemented and working
  • Comments explaining complex logic
  • Test cases in main() for each function
  • Proper function prototypes
  • Clean, indented, readable code
  • README.md with all required sections
Do Not Include
  • Compiled binaries (.exe, .out files)
  • Object files (.o files)
  • IDE-specific folders (.vscode, .idea)
  • Code that does not compile without errors
  • Iterative solutions for recursive tasks
  • string.h usage in string_utils.c
Important: Before submitting, compile and test all your code to ensure it runs without errors. Use: gcc -o main main.c math_utils.c number_analysis.c recursive.c string_utils.c
Submit Your Assignment

Enter your GitHub username - we'll verify your repository automatically

04

Grading Rubric

Your assignment will be graded on the following criteria:

Criteria Points Description
Math Utilities 20 All 5 functions work correctly with various inputs
Number Analysis 20 All 5 functions work correctly, including edge cases
Recursive Functions 25 Proper recursion used (not iteration), correct base cases
String Utilities 20 Custom implementations without string.h, handles edge cases
Main Driver 15 Menu system works, proper testing, input validation
Total 100

Ready to Submit?

Make sure you have completed all requirements and reviewed the grading rubric above.

Submit Your Assignment
05

What You Will Practice

Function Design (3.1)

Writing modular functions with proper declarations, definitions, prototypes, parameters, and return values

Storage Classes (3.2)

Understanding auto, static, extern, and register keywords for controlling variable storage and visibility

Scope and Lifetime (3.3)

Managing local vs global variables, block scope, and understanding when variables are created and destroyed

Recursive Thinking

Breaking problems into base cases and recursive cases, understanding call stack behavior

06

Pro Tips

C Best Practices
  • Use meaningful function and variable names
  • Always declare function prototypes
  • Handle edge cases (negative numbers, zero, empty strings)
  • Test functions with multiple inputs
Recursion Tips
  • Always define a clear base case first
  • Ensure recursive calls move toward base case
  • Trace through small examples on paper
  • Watch for stack overflow with large inputs
Time Management
  • Start with the simplest functions first
  • Build and test incrementally
  • Leave time for the menu driver program
  • Test compilation frequently
Common Mistakes
  • Forgetting to handle negative numbers in digit functions
  • Using loops instead of recursion
  • Missing null terminator in string operations
  • Integer overflow in factorial for large n
07

Pre-Submission Checklist

Code Requirements
Repository Requirements