+1 (315) 557-6473 

Store Data in An Array of Structs Assignment Solutions.


Instructions

Objective
Write a C program to Store data in array of structs

Requirements and Specifications

This assignment relates to the project we have been building as part of our discussions on the Software Development Life Cycle. Our client has requested that we also provide management components for both the customers and employees. This will eventually be integrated with our current sales component so that we can track individual employee and customer records based on the sale log.
  1. Implement the Employees component following the requirements below.
Requirements
  • Represent the Employees in your code using a 3D character array similar to the examples we did in class.
  • The maximum number of employees that can be saved is 128.
  • Create a function list_employee which lists all employee data resembling the example run below.
  • Create a function add_employee which prompts the user to input new employee information. Your code should check to make sure the employee does not already exist before adding it to the 3D array. Also check that the employee can be added based on the maximum number of employees in the system.
  • Create a function get_employee which searches the employee data given an employee ID. If the employee exists, return the index within the data that the employee was found.
  • Create a function find_employee which accepts a string containing the name of an employee to search for. It should return the index of the employee in the database.
  • Each of the above functions should accept the 3D character array of employee information as one of their parameters. Global variables should not be used!
Data Format
  •  ID
  • Name
  • Title
Screenshots of output
Store data in array of structs C language

Source Code

#include

#include

void list_customer(char customer[][3][25], int n)

{

    int i;

    printf("%-9s %-25s %s\n", "ID", "NAME", "PHONE");

    printf("-------------------------------------------------\n");

    for(i = 0; i < n; i++)

    {

        printf("%-9s %-25s %s\n", customer[i][0], customer[i][1], customer[i][2]);

    }

}

int add_customer(char customer[][3][25], int n)

{

    int i;

    char ID[25];

    char NAME[25];

    char PHONE[25];

    printf("Enter ID: ");

    fgets(ID, 25, stdin);

    ID[strlen(ID) - 1] = 0;

    printf("Enter Name: ");

    fgets(NAME, 25, stdin);

    NAME[strlen(NAME) - 1] = 0;

    printf("Enter Phone Number: ");

    fgets(PHONE, 25, stdin);

    PHONE[strlen(PHONE) - 1] = 0;

    if (n < 128) /* if no space, don't add */

    {

        for (i = 0; i < n; i++)

        {

            if (!strcmp(ID, customer[i][0])) /* if found, don't add */

                return n;

        }

        strcpy(customer[n][0], ID);

        strcpy(customer[n][1], NAME);

        strcpy(customer[n][2], PHONE);

        n++;

    }

    return n;

}

int get_customer(char customer[][3][25], int n, char *ID)

{

    int i;

    for (i = 0; i < n; i++)

    {

        if (!strcmp(ID, customer[i][0])) /* if found, return index */

            return i;

    }

    return -1; /* not found */

}

int find_customer(char customer[][3][25], int n, char *NAME)

{

    int i;

    for (i = 0; i < n; i++)

    {

        if (!strcmp(NAME, customer[i][1])) /* if found, return index */

            return i;

    }

    return -1; /* not found */

}

#include

#include

void list_employee(char employee[][3][25], int n)

{

    int i;

    printf("%-9s %-25s %s\n", "ID", "NAME", "TITLE");

    printf("-------------------------------------------------\n");

    for(i = 0; i < n; i++)

    {

        printf("%-9s %-25s %s\n", employee[i][0], employee[i][1], employee[i][2]);

    }

}

int add_employee(char employee[][3][25], int n)

{

    int i;

    char ID[25];

    char NAME[25];

    char TITLE[25];

    printf("Enter ID: ");

    fgets(ID, 25, stdin);

    ID[strlen(ID) - 1] = 0;

    printf("Enter Name: ");

    fgets(NAME, 25, stdin);

    NAME[strlen(NAME) - 1] = 0;

    printf("Enter Title: ");

    fgets(TITLE, 25, stdin);

    TITLE[strlen(TITLE) - 1] = 0;

    if (n < 128) /* if no space, don't add */

    {

        for (i = 0; i < n; i++)

        {

            if (!strcmp(ID, employee[i][0])) /* if found, don't add */

                return n;

        }

        strcpy(employee[n][0], ID);

        strcpy(employee[n][1], NAME);

        strcpy(employee[n][2], TITLE);

        n++;

    }

    return n;

}

int get_employee(char employee[][3][25], int n, char *ID)

{

    int i;

    for (i = 0; i < n; i++)

    {

        if (!strcmp(ID, employee[i][0])) /* if found, return index */

            return i;

    }

    return -1; /* not found */

}

int find_employee(char employee[][3][25], int n, char *NAME)

{

    int i;

    for (i = 0; i < n; i++)

    {

        if (!strcmp(NAME, employee[i][1])) /* if found, return index */

            return i;

    }

    return -1; /* not found */