Answer to Question #203967 in C for Hassam

Question #203967

Write a C Program that does the following:

1. Declares a C-Structure called ‘car’.

2. It should contain the following elements.

a) A char array of size 20 to hold the make of the car e.g. Suzuki.

b) A char array of size 20 to hold the model of the car e.g. Alto

c) An integer capacity to store the seating capacity.

d) A floating-point number to hold the weight of the empty vehicle.

3. Then declare 3 variables of this type of structure.

4. Let the user populate the elements of these variables.

5. Print these structures


1
Expert's answer
2021-06-06T12:59:34-0400
#include<stdio.h>
#include<stdlib.h>


//1. Declares a C-Structure called ‘car’.
struct car{
	//2. It should contain the following elements.
	//a) A char array of size 20 to hold the make of the car e.g. Suzuki.	
	char make[20];
	//b) A char array of size 20 to hold the model of the car e.g. Alto
	char model[20];
	//c) An integer capacity to store the seating capacity.
	int capacity;
	//d) A floating-point number to hold the weight of the empty vehicle.
	float weight;
};


int main( void){
	int i;
	//3. Then declare 3 variables of this type of structure.
	struct car car1;
	struct car car2;
	struct car car3;
	//4. Let the user populate the elements of these variables.
	printf("Enter the make of the car 1: ");
	gets(car1.make);
	printf("Enter the model of the car 1: ");
	gets(car1.model);
	printf("Enter the capacity of the car 1: ");
	scanf("%d",&car1.capacity);
	printf("Enter the weight of the car 1: ");
	scanf("%f",&car1.weight);


	fflush(stdin);
	printf("\nEnter the make of the car 2: ");
	gets(car2.make);
	printf("Enter the model of the car 2: ");
	gets(car2.model);
	printf("Enter the capacity of the car 2: ");
	scanf("%d",&car2.capacity);
	printf("Enter the weight of the car 2: ");
	scanf("%f",&car2.weight);


	fflush(stdin);
	printf("\nEnter the make of the car 3: ");
	gets(car3.make);
	printf("Enter the model of the car 3: ");
	gets(car3.model);
	printf("Enter the capacity of the car 3: ");
	scanf("%d",&car3.capacity);
	printf("Enter the weight of the car 3: ");
	scanf("%f",&car3.weight);


	
	//5. Print these structures
	printf("\n\n%-15s%-15s%-15s%-15s\n","Car make","Car model","Car make","Car capacity","Car weight");
	printf("%-15s%-15s%-15d%-15.2f\n",car1.make,car1.model,car1.capacity,car1.weight);
	printf("%-15s%-15s%-15d%-15.2f\n",car2.make,car2.model,car2.capacity,car2.weight);
	printf("%-15s%-15s%-15d%-15.2f\n",car3.make,car3.model,car3.capacity,car3.weight);


	getchar();
	getchar();
	return 0;
}

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