[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

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

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.


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.

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

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.


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.


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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.