Exercise [A]
(a) The information about colors is to be stored in bits of a char variable called color. The bit number 0 to 6, each represent 7 colors of a rainbow, i.e. bit 0 represents violet, 1 represents
indigo, and so on. Write a program that asks the user to enter a number and based on this number it reports which colors in the rainbow does the number represents.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
char color;
int num;
clrscr();
printf("Please enter the number(0-6): ");
scanf("%d",&num);
color=1<<num;
printf("\n\n");
if(num==0 && color==1)
printf("Violet");
else if(num==1 && color==2)
printf("Indigo");
else if(num==2 && color==4)
printf("Blue");
else if(num==3 && color==8)
printf("Green");
else if(num==4 && color==16)
printf("Yellow");
else if(num==5 && color==32)
printf("Orange");
else if(num==6 && color==64)
printf("Red");
else
printf("Wrong color number!");
getch();
}
----------------------------------------------------------------------------------------------------------------
(b) A company planning to launch a new newspaper in market conducts a survey. The various parameters considered in the survey were, the economic status (upper, middle, and lower class) the languages readers prefer (English, Hindi, Regional language) and category of paper (daily, supplement, tabloid). Write a program, which reads data of 10 respondents through keyboard, and stores the information in an array of integers. The bit-wise information to be stored in an integer is given below:
Bit Number Information
0 Upper class
1 Middle class
2 Lower class
3 English
4 Hindi
5 Regional Language
6 Daily
7 Supplement
8 Tabloid
At the end give the statistical data for number of persons who read English daily, number of upper class people who read tabloid and number of regional language readers.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int arr[10][3],i,j;
unsigned int infr;
int eng=0,utab=0,rgl=0;
for(i=0;i<10;i++) {
clrscr();
gotoxy(20,2);
printf("Enter data of respondent %d:\n\n",i+1);
for(j=0;j<3;j++) {
if(j==0){
printf("Economic Status:\n\n");
printf("0: Upper class\t1: Middle class\t2: Lower class\n\n");
scanf("%d",&arr[i][j]);
}
if(j==1){
printf("\n\nLanguage Preferred:\n\n");
printf("3: English\t4: Hindi\t5:Regional Language\n\n");
scanf("%d",&arr[i][j]);
}
if(j==2){
printf("\n\nType of Paper:\n\n");
printf("6: Daily\t7: Supplement\t8: Tabloid\n\n");
scanf("%d",&arr[i][j]);
}
}
}
/***********************************************/
/* converting the whole array using left shift */
/***********************************************/
for(i=0;i<10;i++) {
for(j=0;j<3;j++) {
arr[i][j]= 1 << arr[i][j]; /* conversion */
}
}
for(i=0;i<10;i++) {
if(arr[i][1]==8) /* english readers */
eng++;
if(arr[i][0]==1 && arr[i][2]==256) /* upper class,tabloid readers */
utab++;
if(arr[i][1]==32) /* regional language readers */
rgl++;
}
clrscr();
gotoxy(20,2);
printf("Reader's statistics:\n\n\n\n");
printf("\tEnglish Reader: %d\n",eng);
printf("\tUpper class Tabloid Readers: %d\n",utab);
printf("\tRegional Language readers: %d\n",rgl);
getch();
}
----------------------------------------------------------------------------------------------------------------
(c) In an inter-college competition, various sports and games are played between different colleges like cricket, basketball, football, hockey, lawn tennis, table tennis, carom and chess. The information regarding the games won by a particular college is stored in bit numbers 0, 1, 2, 3, 4, 5, 6, 7 and 8 respectively of an integer variable called game. The college that wins in 5 or more than 5 games is awarded the Champion of Champions trophy. If a number is entered through the keyboard, then write a program to find out whether the college won the Champion of the Champions trophy or not, along with the names of the games won by the college.
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
int game;
int i,won=0,name;
clrscr();
printf("Enter number: ");
scanf("%d",&name);
printf("\n\n");
for(i=1;i<=8;i++) {
game=i&name;
if(game==1) {
printf("Cricket\n");
won++;
}
if(game==2) {
printf("Basketball\n");
won++;
}
if(game==3) {
printf("Football\n");
won++;
}
if(game==4) {
printf("Hockey\n");
won++;
}
if(game==5) {
printf("Lawn-tennis\n");
won++;
}
if(game==6) {
printf("Table-tennis\n");
won++;
}
if(game==7) {
printf("Carrom\n");
won++;
}
if(game==8) {
printf("Chess\n");
won++;
}
}
if(won>=5) {
printf("\n\n\nCollege %d has won the champions trophy\n",name);
}
else {
printf("\n\n\nCollege %d didn't win the champions trophy\n",name);
}
getch();
}
-----------------------------------------------------------------------------------------------------------------
(d) An animal could be either a canine (dog, wolf, fox, etc.), a feline (cat, lynx, jaguar, etc.), a cetacean (whale, narwhal, etc.) or a marsupial (koala, wombat, etc.). The information whether a particular animal is canine, feline, cetacean, or marsupial is stored in bit number 0, 1, 2 and 3 respectively of a integer variable called type. Bit number 4 of the variable type stores the information about whether the animal is Carnivore or Herbivore.
For the following animal, complete the program to determine whether the animal is a herbivore or a carnivore. Also determine whether the animal is a canine, feline, cetacean or a marsupial.
struct animal
{
char name[30] ;
int type ;
}
struct animal a = { "OCELOT", 18 } ;
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
struct animal
{
char name[30];
int type;
};
struct animal a={"OCELOT",18};
int t1,t2,typ1,typ2;
clrscr();
t1= a.type >> 3;
typ1= t1 & 2; /* checking the bit after shifting */
t2= t1 << 3;
typ2= t2 & 16; /* checking another bit after shifting */
/**********************************************************/
/* checking if ocelot is canine/feline/cetacean/marsupial */
/**********************************************************/
if(typ1==1)
printf("\n\nOCELOT is Canine\n");
if(typ1==2)
printf("\n\nOCELOT is Feline\n");
if(typ1==4)
printf("\n\nOCELOT is Cetacean\n");
if(typ1==8)
printf("\n\nOCELOT is Marsupial\n");
/************************************************/
/* checking if ocelot is carnivore or herbivore */
/************************************************/
if(typ2!=0)
printf("\n\nOCELOT is Carnivore\n");
if(typ2==0)
printf("\n\nOCELOT is Herbivore\n");
getch();
}
-----------------------------------------------------------------------------------------------------------------
(e)The time field in the directory entry is 2 bytes long. Distribution of different bits which account for hours, minutes and seconds is given below. Write a function which would receive the two-byte time entry and return to the calling function, the hours, minutes and seconds.
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
H H H H H M M M M M M S S S S S
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
unsigned hr,mn,sc,input;
void timer();
clrscr();
printf("Enter the time entry: ");
scanf("%d",&input);
timer(input,&hr,&mn,&sc);
printf("\n\nTIME: %u : %u : %u\n",*(&hr),*(&mn),*(&sc));
getch();
}
void timer(unsigned time, unsigned *hr, unsigned *mn, unsigned *sc) {
*hr=time >> 11;
*mn=(time<<5) >> 10;
*sc=(time<<11) >> 11;
}
-----------------------------------------------------------------------------------------------------------------
(f) In order to save disk space information about student is stored in an integer variable. If bit number 0 is on then it indicates Ist year student, bit number 1 to 3 stores IInd year, IIIrd year and IVth year student respectively. The bit number 4 to 7 stores stream Mechanical, Chemical, Electronics and IT. Rest of the bits store room number. Based on the given data, write a program that asks for the room number and displays the information about the student, if its data exists in the array. The contents of array are,
int data[ ] = { 273, 548, 786, 1096 } ;
Solution:
#include<stdio.h>
#include<conio.h>
void main() {
unsigned int yr,br,data_yr,data_br;
int i,j,k,rn,a,b,flag=0;
int data[]={ 273,548,786,1096};
clrscr();
printf("Enter room number: ");
scanf("%d",&rn);
printf("\n\n\n Year: ");
/*********************/
/* checking for year */
/*********************/
for(i=1;i<=8;i=i*2) {
yr= rn & i;
if(yr==1) {
printf("First year");
break;
}
if(yr==2) {
printf("Second year");
break;
}
if(yr==4) {
printf("Third year");
break;
}
if(yr==8) {
printf("Fourth year");
break;
}
}
/***********************/
/* Checking for branch */
/***********************/
printf("\n\nBranch: ");
for(i=16;i<=128;i=i*2) {
br= rn & i;
if(br==16) {
printf("Mechanical");
break;
}
if(br==32) {
printf("Chemical");
break;
}
if(br==64) {
printf("Electronics");
break;
}
if(br==128) {
printf("I.T.");
break;
}
}
/***********************************************/
/* checking if data matches with that of array */
/***********************************************/
for(i=1,j=16;i<=8,j<=128;i=i*2,j=j*2) {
yr=rn&i;
br=rn&j;
for(k=0,a=1,b=16;k<4,a<=8,b<=128;k++,a=a*2,b=b*2) {
data_yr=data[k] & a;
data_br=data[k] & b;
if(yr==data_yr && br==data_br) {
flag+=1;
break;
}
}
}
if(flag==0)
printf("\n\n\ndata doesn't matche with the array.\n");
else
printf("\n\n\ndata matches with the array.\n");
getch();
}
______________________________________________________________________
#include
ReplyDeletemain()
{
char color;
int a;
color=color|1;
printf("Type");
scanf("%d",&a);
if(color<<a==1)
printf("violet\n");
else if(color<<a==2)
printf("blue\n");
else if(color<<a==4)
printf("green\n");
else if(color<<a==8)
printf("green\n");
else if(color<<a==16)
printf("yellow\n");
else if(color<<a==32)
printf("orange\n");
else if(color<<a==64)
printf("red\n");
}
In the (f) question," rest of the bits store room number" would mean the bits numbered from 8 to 15, but you are assuming that room number is made up of the bits numbered from 0 to 7. Which is right?? If I apply what I said then question becomes unlogical.so it is not correct either.help me!
ReplyDeleteHey Nice Blog!!! Thank you for sharing information.Paint By Numbers
ReplyDelete