Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction to Programming Languages CECS121 Lab #7. Programming assignment (10

ID: 3765313 • Letter: I

Question

Introduction to Programming Languages CECS121 Lab #7.

Programming assignment (100 pts): Add additional functionality to your phonebook program from lab# 6. MUST BE IN C++.

Make it possible for users to:

1) Alphabetically sort the list of entries by name (first or last).

2) Find a phone number for a given name.

3) Randomly select a friend from the phonebook for you to call.

4) Delete everyone from the phonebook at the same time.

Here is my lab #6:

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

typedef struct PhoneBook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
  
phone * AddEntry (phone * );
phone * DeleteEntry (phone * );
void PrintEntry (phone * );
int counter = 0;

int main (void)
{
phone *phonebook;
phonebook = (phone*) malloc(sizeof(phone)*1); //Allocate memory for contacts
int iSelection = 0; //Variable for menu choice
  
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
return 1;
}
else {}
  
while (iSelection <= 4)
{
printf(" Phonebook Menu");
printf(" (1) Add Friend");
printf(" (2) Delete Friend");
printf(" (3) Display Phonebook Entries");
printf(" (4) Exit Phonebook");
printf(" What would you like to do? ");
scanf("%d", &iSelection);
  
// Add Friend
if (iSelection == 1)
{
phonebook = AddEntry(phonebook);
}

//Delete Friend
if (iSelection == 2)
{
phonebook = DeleteEntry (phonebook);
}

//Display Phonebook Entries
if (iSelection == 3)
{
PrintEntry(phonebook);
}

//Exit Phonebook
if (iSelection == 4)
{
printf("The Phonebook will now exit.");
system("cls");
free(phonebook);
return 0;
}
  
}
}

//Function Definition to Add Contacts to the Phonebook
phone * AddEntry (phone * phonebook)
{
  
phone* tmp = realloc(phonebook, (counter+1)*sizeof(phone)); //Reallocate memory as number of contacts expands
if(tmp == NULL){
    printf("Out of Memory ");
}
else{
        phonebook = tmp;
        counter++;
printf(" First Name: ");
scanf("%s", phonebook[counter-1].FirstName);
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf(" Friend successfully added to Phonebook ");
  
}
return(phonebook);
}
  
phone * DeleteEntry (phone * phonebook)
{
int x,y;
char deleteFirstName[20];
char deleteLastName[20];
char nullStr[20] = {""};
  
printf(" First name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0) //If deleteFirstName matches phonebook.FirstName
{
   if (strcmp(deleteLastName, phonebook[x].LastName) == 0) //If deleteLastName matches phonebook.LastName
{
               for (y = x+1; y < counter; y++)
               {
              
                   phonebook[y-1] = phonebook[y];
                   /*
               strcpy(phonebook[x].FirstName, nullStr);
               strcpy(phonebook[x].LastName, nullStr);
               strcpy(phonebook[x].PhoneNumber, nullStr);
               */
               }//end of second loop
               counter--; // Contact deleted, update the contact total counter
               phonebook = realloc(phonebook, (counter)*sizeof(phone));
               printf("Record deleted from the phonebook. ");
               return(phonebook);
}//end of if
}
} //end of first loop
   printf("Invalid Entry.");
   return(phonebook);
}//end of delete

// Function Definition to Print All Phonebook Contacts
void PrintEntry (phone * phonebook)
{
int x = 0;
printf(" Phonebook Entries: ");
for( x = 0; x < counter; x++)
{
printf(" (%d) ", x+1);
printf("Name: %s %s ", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s ", phonebook[x].PhoneNumber);
}
} //end

Explanation / Answer

//Added functionality for items 1 and 2

phone * AddEntry (phone * );
phone * DeleteEntry (phone * );
void PrintEntry (phone * );
phone * SortByFirstName(phone *phonebook);
char* getPhoneByName(phone * phonebook);
int counter = 0;

int main (void)
   {
   phone *phonebook;
   phonebook = (phone*) malloc(sizeof(phone)*1); //Allocate memory for contacts
   int iSelection = 0; //Variable for menu choice

   if (phonebook == NULL)
      {
         printf("Out of Memory. The program will now exit");
         return 1;
      }
   else {}

   while (iSelection <= 5)
   {
      printf(" Phonebook Menu");
      printf(" (1) Add Friend");
      printf(" (2) Delete Friend");
      printf(" (3) Display Phonebook Entries");
      printf(" (4) Exit Phonebook");
      printf(" (5) Sort By Firstname");
      printf(" (6) Get phone By Name");


      printf(" What would you like to do? ");
      scanf("%d", &iSelection);

// Add Friend
      if (iSelection == 1)
         {
            phonebook = AddEntry(phonebook);
         }

//Delete Friend
      if (iSelection == 2)
         {
            phonebook = DeleteEntry (phonebook);
         }

//Display Phonebook Entries
      if (iSelection == 3)
         {
            PrintEntry(phonebook);
         }


//Exit Phonebook
      if (iSelection == 4)
         {
            printf("The Phonebook will now exit.");
            system("cls");
            free(phonebook);
            return 0;
         }

          //sort
          if(iSelection == 5) {
              phonebook = SortByFirstName(phonebook);
          }

          if(iSelection == 6) {
              char *phnumber = getPhoneByName(phonebook);
              printf("Number: %s ", phnumber);
          }
      }
   }

phone * SortByFirstName(phone *phonebook) {
   int i = 0, x = 0;
   int j = 0;

   if(counter > 0) {
       phone sortedBook[counter];
       //copy
       for( x = 0; x < counter; x++)
           {
               sortedBook[x] = phonebook[x];
           }

       phone temp;
       for (i = 0; i < counter; i++) {
                  for (j = 0; j < counter - 1; j++) {
                     if (strcmp(sortedBook[j].FirstName, sortedBook[j + 1].FirstName) > 0) {
                        temp = sortedBook[j];
                        sortedBook[j] = sortedBook[j + 1];
                        sortedBook[j+1] = temp;

                     }
                  }
               }
       printf("Sorted phonebook ");
       PrintEntry(sortedBook);
       return sortedBook;
   }
   return phonebook;
}

char* getPhoneByName(phone * phonebook) {
   char firstName[20];
   char lastName[20];

      printf(" First Name: ");
      scanf("%s", firstName);
      printf("Last Name: ");
      scanf("%s", lastName);
      int x = 0;
      char *ph = " ";
      for (x = 0; x < counter; x++)
          {
              if (strcmp(firstName, phonebook[x].FirstName) == 0) //If deleteFirstName matches phonebook.FirstName
              {
                  if (strcmp(lastName, phonebook[x].LastName) == 0) {

                       return phonebook[x].PhoneNumber;
                  }
              }
          }
      return ph;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote