Make a simple program to analyze COVID-19 symptoms in C#

[COVID-19 symptoms]

This example shows how to build an extremely simple program to analyze COVID-19 symptoms. It should not be taken as actual medical advice. It’s presented here as an example of a very simple artificial intelligence application.


Background

This type of artificial intelligence application is a table-driven expert system. It uses a set of simple lookup tables to emulate the skills of an expert, in this case a doctor. In a real expert system, you would do a ton of research, testing, and adjusting to get the values in the tables just right. For this example I just guessed. They will work for extremely obvious cases. For example, if you have a runny nose, sneezing, and no other symptoms, then you are more likely to have allergies than COVID-19.

In any case, don’t use this program for important diagnoses. If you have serious concerns, consult a doctor. (If you can find one who has time for you.)

I picked the numbers in the tables based on values shown in the following chart included in the Business Insider article How coronavirus symptoms compare with those of the flu, allergies, and the common cold.


[COVID-19 symptoms]

Table Values

The idea behind the program is simple: the program gives you points for each of the COVID-19 symptoms that you have. The program assigns the following points for each of the symptoms shown in the previous chart.

Frequency Points
Common 20
Sometimes 5
Mild 3
Rare 1
No 0

Again, let me emphasize that I just picked those numbers arbitrarily. I don’t have enough data to adjust them properly.

The Program

The program uses the following constants and table values.

// Point values.
private const int Common = 20;
private const int Sometimes = 5;
private const int Mild = 3;
private const int Rare = 1;
private const int No = 0;

// Values for each diagnosis.
private int[,] Values =
{
    { Common, Common, Common, Sometimes, Sometimes, Sometimes, Sometimes, Rare, Rare, No },
    { Rare, Mild, No, Rare, Common, Common, Sometimes, No, Common, Common },
    { Common, Common, No, Common, Common, Common, Common, Sometimes, No, No },
    { Sometimes, Sometimes, Common, Sometimes, No, No, Sometimes, No, Common, Common },
};
private const int DiarrheaSymptom = 7;

// Indices in the array.
private int Corona = 0;
private int CommonCold = 1;
private int Flu = 2;
private int Allergies = 3;

The first set of constants defines the point values for the symptoms. The Values array holds the values for the symptoms for the different diagnoses. For example, the first row holds the values for the COVID-19 symptoms.

The last set of constants defines the row numbers for the diagnoses (although the program only uses the Flu constant.)

When the program starts, it executes the following Form_Load event handler.

// An array of CheckBoxes so we can loop over them.
private CheckBox[] CheckBoxes;

private void Form1_Load(object sender, EventArgs e)
{
    // Load the CheckBoxes array.
    CheckBoxes = new CheckBox[]
    {
        chkFever,
        chkDryCough,
        chkShortnessOfBreath,
        chkHeadaches,
        chkAchesAndPains,
        chkSoreThroat,
        chkFatigue,
        chkDiarrhea,
        chkRunnyNose,
        chkSneezing,
    };

    Analyze();
}

The CheckBoxes array holds references to the form’s CheckBox controls. The Form_Load event handler initializes the array and then calls the Analyze method.

All of the program’s CheckBox controls execute the following code when you check or uncheck them.

private void chkSymptom_CheckedChanged(object sender, EventArgs e)
{
    Analyze();
}

This code simply calls the Analyze method shown in the following code.

private void Analyze()
{
    int num_diagnoses = Values.GetUpperBound(0) + 1;
    int num_symptoms = Values.GetUpperBound(1) + 1;
    int[] totals = new int[num_diagnoses];
    for (int diagnosis = 0; diagnosis < num_diagnoses; diagnosis++)
    {
        for (int symptom = 0; symptom < num_symptoms; symptom++)
        {
            if (CheckBoxes[symptom].Checked)
                totals[diagnosis] += Values[diagnosis, symptom];
        }
    }

    // If an adult, remove diarrhea from flu.
    if (chkAdult.Checked && chkDiarrhea.Checked)
    {
        totals[Flu] -= Values[Flu, DiarrheaSymptom];
    }

    // Display results.
    Label[] labels = { lblCoronaVirus, lblCold, lblFlu, lblAllergies };
    for (int diagnosis = 0; diagnosis < num_diagnoses; diagnosis++)
    {
        labels[diagnosis].Width = totals[diagnosis];
        labels[diagnosis].Text = totals[diagnosis].ToString();
    }
}

This method first checks the Values array’s bounds to get the number of diagnoses and the number of symptoms. It then creates a totals array to hold the point totals for each of the diagnoses.

Next the code loops through the diagnoses. For each diagnosis, the code loops through the CheckBoxes array to see which are boxes checked. If a box is checked, then the code adds the point value for that diagnosis and symptom to the current diagnosis.

After it finishes calculating the basic point totals, the code makes one adjustment. The diarrhea symptom only applies to children. If the Adult and Diarrhea boxes are both checked, then code subtracts the Flu/Diarrhea value from the flu diagnosis total.

The code finishes by displaying the results. It creates an array holding references to the green result labels. It then loops through the diagnoses. For each diagnosis, the code sets the corresponding label’s width and text to indicate the totals.

Conclusion

Note that the numbers are point totals not percentages. If Allergies scores a 65 and COVID-19 scores 26, that doesn’t mean there is a 65% chance that you have allergies. It just means that you are more likely to have allergies than COVID-19.

There are other symptoms that you might want to add. For example, if you know that you have seasonal allergies and it is allergy season, then that should probably increase the Allergies/Runny Nose points.

Note that some people have very mild COVID-19 symptoms and don’t show symptoms initially. Also note that this program does not cover other possible causes for symptoms such as food poisoning (diarrhea), a strenuous workout (aches and pains), or lack of sleep caused by worrying about COVID-19 (fatigue). Duh.

But if you just have a runny nose and wonder if you have COVID-19, you probably shouldn’t rush to the emergency room.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms and tagged , , , , , , . Bookmark the permalink.

2 Responses to Make a simple program to analyze COVID-19 symptoms in C#

  1. Sinan Basalan says:

    In this era, why do you use a ancient version of .Net?

    • RodStephens says:

      I normally use C# 2008 for two main reasons. First, it’s much faster, smaller, and easier to use than the more recent versions. The newer versions are much larger and slower, and they only include a few features that are really useful.

      Second, newer versions can upgrade an older program but older versions cannot downgrade newer programs. If you have Visual Studio 2019, then you can open a C# 2008 program but not vice versa.

      I do use the most recent version occasionally. In particular I use it when I’m writing a book (people who buy a book want to use the latest version) or on the rare occasion that I want to demonstrate a new feature.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.