CS50 2018 - Lecture 0 - Phone Book

3 min read 4 hours ago
Published on Oct 23, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial is designed to guide you through the concepts introduced in CS50's Lecture 0, focusing on the phone book application. It will cover the foundational elements of programming and data structures, which are crucial for building efficient applications. By the end of this tutorial, you will have a better understanding of how to manage data in a phone book context, as well as the importance of algorithms in programming.

Step 1: Understanding the Problem

Before diving into coding, it’s essential to understand the problem you are trying to solve. In this case, we want to create a phone book application that allows users to store and retrieve contact information.

  • Define the requirements:
    • Users should be able to add new contacts.
    • Users should be able to search for contacts by name.
    • Users should be able to delete contacts.

Step 2: Setting Up Your Environment

To start coding, you need to set up your programming environment.

  • Install a code editor such as Visual Studio Code or Sublime Text.
  • Ensure you have a C compiler installed (e.g., GCC).
  • Create a new directory for your project and navigate into it.

Step 3: Designing the Data Structure

Choosing the right data structure is crucial for efficiency. For a phone book, you can use an array or a linked list, but a hash table is often more efficient for searching.

  • Define a structure for the contact:

    typedef struct {
        char name[50];
        char number[15];
    } contact;
    
  • Choose a data structure:

    • For simplicity, start with an array of contact structs.
    • Consider the size of the array based on expected usage.

Step 4: Implementing Adding Contacts

Now, let’s implement the functionality to add a contact to the phone book.

  • Create a function to add a contact:

    void add_contact(contact phone_book[], int *count, char *name, char *number) {
        strcpy(phone_book[*count].name, name);
        strcpy(phone_book[*count].number, number);
        (*count)++;
    }
    
  • Call this function in your main routine when a user wants to add a contact.

Step 5: Implementing Searching for Contacts

Next, implement a search function that allows users to find contacts by name.

  • Create a function to search for a contact:

    contact *search_contact(contact phone_book[], int count, char *name) {
        for (int i = 0; i < count; i++) {
            if (strcmp(phone_book[i].name, name) == 0) {
                return &phone_book[i];
            }
        }
        return NULL; // Not found
    }
    
  • Ensure to handle cases where the contact does not exist.

Step 6: Implementing Deleting Contacts

To manage contacts effectively, you should also allow users to delete contacts.

  • Create a function to delete a contact:
    void delete_contact(contact phone_book[], int *count, char *name) {
        for (int i = 0; i < *count; i++) {
            if (strcmp(phone_book[i].name, name) == 0) {
                for (int j = i; j < *count - 1; j++) {
                    phone_book[j] = phone_book[j + 1];
                }
                (*count)--;
                return;
            }
        }
    }
    

Conclusion

In this tutorial, you learned how to build a simple phone book application using C. You covered the essential steps, including problem understanding, environment setup, data structure design, and implementing core functionalities like adding, searching, and deleting contacts.

As a next step, consider exploring more sophisticated data structures such as binary search trees or hash tables for more efficient data management. Happy coding!