Answer to Question #211758 in C for Visva

Question #211758

Write a C program to store the information of vehicles. Use bit fields to store the status of information.

* The types of vehicles information are Two Wheeler=3, Four Wheeler=4, Petrol=0, Diesel=1, Old=5 and New=6

 * The bit fields member variables are fuel, type and model


1
Expert's answer
2021-06-29T02:02:05-0400


#include <stdio.h>




// Write a C program to store the information of vehicles. Use bit fields to store the status of information.
// The types of vehicles information are Two Wheeler=3, Four Wheeler=4, Petrol=0, Diesel=1, Old=5 and New=6
// The bit fields member variables are fuel, type and model


#define PETROL			0
#define DIESEL  		1
#define TWO_WHEELER		3
#define FOUR_WHEELER	4
#define OLD				5
#define NEW				6




main(void)
{
	unsigned char VehicleInfo=0x00, Flag=1,c;
	unsigned int temp;
	while(Flag)
	{
		temp=0;
		VehicleInfo=0x00;
		c=1;
		while(c)
		{
			printf("\nEnter Vehicle Category: (Press 3 for 2-Wheeler or 4 for 4-Wheeler): "); 
			scanf("%d",&temp);
			if(temp==TWO_WHEELER)   VehicleInfo = 0x08;
			if(temp==FOUR_WHEELER)  VehicleInfo = 0x10;
			if(temp==3 || temp==4) c=0;
		}
		c=1;
		while(c)
		{
			printf("\nEnter Petrol/Diesel (Press 0 for Petrol or 1 for Diesel): ") ;
			scanf("%d",&temp);
			if(temp==PETROL)   VehicleInfo = VehicleInfo|0x01;
			if(temp==DIESEL)   VehicleInfo = VehicleInfo|0x02;
			if(temp==0 || temp==1) c=0;
		}


		temp=0;
		c=1;
		while(c)
		{
			printf("\nEnter New/Old Vehicle (Press 5 for Old or 6 for New) :");
			scanf("%d",&temp);
			if(temp==OLD)   VehicleInfo = VehicleInfo|0x20;
			if(temp==NEW)   VehicleInfo = VehicleInfo|0x40;
			if(temp==5 || temp==6) c =0;
		}
		
		if(VehicleInfo)
		{
			printf("\n\nVehicle Info:");
			if(VehicleInfo&0x08) printf("\nVehicle's Class: Two Wheeler");
			if(VehicleInfo&0x10) printf("\nVehicle's Class: Four Wheeler");


			if(VehicleInfo&0x01) printf("\n\nFuel Class: Petrol");
			if(VehicleInfo&0x02) printf("\n\nFuel Class: Diesel");


			if(VehicleInfo&0x20) printf("\n\nModel:  Old");
			if(VehicleInfo&0x40) printf("\n\nModel:  New");
		}
		printf("\n\nPress 1 to Continue or 0 to QUIT: "); scanf("%d",&Flag);
					
	}
	
	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