Create a structure for holding data of 50’s industrial machine such as machine id, machine name, date of purchase.

Question

a) Write a function that continuously ask user to enter the machine details until user press ‘n’ or user already entered 50 machines’ data.
b) Display the record in tabular form

Machine ID Machine Name Date of Purchase
1 Double side planer 02/06/2000
2 Drill press 17/09/2010
3 Drum sander 22/03/2015
4 Jointer 10/11/2020

c) Write a function that allow user to search for the particular machine and calculate the age of machine in months.

For example, User search for machine ID = 4, the age of machine till September 2021 is 9 months.

Summary

Basically, a scenario is given in which the data of 50’s industrial machine will be collected from the user. Then, all the machine details are displayed in the form of a table. Then, a machine is searched giving the machine id, and the age of the machine is calculated in terms of months with respect to September 2021.

Explanation

The structure named machine is implemented having the data members as machineId, machineName and date. Then a global variable named ‘m’ is declared to store the data of 50’s industrial machine. Then, the function named getData() is implemented in which the details of every machine are taken from the user and the details are stored in the structure array variable. Then, in the main method, the getData method is called.

Then, the details of all the machines are displayed in the form of a table as output to the console. The function named calculateAge() is implemented to which an id is passed which is checked for the machine id in the machine structure array. If the id is present then the number of months is calculated and displayed as output to the console.

Code

#include <stdio.h>
/* to have the details about each machine */
struct machine{
    /* data members */
    int machineId;
    char machineName[30];
    char date[10];
};
/* to store 50 machine details */
struct machine m[50];
/* to get the data of machine details from user */
int getData(){
    int k=0;
    char c[1]="y";
    /* to get the machine details from user */
    while(c[0]=='y'){
        printf("Enter machine id: ");
        scanf("%d",&m[k].machineId);
        printf("Enter machine name: ");
        scanf("%s",m[k].machineName);
        printf("Enter the date of purchase: ");
        scanf("%s",m[k].date);
        /* to enter one more */
        printf("Enter another (y/n): ");
        scanf("%s",c);
        k++;
    }
    return k;
}
/* function to calculate the age of a particular machine */
void calculateAge(int id,int cc){
    for(int i=0;i<cc;i++){
        /* if the ith machine id matches with the id given */
        if(id==m[i].machineId){
            int month=((int)m[i].date[3]-(int)'0')*10 + ((int)m[i].date[4]-(int)'0');
            int year=((int)m[i].date[6]-(int)'0')*1000 + ((int)m[i].date[7]-(int)'0')*100;
            year = year+((int)m[i].date[8]-(int)'0')*10 + ((int)m[i].date[9]-(int)'0');
            int result=(2021-year-1)*12;
            result=result+(12-month)+8;
            printf("\nUser search for machine ID = %d, the age of the machine ",id);
            printf("till Septemeber 2021 is %d months.",result);
            break;
        }
    }
}
int main()
{
    int p=getData();
    printf("%d\n",p);
    /* to display the table of all the machine details */
    printf("\nMachine ID    Machine Name        Date of Purchase\n");
    for(int i=0;i<p;i++){
    printf("%d             %s               %s\n",m[i].machineId,m[i].machineName,m[i].date);
    }
    int id;
    printf("\nEnter an id to search the machine: ");
    scanf("%d",&id);
    calculateAge(id,p);
}

Output

data of 50's industrial machine

 

Also, read other blog of consider evaluating a polynomial.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *