So I have a question about simplifying my C code , This is the question : A pandemic of the Heineken virus is underway in Croatia and around the world. The National Civil Protection Headquarters plans to introduce protection measures that will take effect from Sunday at midnight, and they are:
If it is sunny and windless outside, it is possible to gather up to a maximum of 5 people, because the virus spreads very easily in such conditions. If it is sunny outside but the wind is blowing, it is possible to gather up to a maximum of 15 people, because the virus is harder to spread because the wind blows it away from a group of people (if someone happens to have Heineken). If it rains outside, it is possible to gather up to a maximum of 10 people, because the rain collects the virus in its droplets that fall to the floor, but it is possible to bounce off the floor and a person inhales them, and yet there is a possibility of infection. If it is raining outside and the wind is blowing, it is not possible to gather, because the weather is bad outside anyway.
Write a program that will help people know how to behave in this chaos. Your program receives a coded input parameter in the form of a three-digit number that indicates what the weather is like outside, and prints out how many people are allowed to gather together in that case.
A correctly coded number may contain values from the range [0, 3]:
0 - no information 1 - sunny 2 - it's raining 3 - the wind is blowing
The entry is invalid if the number: does not contain code digits from the defined range contains the same code digit more than once (except digit 0 which may appear more than once) contains non-code digits (all except 0, 1, 2 and 3) contains digits that are coded, but the procedure in these weather conditions is not defined (eg it is sunny and raining) .
I have written coda as follows :
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
int y[3];
int a, b, c;
scanf("%1d%1d%1d", y, y + 1, y + 2);
a = y[0];
b = y[1];
c = y[2];
if (a > 3 || b > 3 || c > 3) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 0 && c == 0) {
printf("Maximum 5!\n");
}
else if (a == 0 && b == 1 && c == 0) {
printf("Maximum 5!\n");
}
else if (a == 0 && b == 0 && c == 1) {
printf("Maximum 5!\n");
}
else if (a == 1 && b == 1 && c == 1) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 1 && c == 0) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 0 && c == 1) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 2 && c == 0) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 2 && c == 3) {
printf("Invalid entry!\n");
}
else if (a == 2 && b == 1 && c == 0) {
printf("Invalid entry!\n");
}
else if (a == 2 && b == 1 && c == 3) {
printf("Invalid entry!\n");
}
else if (a == 3 && b == 1 && c == 2) {
printf("Invalid entry!\n");
}
else if (a == 3 && b == 0 && c == 1) {
printf("Invalid entry!\n");
}
else if (a == 1 && b == 3 && c == 0) {
printf("Maximum 15!\n");
}
else if (a == 1 && b == 0 && c == 3) {
printf("Maximum 15!\n");
}
else if (a == 0 && b == 1 && c == 3) {
printf("Maximum 15!\n");
}
else if (a == 3 && b == 1 && c == 0) {
printf("Maximum 15!\n");
}
else if (a == 3 && b == 0 && c == 1) {
printf("Maximum 15!\n");
}
else if (a == 0 && b == 2 && c == 3) {
printf("Not possible to gather!\n");
}
else if (a == 0 && b == 3 && c == 2) {
printf("Not possible to gather!\n");
}
else if (a == 2 && b == 0 && c == 3) {
printf("Not possible to gather!\n");
}
else if (a == 2 && b == 3 && c == 0) {
printf("Not possible to gather!\n");
}
else if (a == 3 && b == 0 && c == 2) {
printf("Not possible to gather!\n");
}
else if (a == 3 && b == 2 && c == 0) {
printf("Not possible to gather!\n");
}
else if (a == 0 && b == 0 && c == 0) {
printf("Invalid entry!\n");
}
return 0;
}
So , is there more simpler way to write a code without using each possible combination of else if loop ? Thanks in advance :)
y[]. Suggest reading the values directly intoaandbandc– user3629249 Nov 02 '20 at 14:48