1

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(&quot;%1d%1d%1d&quot;, y, y + 1, y + 2);

a = y[0];
b = y[1];
c = y[2];

if (a &gt; 3 || b &gt; 3 || c &gt; 3) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 0 &amp;&amp; c == 0) {
    printf(&quot;Maximum 5!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 1 &amp;&amp; c == 0) {
    printf(&quot;Maximum 5!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 0 &amp;&amp; c == 1) {
    printf(&quot;Maximum 5!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 1 &amp;&amp; c == 1) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 1 &amp;&amp; c == 0) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 0 &amp;&amp; c == 1) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 2 &amp;&amp; c == 0) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 2 &amp;&amp; c == 3) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 2 &amp;&amp; b == 1 &amp;&amp; c == 0) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 2 &amp;&amp; b == 1 &amp;&amp; c == 3) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 1 &amp;&amp; c == 2) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 0 &amp;&amp; c == 1) {
    printf(&quot;Invalid entry!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 3 &amp;&amp; c == 0) {
    printf(&quot;Maximum 15!\n&quot;);
}
else if (a == 1 &amp;&amp; b == 0 &amp;&amp; c == 3) {
    printf(&quot;Maximum 15!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 1 &amp;&amp; c == 3) {
    printf(&quot;Maximum 15!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 1 &amp;&amp; c == 0) {
    printf(&quot;Maximum 15!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 0 &amp;&amp; c == 1) {
    printf(&quot;Maximum 15!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 2 &amp;&amp; c == 3) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 3 &amp;&amp; c == 2) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 2 &amp;&amp; b == 0 &amp;&amp; c == 3) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 2 &amp;&amp; b == 3 &amp;&amp; c == 0) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 0 &amp;&amp; c == 2) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 3 &amp;&amp; b == 2 &amp;&amp; c == 0) {
    printf(&quot;Not possible to gather!\n&quot;);
}
else if (a == 0 &amp;&amp; b == 0 &amp;&amp; c == 0) {
    printf(&quot;Invalid entry!\n&quot;);
}


return 0;

}

So , is there more simpler way to write a code without using each possible combination of else if loop ? Thanks in advance :)

Richard
  • 3,961
  • 13
  • 34
Aster Nash
  • 13
  • 3
  • Tip: you might be able to read the input into an integer value, and use comparisons that are a bit more readable: if( input_int == 310) printf("Maximum 15!\n"); – MPIchael Nov 02 '20 at 14:28
  • A good start to simplification would be the elimination of the array y[]. Suggest reading the values directly into a and b and c – user3629249 Nov 02 '20 at 14:48

1 Answers1

2

With the way you have written it, where you enumerate all possible states (though it looks like you are still missing a few), you can do the valid cases first, then just have invalid grouped into a final else statement. This will cut a bunch of tests for specific invalid states.

To change your approach, instead of storing a,b,c you could instead store the count of no info, sunny, rainy, and windy. The order of elements in the code doesn't matter, only the amount, so this should lead to fewer and simpler else-if tests.

Tyberius
  • 1,023
  • 1
  • 10
  • 26
  • Alright , I see your point and flaws in my code ,I will try to shorten it. Thanks for answering my question :) – Aster Nash Nov 01 '20 at 09:32