Answer to Question #203972 in C for Lucy

Question #203972

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:38-0400
#include<stdio.h>
#include<stdlib.h>


struct car{
	char make[20];
	char model[20];
	int capacity;
	float weight;
};
void populateElements(struct car* c,int carNumber);
void printCar(struct car c);


int main( void){

	struct car c1;
	struct car c2;
	struct car c3;


	populateElements(&c1,1);
	fflush(stdin);
	printf("\n");
	populateElements(&c2,2);
	fflush(stdin);
	printf("\n");
	populateElements(&c3,3);




	printf("\n");
	printCar(c1);
	printCar(c2);
	printCar(c3);


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


void populateElements(struct car* c,int carNumber){
	printf("Enter the make of the car %d: ",carNumber);
	gets(c->make);
	printf("Enter the model of the car %d: ",carNumber);
	gets(c->model);
	printf("Enter the capacity of the car %d: ",carNumber);
	scanf("%d",&c->capacity);
	printf("Enter the weight of the car %d: ",carNumber);
	scanf("%f",&c->weight);
}
void printCar(struct car c){
	printf("Car make: %s\n",c.make);
	printf("Car model: %s\n",c.model);
	printf("Car capacity: %d\n",c.capacity);
	printf("Car weight: %.2f\n\n",c.weight);
}

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