Assignment 4-A

Array Manipulation

Build a complete array toolkit that demonstrates all Module 4 concepts: array operations, sorting algorithms, searching techniques, matrix operations, and string processing. Master fundamental data structure skills in C.

4-6 hours
Intermediate
120 Points
Submit Assignment
What You Will Practice
  • Array declaration and initialization
  • Sorting algorithms (Bubble, Selection, Insertion)
  • Searching algorithms (Linear, Binary)
  • 2D arrays and matrix operations
  • String arrays and processing
Contents
01

Assignment Overview

In this assignment, you will build an Array Operations Toolkit consisting of multiple C programs that demonstrate mastery of arrays. This comprehensive project requires you to apply ALL concepts from Module 4: array basics, sorting algorithms, searching algorithms, multi-dimensional arrays, and string processing.

Algorithm Focus: This assignment emphasizes understanding how algorithms work. Display intermediate steps in sorting to show the algorithm's progress, and count comparisons in searching.
Skills Applied: This assignment tests your understanding of Arrays (4.1), Multi-dimensional Arrays (4.2), and Strings (4.3) from Module 4.
Arrays (4.1)

Declaration, initialization, traversal, element access

Multi-dimensional (4.2)

2D arrays, matrices, row-column operations

Strings (4.3)

Character arrays, string functions, processing

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

The Scenario

DataCrunch Analytics

You have been hired as a Junior Software Developer at DataCrunch Analytics, a company that builds data processing tools. Your team lead has assigned you this project:

"We need a collection of utility programs that demonstrate fundamental array algorithms. These will serve as reference implementations for our interns. Create programs for statistical analysis, sorting, searching, matrix operations, and string processing. Make sure to show how each algorithm works step by step!"

Your Task

Create a collection of C programs that implement classic array algorithms. Each program must compile without warnings using gcc -Wall -Wextra, demonstrate the algorithm's operation clearly, and follow good coding practices with proper comments.

03

Requirements

Your toolkit must include ALL of the following programs. Each program is mandatory and will be tested individually.

1
Array Statistics (array_stats.c)

Create a program that performs statistical analysis on an integer array:

  • Take array size (n) and n elements from user input
  • Calculate and display: sum, average, minimum, maximum
  • Find and display the second largest and second smallest elements
  • Count and display the number of even and odd elements
  • Find the frequency of each unique element
// Example function signatures
void input_array(int arr[], int size);
int calculate_sum(int arr[], int size);
double calculate_average(int arr[], int size);
int find_min(int arr[], int size);
int find_max(int arr[], int size);
int find_second_largest(int arr[], int size);
void count_even_odd(int arr[], int size, int *even, int *odd);
2
Sorting Algorithms (sorting.c)

Implement three classic sorting algorithms with visualization:

  • void bubble_sort(int arr[], int n) - Bubble Sort
  • void selection_sort(int arr[], int n) - Selection Sort
  • void insertion_sort(int arr[], int n) - Insertion Sort
  • Display the array after each pass to show sorting progress
  • Count and display total number of swaps for each algorithm
  • Allow user to choose which algorithm to run
// Display after each pass
void bubble_sort(int arr[], int n) {
    int swaps = 0;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                swap(&arr[j], &arr[j + 1]);
                swaps++;
            }
        }
        printf("Pass %d: ", i + 1);
        print_array(arr, n);
    }
    printf("Total swaps: %d\n", swaps);
}
3
Searching Algorithms (searching.c)

Implement searching algorithms with comparison counting:

  • int linear_search(int arr[], int n, int key, int *comparisons) - Linear Search
  • int binary_search(int arr[], int n, int key, int *comparisons) - Binary Search
  • Display number of comparisons made for each search
  • For binary search, sort the array first and show the sorted array
  • Display the index where element was found, or -1 if not found
  • Compare efficiency: search same element with both algorithms
// Binary search with comparison count
int binary_search(int arr[], int n, int key, int *comparisons) {
    int low = 0, high = n - 1;
    *comparisons = 0;
    
    while (low <= high) {
        int mid = (low + high) / 2;
        (*comparisons)++;
        
        if (arr[mid] == key) return mid;
        else if (arr[mid] < key) low = mid + 1;
        else high = mid - 1;
    }
    return -1;
}
4
Matrix Operations (matrix.c)

Create a program that performs various matrix operations:

  • void input_matrix(int mat[][MAX], int rows, int cols) - Input matrix
  • void print_matrix(int mat[][MAX], int rows, int cols) - Display matrix
  • void add_matrices(int a[][MAX], int b[][MAX], int result[][MAX], int r, int c) - Addition
  • void multiply_matrices(...) - Matrix multiplication (check dimensions)
  • void transpose_matrix(int mat[][MAX], int trans[][MAX], int r, int c) - Transpose
  • Display results in proper matrix format with aligned columns
#define MAX 10

void print_matrix(int mat[][MAX], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%4d ", mat[i][j]);
        }
        printf("\n");
    }
}
5
String Array Processing (string_array.c)

Create a program that processes an array of strings:

  • Take an array of 5 names from user input
  • Sort names alphabetically using bubble sort (with strcmp)
  • Search for a name (case-insensitive comparison)
  • Find and display the longest and shortest name
  • Reverse each name in place and display
  • Count total characters across all names
#define MAX_NAMES 5
#define MAX_LEN 50

void sort_names(char names[][MAX_LEN], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (strcmp(names[j], names[j + 1]) > 0) {
                char temp[MAX_LEN];
                strcpy(temp, names[j]);
                strcpy(names[j], names[j + 1]);
                strcpy(names[j + 1], temp);
            }
        }
    }
}
6
Array Rotation (rotation.c)

Create a program demonstrating array rotation operations:

  • void rotate_left(int arr[], int n, int k) - Rotate left by k positions
  • void rotate_right(int arr[], int n, int k) - Rotate right by k positions
  • Handle cases where k is greater than n
  • Display array before and after rotation
  • Implement using reversal algorithm for O(n) efficiency
// Efficient rotation using reversal
void reverse(int arr[], int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

void rotate_left(int arr[], int n, int k) {
    k = k % n;  // Handle k > n
    reverse(arr, 0, k - 1);
    reverse(arr, k, n - 1);
    reverse(arr, 0, n - 1);
}
04

Submission

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

Required Repository Name
array-manipulation-toolkit
github.com/<your-username>/array-manipulation-toolkit
Required Files
array-manipulation-toolkit/
├── array_stats.c          # Program 1: Statistical analysis
├── sorting.c              # Program 2: Sorting algorithms
├── searching.c            # Program 3: Searching algorithms
├── matrix.c               # Program 4: Matrix operations
├── string_array.c         # Program 5: String processing
├── rotation.c             # Program 6: Array rotation
├── Makefile               # Build all programs with: make all
└── README.md              # Documentation (see requirements below)
README.md Must Include:
  • Your full name and submission date
  • Brief description of each program
  • Compilation and usage instructions
  • Sample output from at least 3 programs
  • Time complexity analysis for sorting/searching algorithms
Makefile Required:
CC = gcc
CFLAGS = -Wall -Wextra -std=c11

all: array_stats sorting searching matrix string_array rotation

array_stats: array_stats.c
	$(CC) $(CFLAGS) -o array_stats array_stats.c

# ... similar rules for other programs

clean:
	rm -f array_stats sorting searching matrix string_array rotation
Do Include
  • All 6 programs implemented and working
  • Comments explaining algorithm logic
  • Step-by-step output for sorting algorithms
  • Comparison counts for searching algorithms
  • Proper input validation
  • Makefile for easy compilation
  • README.md with all required sections
Do Not Include
  • Compiled binaries or object files
  • Hardcoded array values (must use user input)
  • Sorting without showing passes
  • Searching without counting comparisons
  • Code that does not compile with -Wall -Wextra
  • Missing boundary checks for arrays
Important: Before submitting, test all programs with various inputs including edge cases (empty arrays, single element, all same values).
Submit Your Assignment

Enter your GitHub username - we will verify your repository automatically

05

Grading Rubric

Your assignment will be graded on the following criteria:

Criteria Points Description
Array Statistics 20 All calculations correct, handles edge cases, clean output
Sorting Algorithms 25 All three algorithms work, shows passes, counts swaps
Searching Algorithms 20 Both algorithms work, counts comparisons, handles not found
Matrix Operations 20 All operations work, checks dimensions, proper display format
String Array Processing 15 Sorting, searching, finding longest/shortest all work
Array Rotation 10 Both rotations work, handles k > n, efficient implementation
Code Quality and Documentation 10 Clean code, comments, Makefile, README, no compiler warnings
Total 120

Ready to Submit?

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

Submit Your Assignment
06

What You Will Practice

Array Fundamentals (4.1)

Declaration, initialization, traversal, passing arrays to functions

Sorting Algorithms

Bubble Sort, Selection Sort, Insertion Sort, algorithm visualization

Searching Algorithms

Linear Search, Binary Search, complexity analysis, comparison counting

Multi-dimensional Arrays (4.2)

2D arrays, matrix operations, transpose, multiplication

07

Pro Tips

Sorting Tips
  • Bubble Sort: Compare adjacent elements, swap if needed
  • Selection Sort: Find minimum, swap with current position
  • Insertion Sort: Insert each element into sorted portion
  • All three have O(n^2) time complexity
Searching Tips
  • Linear Search: O(n) - works on unsorted arrays
  • Binary Search: O(log n) - requires sorted array
  • Always check if element exists before returning index
  • Count comparisons to show algorithm efficiency
Matrix Tips
  • For multiplication: A[m x n] * B[n x p] = C[m x p]
  • Check dimensions before multiplying
  • Transpose: rows become columns, columns become rows
  • Use proper formatting for matrix display
Common Mistakes
  • Array index out of bounds (0 to n-1)
  • Forgetting to sort before binary search
  • Not handling empty arrays or single elements
  • String comparison with == instead of strcmp
08

Pre-Submission Checklist

Code Requirements
Repository Requirements