online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_PEOPLE 100 #define FILENAME "network.txt" typedef struct Person { char name[30]; struct Person** friends; int num_friends; } Person; typedef struct Network { Person* people[MAX_PEOPLE]; int num_people; } Network; // Function prototypes Network* create_network(); void free_network(Network* net); Person* create_person(const char* name); void add_person(Network* net, const char* name); Person* find_person(Network* net, const char* name); void add_friendship(Network* net, const char* name1, const char* name2); void print_network(Network* net); void print_person_friends(Person* p); void find_friends_of_friends(Network* net, const char* name, int depth); void find_fof_recursive(Network* net, Person* p, Person* origin, int current_depth, int max_depth, int* visited); void save_network(Network* net); void load_network(Network* net); void display_menu(); // ===== STUDENT EXERCISE FUNCTIONS (TO BE IMPLEMENTED) ===== void remove_person(Network* net, const char* name); void remove_friendship(Network* net, const char* name1, const char* name2); int count_mutual_friends(Network* net, const char* name1, const char* name2); Person* find_most_popular(Network* net); int shortest_path(Network* net, const char* name1, const char* name2); void suggest_friends(Network* net, const char* name); int count_total_friendships(Network* net); void print_sorted_by_friends(Network* net); // ===== TEST FUNCTIONS ===== void run_all_tests(Network* net); void test_count_total_friendships(); void test_find_most_popular(); void test_count_mutual_friends(); void test_remove_friendship(); void test_remove_person(); void test_print_sorted_by_friends(); void test_suggest_friends(); void test_shortest_path(); // Create a new network Network* create_network() { Network* net = (Network*)malloc(sizeof(Network)); net->num_people = 0; for (int i = 0; i < MAX_PEOPLE; i++) { net->people[i] = NULL; } return net; } // Free all memory used by the network void free_network(Network* net) { for (int i = 0; i < net->num_people; i++) { if (net->people[i] != NULL) { free(net->people[i]->friends); free(net->people[i]); } } free(net); } // Create a new person Person* create_person(const char* name) { Person* p = (Person*)malloc(sizeof(Person)); strncpy(p->name, name, 29); p->name[29] = '\0'; p->friends = NULL; p->num_friends = 0; return p; } // Add a person to the network void add_person(Network* net, const char* name) { if (net->num_people >= MAX_PEOPLE) { printf("Network is full!\n"); return; } if (find_person(net, name) != NULL) { printf("Person '%s' already exists!\n", name); return; } net->people[net->num_people] = create_person(name); net->num_people++; printf("Added '%s' to the network.\n", name); } // Find a person by name Person* find_person(Network* net, const char* name) { for (int i = 0; i < net->num_people; i++) { if (strcmp(net->people[i]->name, name) == 0) { return net->people[i]; } } return NULL; } // Add a bidirectional friendship void add_friendship(Network* net, const char* name1, const char* name2) { Person* p1 = find_person(net, name1); Person* p2 = find_person(net, name2); if (p1 == NULL || p2 == NULL) { printf("One or both people not found!\n"); return; } if (p1 == p2) { printf("Cannot befriend yourself!\n"); return; } // Check if already friends for (int i = 0; i < p1->num_friends; i++) { if (p1->friends[i] == p2) { printf("'%s' and '%s' are already friends!\n", name1, name2); return; } } // Add p2 to p1's friends p1->friends = (Person**)realloc(p1->friends, (p1->num_friends + 1) * sizeof(Person*)); p1->friends[p1->num_friends] = p2; p1->num_friends++; // Add p1 to p2's friends p2->friends = (Person**)realloc(p2->friends, (p2->num_friends + 1) * sizeof(Person*)); p2->friends[p2->num_friends] = p1; p2->num_friends++; printf("Friendship created between '%s' and '%s'!\n", name1, name2); } // Print all people and their friends void print_network(Network* net) { if (net->num_people == 0) { printf("\nNetwork is empty!\n"); return; } printf("\n=== Social Network ===\n"); for (int i = 0; i < net->num_people; i++) { print_person_friends(net->people[i]); } printf("======================\n"); } // Print a person's friends void print_person_friends(Person* p) { printf("%s's friends: ", p->name); if (p->num_friends == 0) { printf("(none)"); } else { for (int i = 0; i < p->num_friends; i++) { printf("%s", p->friends[i]->name); if (i < p->num_friends - 1) printf(", "); } } printf("\n"); } // Find friends of friends using recursion void find_friends_of_friends(Network* net, const char* name, int depth) { Person* p = find_person(net, name); if (p == NULL) { printf("Person '%s' not found!\n", name); return; } int* visited = (int*)calloc(net->num_people, sizeof(int)); // Mark the origin person as visited for (int i = 0; i < net->num_people; i++) { if (net->people[i] == p) { visited[i] = 1; break; } } printf("\n=== Friends of Friends for '%s' (depth %d) ===\n", name, depth); find_fof_recursive(net, p, p, 0, depth, visited); printf("===============================================\n"); free(visited); } // Recursive helper for finding friends of friends void find_fof_recursive(Network* net, Person* p, Person* origin, int current_depth, int max_depth, int* visited) { if (current_depth >= max_depth) return; for (int i = 0; i < p->num_friends; i++) { Person* friend = p->friends[i]; // Find index of this friend int friend_idx = -1; for (int j = 0; j < net->num_people; j++) { if (net->people[j] == friend) { friend_idx = j; break; } } if (!visited[friend_idx]) { visited[friend_idx] = 1; // Print indentation based on depth for (int d = 0; d <= current_depth; d++) { printf(" "); } printf("-> %s (depth %d)\n", friend->name, current_depth + 1); // Recurse find_fof_recursive(net, friend, origin, current_depth + 1, max_depth, visited); } } } // Save network to file void save_network(Network* net) { FILE* fp = fopen(FILENAME, "w"); if (fp == NULL) { printf("Error opening file for writing!\n"); return; } // Save number of people fprintf(fp, "%d\n", net->num_people); // Save all people names for (int i = 0; i < net->num_people; i++) { fprintf(fp, "%s\n", net->people[i]->name); } // Save friendships for (int i = 0; i < net->num_people; i++) { Person* p = net->people[i]; for (int j = 0; j < p->num_friends; j++) { // Only save if friend index is greater (avoid duplicates) int friend_idx = -1; for (int k = 0; k < net->num_people; k++) { if (net->people[k] == p->friends[j]) { friend_idx = k; break; } } if (friend_idx > i) { fprintf(fp, "%s,%s\n", p->name, p->friends[j]->name); } } } fclose(fp); printf("Network saved to '%s'.\n", FILENAME); } // Load network from file void load_network(Network* net) { FILE* fp = fopen(FILENAME, "r"); if (fp == NULL) { printf("No saved network found.\n"); return; } // Clear existing network for (int i = 0; i < net->num_people; i++) { if (net->people[i] != NULL) { free(net->people[i]->friends); free(net->people[i]); } } net->num_people = 0; int num_people; fscanf(fp, "%d\n", &num_people); // Load all people for (int i = 0; i < num_people; i++) { char name[30]; fgets(name, 30, fp); name[strcspn(name, "\n")] = '\0'; add_person(net, name); } // Load friendships char line[100]; while (fgets(line, 100, fp) != NULL) { line[strcspn(line, "\n")] = '\0'; char* name1 = strtok(line, ","); char* name2 = strtok(NULL, ","); if (name1 != NULL && name2 != NULL) { add_friendship(net, name1, name2); } } fclose(fp); printf("Network loaded from '%s'.\n", FILENAME); } // Display menu void display_menu() { printf("\n=== Mini Social Network ===\n"); printf("1. Add person\n"); printf("2. Add friendship\n"); printf("3. Print network\n"); printf("4. Find friends of friends\n"); printf("5. Save network\n"); printf("6. Load network\n"); printf("7. Remove person\n"); printf("8. Remove friendship\n"); printf("9. Count mutual friends\n"); printf("10. Find most popular person\n"); printf("11. Find shortest path\n"); printf("12. Suggest friends\n"); printf("13. Count total friendships\n"); printf("14. Print sorted by friends\n"); printf("15. Run all tests\n"); printf("16. Exit\n"); printf("Choice: "); } // ============================================================ // STUDENT EXERCISE FUNCTIONS - IMPLEMENT THESE! // ============================================================ /* * Exercise 1: Remove a person from the network * * Requirements: * - Find the person in the network * - Remove them from all their friends' friend lists * - Free the person's friends array * - Free the person struct * - Shift remaining people in the network array * - Update num_people * * Hints: * - Use find_person() to locate the person * - Loop through their friends and remove the person from each friend's list * - Use memmove() or manual shifting to remove from array * - Don't forget to free memory! */ void remove_person(Network* net, const char* name) { // TODO: Implement this function printf("TODO: Remove person '%s' from network\n", name); } /* * Exercise 2: Remove a friendship between two people * * Requirements: * - Find both people * - Remove each from the other's friend list * - Reallocate memory to shrink the arrays (optional but good practice) * - Handle case where they aren't friends * * Hints: * - Find the index of each person in the other's friend array * - Shift remaining elements to fill the gap * - Decrease num_friends for both * - Consider using realloc() to shrink the arrays */ void remove_friendship(Network* net, const char* name1, const char* name2) { // TODO: Implement this function printf("TODO: Remove friendship between '%s' and '%s'\n", name1, name2); } /* * Exercise 3: Count mutual friends between two people * * Requirements: * - Find both people * - Compare their friend lists * - Count how many friends they have in common * - Return the count * * Hints: * - Use nested loops to compare friend lists * - Compare pointers (p1->friends[i] == p2->friends[j]) * - Return 0 if either person doesn't exist */ int count_mutual_friends(Network* net, const char* name1, const char* name2) { // TODO: Implement this function printf("TODO: Count mutual friends between '%s' and '%s'\n", name1, name2); return 0; } /* * Exercise 4: Find the most popular person (most friends) * * Requirements: * - Loop through all people * - Track who has the most friends * - Return pointer to that person * - Return NULL if network is empty * * Hints: * - Keep track of max_friends and most_popular variables * - Compare each person's num_friends * - Handle ties (just return first one found) */ Person* find_most_popular(Network* net) { // TODO: Implement this function printf("TODO: Find most popular person\n"); return NULL; } /* * Exercise 5: Find shortest path between two people (BFS) * * Requirements: * - Use breadth-first search (BFS) to find shortest connection * - Return the length of the shortest path * - Return -1 if no path exists * * Hints: * - Use a queue for BFS (can use an array as simple queue) * - Keep track of visited people * - Track distance for each person * - This is more challenging - requires understanding of graph algorithms! * * Algorithm outline: * 1. Create queue and visited array * 2. Start with person 1, mark as visited, distance = 0 * 3. While queue not empty: * - Dequeue a person * - If it's person 2, return distance * - For each friend, if not visited: * - Mark visited, set distance = current + 1, enqueue * 4. If queue empties, return -1 (no path) */ int shortest_path(Network* net, const char* name1, const char* name2) { // TODO: Implement this function using BFS printf("TODO: Find shortest path between '%s' and '%s'\n", name1, name2); return -1; } /* * Exercise 6: Suggest friends (friends of friends who aren't already friends) * * Requirements: * - Find all friends of friends * - Exclude people who are already direct friends * - Exclude the person themselves * - Print suggestions with count of mutual friends * * Hints: * - Loop through person's friends * - For each friend, loop through their friends * - Check if that person is already a friend * - Use count_mutual_friends() to show why they're suggested */ void suggest_friends(Network* net, const char* name) { // TODO: Implement this function printf("TODO: Suggest friends for '%s'\n", name); } /* * Exercise 7: Count total friendships in network * * Requirements: * - Count unique friendships (don't count each friendship twice) * - Return total count * * Hints: * - Since friendships are bidirectional, each friendship is counted twice * - Sum all num_friends and divide by 2 * - OR count only when friend index > current person index */ int count_total_friendships(Network* net) { // TODO: Implement this function printf("TODO: Count total friendships\n"); return 0; } /* * Exercise 8: Print people sorted by number of friends * * Requirements: * - Create a sorted list of people by num_friends (descending) * - Print in order with friend counts * - Don't modify the original network * * Hints: * - Create temporary array of Person pointers * - Copy pointers from network * - Use bubble sort, selection sort, or qsort() * - For qsort(), you'll need a comparison function */ void print_sorted_by_friends(Network* net) { // TODO: Implement this function printf("TODO: Print people sorted by number of friends\n"); } // ============================================================ // TEST FUNCTIONS FOR STUDENT EXERCISES // ============================================================ /* * Helper function to create a test network * Creates a network with the following structure: * * Alice --- Bob --- Charlie * | | | * Diana Eve Frank * | * Grace */ Network* create_test_network() { Network* net = create_network(); // Add people add_person(net, "Alice"); add_person(net, "Bob"); add_person(net, "Charlie"); add_person(net, "Diana"); add_person(net, "Eve"); add_person(net, "Frank"); add_person(net, "Grace"); // Add friendships add_friendship(net, "Alice", "Bob"); add_friendship(net, "Alice", "Diana"); add_friendship(net, "Bob", "Charlie"); add_friendship(net, "Bob", "Eve"); add_friendship(net, "Charlie", "Frank"); add_friendship(net, "Diana", "Grace"); return net; } void test_count_total_friendships() { printf("\n========== TEST: count_total_friendships() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); int total = count_total_friendships(net); printf("\n--- Test Results ---\n"); printf("Expected: 6 friendships\n"); printf("Your result: %d friendships\n", total); if (total == 6) { printf("✓ TEST PASSED!\n"); } else { printf("✗ TEST FAILED - Check your implementation\n"); } // Test empty network Network* empty_net = create_network(); int empty_total = count_total_friendships(empty_net); printf("\nEmpty network test:\n"); printf("Expected: 0 friendships\n"); printf("Your result: %d friendships\n", empty_total); if (empty_total == 0) { printf("✓ TEST PASSED!\n"); } else { printf("✗ TEST FAILED\n"); } free_network(net); free_network(empty_net); printf("====================================================\n"); } void test_find_most_popular() { printf("\n========== TEST: find_most_popular() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); Person* popular = find_most_popular(net); printf("\n--- Test Results ---\n"); printf("Expected: Bob (3 friends)\n"); if (popular != NULL) { printf("Your result: %s (%d friends)\n", popular->name, popular->num_friends); if (strcmp(popular->name, "Bob") == 0 && popular->num_friends == 3) { printf("✓ TEST PASSED!\n"); } else { printf("✗ TEST FAILED - Check your implementation\n"); } } else { printf("Your result: NULL\n"); printf("✗ TEST FAILED - Should return Bob\n"); } // Test empty network Network* empty_net = create_network(); Person* empty_result = find_most_popular(empty_net); printf("\nEmpty network test:\n"); printf("Expected: NULL\n"); printf("Your result: %s\n", empty_result ? empty_result->name : "NULL"); if (empty_result == NULL) { printf("✓ TEST PASSED!\n"); } else { printf("✗ TEST FAILED\n"); } free_network(net); free_network(empty_net); printf("===============================================\n"); } void test_count_mutual_friends() { printf("\n========== TEST: count_mutual_friends() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); printf("\n--- Test Cases ---\n"); // Test 1: Alice and Charlie (should have 1 mutual friend: Bob) int mutual1 = count_mutual_friends(net, "Alice", "Charlie"); printf("Test 1: Alice and Charlie\n"); printf("Expected: 1 mutual friend (Bob)\n"); printf("Your result: %d mutual friend(s)\n", mutual1); printf(mutual1 == 1 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 2: Alice and Diana (should have 0 mutual friends) int mutual2 = count_mutual_friends(net, "Alice", "Diana"); printf("Test 2: Alice and Diana\n"); printf("Expected: 0 mutual friends (they are direct friends)\n"); printf("Your result: %d mutual friend(s)\n", mutual2); printf(mutual2 == 0 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 3: Eve and Charlie (should have 1 mutual friend: Bob) int mutual3 = count_mutual_friends(net, "Eve", "Charlie"); printf("Test 3: Eve and Charlie\n"); printf("Expected: 1 mutual friend (Bob)\n"); printf("Your result: %d mutual friend(s)\n", mutual3); printf(mutual3 == 1 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 4: Grace and Frank (should have 0 mutual friends) int mutual4 = count_mutual_friends(net, "Grace", "Frank"); printf("Test 4: Grace and Frank\n"); printf("Expected: 0 mutual friends (no common connections)\n"); printf("Your result: %d mutual friend(s)\n", mutual4); printf(mutual4 == 0 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); free_network(net); printf("==================================================\n"); } void test_remove_friendship() { printf("\n========== TEST: remove_friendship() ==========\n"); Network* net = create_test_network(); printf("Initial Network:\n"); print_network(net); printf("\n--- Removing friendship: Alice and Bob ---\n"); remove_friendship(net, "Alice", "Bob"); printf("\nNetwork after removal:\n"); print_network(net); Person* alice = find_person(net, "Alice"); Person* bob = find_person(net, "Bob"); printf("\n--- Test Results ---\n"); printf("Alice's friends (expected 1: Diana): "); print_person_friends(alice); printf("Bob's friends (expected 2: Charlie, Eve): "); print_person_friends(bob); int alice_correct = (alice->num_friends == 1); int bob_correct = (bob->num_friends == 2); // Check that Alice and Bob are not in each other's friend lists int found_bob_in_alice = 0; for (int i = 0; i < alice->num_friends; i++) { if (alice->friends[i] == bob) found_bob_in_alice = 1; } int found_alice_in_bob = 0; for (int i = 0; i < bob->num_friends; i++) { if (bob->friends[i] == alice) found_alice_in_bob = 1; } if (alice_correct && bob_correct && !found_bob_in_alice && !found_alice_in_bob) { printf("✓ TEST PASSED!\n"); } else { printf("✗ TEST FAILED - Check your implementation\n"); if (found_bob_in_alice) printf(" Error: Bob still in Alice's friend list\n"); if (found_alice_in_bob) printf(" Error: Alice still in Bob's friend list\n"); } free_network(net); printf("===============================================\n"); } void test_remove_person() { printf("\n========== TEST: remove_person() ==========\n"); Network* net = create_test_network(); printf("Initial Network:\n"); print_network(net); printf("\n--- Removing person: Bob ---\n"); printf("Bob has 3 friends: Alice, Charlie, Eve\n"); remove_person(net, "Bob"); printf("\nNetwork after removal:\n"); print_network(net); printf("\n--- Test Results ---\n"); printf("Expected network size: 6 people\n"); printf("Your result: %d people\n", net->num_people); Person* bob = find_person(net, "Bob"); printf("\nSearching for Bob (should be NULL): %s\n", bob ? "FOUND (ERROR!)" : "NULL"); Person* alice = find_person(net, "Alice"); Person* charlie = find_person(net, "Charlie"); Person* eve = find_person(net, "Eve"); printf("\nAlice's friends (expected 1: Diana): "); if (alice) print_person_friends(alice); printf("Charlie's friends (expected 1: Frank): "); if (charlie) print_person_friends(charlie); printf("Eve's friends (expected 0): "); if (eve) print_person_friends(eve); int size_correct = (net->num_people == 6); int bob_removed = (bob == NULL); int alice_correct = (alice && alice->num_friends == 1); int charlie_correct = (charlie && charlie->num_friends == 1); int eve_correct = (eve && eve->num_friends == 0); if (size_correct && bob_removed && alice_correct && charlie_correct && eve_correct) { printf("\n✓ TEST PASSED!\n"); } else { printf("\n✗ TEST FAILED - Check your implementation\n"); if (!size_correct) printf(" Error: Network size incorrect\n"); if (!bob_removed) printf(" Error: Bob not removed from network\n"); if (!alice_correct) printf(" Error: Alice's friends not updated\n"); if (!charlie_correct) printf(" Error: Charlie's friends not updated\n"); if (!eve_correct) printf(" Error: Eve's friends not updated\n"); } free_network(net); printf("===========================================\n"); } void test_print_sorted_by_friends() { printf("\n========== TEST: print_sorted_by_friends() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); printf("\n--- Expected Order (by friend count) ---\n"); printf("1. Bob (3 friends)\n"); printf("2. Alice (2 friends)\n"); printf("3. Charlie (2 friends)\n"); printf("4. Diana (2 friends)\n"); printf("5. Eve (1 friend)\n"); printf("6. Frank (1 friend)\n"); printf("7. Grace (1 friend)\n"); printf("\n--- Your Output ---\n"); print_sorted_by_friends(net); printf("\nNote: Manually verify the output is sorted correctly!\n"); printf("People with same friend count can be in any order.\n"); free_network(net); printf("=====================================================\n"); } void test_suggest_friends() { printf("\n========== TEST: suggest_friends() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); printf("\n--- Test 1: Suggestions for Alice ---\n"); printf("Expected suggestions:\n"); printf(" - Charlie (1 mutual friend: Bob)\n"); printf(" - Eve (1 mutual friend: Bob)\n"); printf(" - Grace (1 mutual friend: Diana)\n"); printf("\nYour output:\n"); suggest_friends(net, "Alice"); printf("\n--- Test 2: Suggestions for Grace ---\n"); printf("Expected suggestions:\n"); printf(" - Alice (1 mutual friend: Diana)\n"); printf("\nYour output:\n"); suggest_friends(net, "Grace"); printf("\n--- Test 3: Suggestions for Bob ---\n"); printf("Expected suggestions:\n"); printf(" - Diana (1 mutual friend: Alice)\n"); printf(" - Frank (1 mutual friend: Charlie)\n"); printf("\nYour output:\n"); suggest_friends(net, "Bob"); printf("\nNote: Manually verify the suggestions are correct!\n"); free_network(net); printf("=============================================\n"); } void test_shortest_path() { printf("\n========== TEST: shortest_path() ==========\n"); Network* net = create_test_network(); printf("Test Network:\n"); print_network(net); printf("\n--- Test Cases ---\n"); // Test 1: Adjacent nodes int path1 = shortest_path(net, "Alice", "Bob"); printf("Test 1: Alice to Bob\n"); printf("Expected: 1 (direct friends)\n"); printf("Your result: %d\n", path1); printf(path1 == 1 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 2: 2 steps int path2 = shortest_path(net, "Alice", "Charlie"); printf("Test 2: Alice to Charlie\n"); printf("Expected: 2 (Alice -> Bob -> Charlie)\n"); printf("Your result: %d\n", path2); printf(path2 == 2 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 3: 3 steps int path3 = shortest_path(net, "Alice", "Frank"); printf("Test 3: Alice to Frank\n"); printf("Expected: 3 (Alice -> Bob -> Charlie -> Frank)\n"); printf("Your result: %d\n", path3); printf(path3 == 3 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 4: Same person int path4 = shortest_path(net, "Alice", "Alice"); printf("Test 4: Alice to Alice\n"); printf("Expected: 0 (same person)\n"); printf("Your result: %d\n", path4); printf(path4 == 0 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 5: Longest path in this network int path5 = shortest_path(net, "Grace", "Frank"); printf("Test 5: Grace to Frank\n"); printf("Expected: 5 (Grace -> Diana -> Alice -> Bob -> Charlie -> Frank)\n"); printf("Your result: %d\n", path5); printf(path5 == 5 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); // Test 6: Create disconnected person add_person(net, "Isolated"); int path6 = shortest_path(net, "Alice", "Isolated"); printf("Test 6: Alice to Isolated (no connection)\n"); printf("Expected: -1 (no path)\n"); printf("Your result: %d\n", path6); printf(path6 == -1 ? "✓ PASSED\n\n" : "✗ FAILED\n\n"); free_network(net); printf("===========================================\n"); } void run_all_tests() { printf("\n"); printf("╔════════════════════════════════════════════════════╗\n"); printf("║ RUNNING ALL TEST CASES FOR STUDENT EXERCISES ║\n"); printf("╚════════════════════════════════════════════════════╝\n"); test_count_total_friendships(); printf("\nPress Enter to continue..."); getchar(); test_find_most_popular(); printf("\nPress Enter to continue..."); getchar(); test_count_mutual_friends(); printf("\nPress Enter to continue..."); getchar(); test_remove_friendship(); printf("\nPress Enter to continue..."); getchar(); test_remove_person(); printf("\nPress Enter to continue..."); getchar(); test_print_sorted_by_friends(); printf("\nPress Enter to continue..."); getchar(); test_suggest_friends(); printf("\nPress Enter to continue..."); getchar(); test_shortest_path(); printf("\n"); printf("╔════════════════════════════════════════════════════╗\n"); printf("║ ALL TESTS COMPLETED! ║\n"); printf("╚════════════════════════════════════════════════════╝\n"); } int main() { Network* net = create_network(); int choice; char name1[30], name2[30]; char response; int depth; printf("Welcome to Mini Social Network!\n"); while (1) { display_menu(); scanf("%d", &choice); getchar(); // consume newline switch (choice) { case 1: printf("Enter name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; add_person(net, name1); break; case 2: printf("Enter first person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; printf("Enter second person's name: "); fgets(name2, 30, stdin); name2[strcspn(name2, "\n")] = '\0'; printf("Make friends? (%s, %s) [y/n]: ", name1, name2); scanf("%c", &response); getchar(); if (response == 'y' || response == 'Y') { add_friendship(net, name1, name2); } else { printf("Friendship cancelled.\n"); } break; case 3: print_network(net); break; case 4: printf("Enter person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; printf("Enter search depth (1-3): "); scanf("%d", &depth); getchar(); find_friends_of_friends(net, name1, depth); break; case 5: save_network(net); break; case 6: load_network(net); break; case 7: printf("Enter person's name to remove: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; remove_person(net, name1); break; case 8: printf("Enter first person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; printf("Enter second person's name: "); fgets(name2, 30, stdin); name2[strcspn(name2, "\n")] = '\0'; remove_friendship(net, name1, name2); break; case 9: printf("Enter first person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; printf("Enter second person's name: "); fgets(name2, 30, stdin); name2[strcspn(name2, "\n")] = '\0'; { int mutual = count_mutual_friends(net, name1, name2); printf("Mutual friends: %d\n", mutual); } break; case 10: { Person* popular = find_most_popular(net); if (popular != NULL) { printf("Most popular: %s with %d friends\n", popular->name, popular->num_friends); } } break; case 11: printf("Enter start person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; printf("Enter end person's name: "); fgets(name2, 30, stdin); name2[strcspn(name2, "\n")] = '\0'; { int path_length = shortest_path(net, name1, name2); if (path_length >= 0) { printf("Shortest path: %d connection(s)\n", path_length); } else { printf("No path exists!\n"); } } break; case 12: printf("Enter person's name: "); fgets(name1, 30, stdin); name1[strcspn(name1, "\n")] = '\0'; suggest_friends(net, name1); break; case 13: { int total = count_total_friendships(net); printf("Total friendships: %d\n", total); } break; case 14: print_sorted_by_friends(net); break; case 15: run_all_tests(); break; case 16: printf("Saving and exiting...\n"); save_network(net); free_network(net); return 0; default: printf("Invalid choice!\n"); } } return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue