In C: Modify a struct employee so that the phone number is stored as a structure
ID: 3831008 • Letter: I
Question
In C: Modify a struct employee so that the phone number is stored as a structure, with separate fields for the area code, the local prefix, and the final four digits. Change the functions that print the employee table to print phone numbers along with name.
Modifying the code that appears too look like the following figures. Place all of these functions into a single file, rather than having them in separate files.
Figures 12.6
//Define a struct employee
Struct employee
{
long number;
char name[MAXNAME];
char phone[MAXDIGITS];
figure 12.7
//print the name field of each element in a structure
int main()
{voide printEmps(struct employwee table[], int);
Struct employee emptab[]=
{1001,”franklin, tammy”, “818-555-3333”, 29}
{1023, “perl, doris”, “213-123-4567”, 29}
};
PrintEmps(emptab, sizeof(emptab)/sizeof(emptab[0]));
Figure 12,8
//array printing function, using array subscripting
Void printEmps(struct employee table[], int n)
{
Int i;
For (i=0; i<n; i++)
Printf(“name: &s ”, table[i].name);
Figure 12.9
//better array priting function using pointer indexing.
Void printEmps(struct employee*ptr, int n)
Stuct employee*endptr=ptr+n;
For(;ptr<endptr;ptr++)
Printf(“name:%s ”ptr->name);
Use strings, rather than ints, to represent the three parts of a phone number. With ints, you would not be able to print leading zeroes. E.g., (952) 555-0034 would print as (952) 555-34.
Name the function in Figure 12.9 prEmps2, and call both it and printEmps from the main function so that you may demonstrate that both of these functions work from within the same program.
prEmps2(Printf(“name:%s ”ptr->name);
printEmps(Printf(“name: &s ”, table[i].name);
Format the output so that the phone numbers print with the hyphen and parentheses. E.g., (952) 487-8300.
Notice that you will be creating a nested structure, as the phone number is to be stored in a separate structure.
comments on the following:
- program description
- function descriptions
- all variable and constant declarations
- ambiguous or complex sections of code
the correct use of local variables, local prototypes, and parameter passing
coding stysle
struct phone_num
{
Char areacode[4];
Char prefix[4];
Char lastfour[5];
}
Struct employee
}
Struct phone-num phone;
}
Explanation / Answer
Hi, Please find my code.
#include <stdio.h>
#define MAXNAME 50
struct phone_num
{
char areacode[4];
char prefix[4];
char lastfour[5];
};
struct employee
{
long number;
char name[MAXNAME];
struct phone_num phone;
};
void printEmps(struct employee table[], int);
void printEmps2(struct employee *ptr, int n);
int main()
{
struct employee emptab[]= {{1001,"franklin, tammy", {"818","555","3333"}},{1023, "perl, doris", {"213","123","4567"}}};
printEmps(emptab, sizeof(emptab)/sizeof(emptab[0]));
printEmps2(emptab, sizeof(emptab)/sizeof(emptab[0]));
}
void printEmps(struct employee table[], int n)
{
int i;
for (i=0; i<n; i++)
printf("name:%s, phone: (%s)%s-%s ", table[i].name,table[i].phone.areacode,table[i].phone.prefix,table[i].phone.lastfour);
}
//better array priting function using pointer indexing.
void printEmps2(struct employee *ptr, int n){
struct employee* endptr=ptr+n;
for(;ptr<endptr;ptr++)
printf("name:%s, phone: (%s)%s-%s ", ptr->name,ptr->phone.areacode,ptr->phone.prefix,ptr->phone.lastfour);
}