Answer to Question #204545 in C for Hassam

Question #204545

Write a C program that declares an array of structures of type ‘car’. and asks the user to fill in the elements. Once all the structures have been initialized, the program should present a menu to the user so that he can print a certain structure, or modify its elements.


1
Expert's answer
2021-06-10T05:58:03-0400
#include<stdio.h>
#include<stdlib.h>
struct car
{
    char model[30],company[30],type[30];
    float cost;
}c[30];
int main()
{
    int n,i,num,ind;
    printf("Enter the number of entries : ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("Enter details for car number %d\n",i+1);
        printf("Enter company : ");
        scanf("%s",c[i].company);
        printf("Enter model : ");
        scanf("%s",c[i].model);
        printf("Enter type : ");
        scanf("%s",c[i].type);
        printf("Enter cost : ");
        scanf("%f",&c[i].cost);
    }
    printf("1. Display the details of a car\n");
    printf("2. Modify the details of car\n");
    scanf("%d",&num);
    switch(num)
    {
        case 1:
                printf("Enter the index : ");
                scanf("%d",&ind);
                printf("Details for car number %d\n",ind);
                printf("Company : %s\n",c[ind-1].company);
                printf("Model : %s\n",c[ind-1].model);
                printf("Type : %s\n",c[ind-1].type);
                printf("Enter cost : %f\n\n",c[ind-1].cost);
                break;
        case 2:
                printf("Enter the index : ");
                scanf("%d",&ind);
                printf("Enter company : ");
                scanf("%s",c[ind].company);
                printf("Enter model : ");
                scanf("%s",c[ind].model);
                printf("Enter type : ");
                scanf("%s",c[ind].type);
                printf("Enter cost : ");
                scanf("%f",&c[ind].cost);
                break;
        default: printf("Invalid Input !!!");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS