#include <stdio.h> #include <stdlib.h> #include <string.h> struct final { char n
ID: 3627863 • Letter: #
Question
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct final
{
char name[100];
int phone;
};
typedef struct final Final;
#define MAX 15
int fillArray(Final array[]);
//void sortArray(Final array[], int total);
int menu();
void add(Final array[], int * total);
void find(Final array[], int total);
void print(Final array[], int total);
int main()
{
Final array[MAX];
int choice;
int total;
total = fillArray(array);
//sortArray(array, total);
do
{
choice = menu();
switch(choice)
{
case 1: add(array, &total);
break;
//case 2: find(array, total);
//break;
case 3: print(array, total);
break;
// Extra Credit
// case 4: delete(array, &total);
// break;
default: printf("Thanks ");
}// end switch
}while(choice != 5);
return 0;
}// end main
//fills array
int fillArray(Final array[])
{
int numberNames;
int phoneNumber;
char temp[MAX];
int i;
printf("How many names and numbers would you like to enter? ");
scanf("%i", &numberNames);
if(numberNames<15)
{
for(i=0;i < numberNames; i++)
{
printf("Please enter the name (no spaces,all lower case) ");
scanf("%s", &temp);
while(getchar()!=' ')
;
strcpy(array[i].name, temp);
printf("Please enter a 4 digit phone number ");
scanf("%i", &phoneNumber);
array[i].phone=phoneNumber;
}//end for loop
}//end if
return numberNames;
}//end fill array
//sorts Array
/*void sortArray(Final array[], int total)
{
int i,j;
char temp[MAX];
for(i=0; ifor(j=0; jif(array[i].name>array[j].name)
{
temp=array[i].name;
array[i].name=array[j].name;
array[j].name=temp;
}//End For
}//End Function*/
//menu
int menu()
{
int choice;
while(choice<1||choice>5)
{
printf("What would you like to do? ");
printf("1. Add a new person and phone number to the book. ");
printf("2. Find a name in the phone book. ");
printf("3. Print all of the names and numbers in the directory. ");
printf("4. Delete a name from the book. ");
printf("5. Quit the program. ");
printf("your choice--> ");
scanf("%i", &choice);
}//end while
return choice;
}//end menu
//definition to add
void add(Final phBook[], int * total)
{
if(*total<15)
{
printf("What name would you like to add.( no spaces, all lower case): ");
scanf("%s",phBook[*total].name);
printf("Enter 4 digit phone number: ");
scanf("%d",&phBook[*total].phone);
(*total)++;
}
}//end add
///definition to find
void find(Final phBook[], int total)
{
char name[100];
int found=0;
int i;
printf("Enter a name( no spaces, all lower case): ");
scanf("%s",name);
for(i=0;i<found;i++)
if(strcmp(phBook[i].name,name)==0)
{
printf("Name: %s",phBook[i].name);
printf(" Phone Number: %d ",phBook[i].phone);
found=1;
}
if(found==0)
printf("Name doesn't exists in the array");
}//end find
//prints the array to the screen
void print(Final array[], int total)
{
int x;
for(x=0; x<total;x++)
{
printf("Name: %s ", array[x].name);
printf("Phone: %i ", array[x].phone);
}//end for loop
}//end print
//deletes something in the array
/*void delete(Final array[], int* total)
{
int x, y, catdog;
for(x=0; x< catdog; x++)
for(y=0; y<catdog;y++)*/
Explanation / Answer
//header section
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct final
{
char name[100];
int phone;
};
typedef struct final Final;
#define MAX 15
//function prototypes
int fillArray(Final []);
void sortArray(Final [], int total);
int menu();
void add(Final [], int * total);
void find(Final [], int total);
void print(Final [], int total);
void Delete(Final [], int *);
//main function
int main()
{
Final phBook[MAX];
int choice;
int total;
total = fillArray(phBook);
sortArray(phBook, total);
do
{
choice = menu();
switch(choice)
{
case 1: add(phBook, &total);
break;
case 2: find(phBook, total);
break;
case 3: print(phBook, total);
break;
// Extra Credit
case 4: Delete(phBook, &total);
break;
default: printf("Thanks ");
}// end switch
}while(choice != 5);
system("pause");
return 0;
}// end main
//definition to fill array
int fillArray(Final phBook[])
{
int total,i;
printf("Enter number of elements to be enter :");
do
{
scanf("%d",&total);
if(total>MAX)
printf("Invalid size, Enter Again... ");
}while(total>MAX);
for(i=0;i<total;i++)
{
printf("Enter a name( no spaces, all lower case guaranteed):");
scanf("%s",phBook[i].name);
printf("Enter 4 digit phone number(guarenteed): ");
scanf("%d",&phBook[i].phone);
}
return total;
}
//definition to sort array
void sortArray(Final phBook[], int total)
{
Final temp;//Temperory Slopes
int swap,count;//Bool variable
//Do while loop
do
{
//Setting swap to false
swap=0;
for(count=0;count<total-1;count++)
{
//If present value is greater than next indexed value
if(strcmp(phBook[count].name,phBook[count+1].name)>0)
{
strcpy(temp.name,phBook[count].name);
temp.phone=phBook[count].phone;
strcpy(phBook[count].name,phBook[count+1].name);
phBook[count].phone=phBook[count+1].phone;
strcpy(phBook[count+1].name,temp.name);
phBook[count+1].phone=temp.phone;
//Change state of swap
swap=1;
}//End If
else if(strcmp(phBook[count].name,phBook[count+1].name)==0)
{
if(phBook[count].phone>phBook[count+1].phone)
{
strcpy(temp.name,phBook[count].name);
temp.phone=phBook[count].phone;
strcpy(phBook[count].name,phBook[count+1].name);
phBook[count].phone=phBook[count+1].phone;
strcpy(phBook[count+1].name,temp.name);
phBook[count+1].phone=temp.phone;
//Change state of swap
swap=1;
}
}
}//End For
}while(swap==1);//End do-while
}//End Function
//definition to display menu
int menu()
{
int choice;
printf(" What would you like to do? ");
printf("1. Add a new person and phone number to the book. ");
printf("2. Find a name in the phone book. ");
printf("3. Print all of the names and numbers in the directory. ");
printf("4. Delete a name from the book. ");
printf("5. Quit the program. ");
do
{
printf("your choice--> ");;
scanf("%d",&choice);
if(choice<0||choice>5)
printf("Invalid choice, Try Again..");
}while(choice<0||choice>5);
return choice;
}
//definition to add
void add(Final phBook[], int * total)
{
if(*total<14)
{
printf("Enter a name( no spaces, all lower case guaranteed): ");
scanf("%s",phBook[*total].name);
printf("Enter 4 digit phone number(guarenteed): ");
scanf("%d",&phBook[*total].phone);
(*total)++;
}
}
//definition to find
void find(Final phBook[], int total)
{
char name[100];
int found=0,i;
printf("Enter a name( no spaces, all lower case guaranteed): ");
scanf("%s",name);
for(i=0;i<total;i++)
{
if(strcmp(phBook[i].name,name)==0)
{
printf("Name: %s",phBook[i].name);
printf(" Phone Number: %d ",phBook[i].phone);
found=1;
}
}
if(found==0)
printf("Name doesn't exists in the array");
//definition to print all the data
void print(Final phBook[], int total)
{
int i;
printf("Data in array is: ");
for(i=0;i<total;i++)
{
printf(" Name :%s",phBook[i].name);
printf(" Phone Number :%d ",phBook[i].phone);
}
}
//definition to delete an entry from the array
void Delete(Final phBook[],int *total)
{
char name[25];
int i,j,found=0;
printf("Enter name to delete( no spaces, all lower case guaranteed): ");
scanf("%s",name);
for(i=0;i<*total;i++)
{
if(strcmp(name,phBook[i].name)==0)
{
printf("Name: %s",name);
printf(" Number: %d",phBook[i].phone);
printf(" .. is succesfully deleted from the list");
for(j=i;j<(*total)-1;j++)
phBook[j]=phBook[j+1];
(*total)--;
found=1;
break;
}
}
if(found==0)
printf("Name: %s is not found.",name);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.