Skip to main content
edited body
Source Link
Roland Illig
  • 21.4k
  • 2
  • 34
  • 83

The longest line in your code is 100001745 characters long. Assuming that a single character is 2 mm wide, I'd need a screen of 203.5 meters to show the whole line. I don't have such a screen.

The longest line in your code is 10000 characters long. Assuming that a single character is 2 mm wide, I'd need a screen of 20 meters to show the whole line. I don't have such a screen.

The longest line in your code is 1745 characters long. Assuming that a single character is 2 mm wide, I'd need a screen of 3.5 meters to show the whole line. I don't have such a screen.

added 1 character in body
Source Link
Roland Illig
  • 21.4k
  • 2
  • 34
  • 83

The longest line in your code is 10000 characters long. Assuming that a single character is 32 mm wide, I'd need a screen of 320 meters to show the whole line. I don't have such a screen.

The longest line in your code is 10000 characters long. Assuming that a single character is 3 mm wide, I'd need a screen of 3 meters to show the whole line. I don't have such a screen.

The longest line in your code is 10000 characters long. Assuming that a single character is 2 mm wide, I'd need a screen of 20 meters to show the whole line. I don't have such a screen.

Source Link
Roland Illig
  • 21.4k
  • 2
  • 34
  • 83

The first thing I noticed about your code is that it is very compressed. There are almost no empty lines, and the lines are quite long.

The longest line in your code is 10000 characters long. Assuming that a single character is 3 mm wide, I'd need a screen of 3 meters to show the whole line. I don't have such a screen.

To understand that single line, I had to split it up into this block of code:

//create an array with all the questions and possible answers to display to the user
var questions = new[] {
    "Question 1: True or False – Binary numbers consist of a sequence of ones and zeros? "
    + "Press T or F and enter to submit",

    "Question 2: Multiple choice – Add the following binary numbers: 1011+0011. "
    + "Answers: 1) 1110, 2) 1022, 3) 1010, 4) 1032. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 3: Multiple choice – Add the following binary numbers: 11011011+01100110. "
    + "Answers: 1) 11000001, 2) 11111111, 3) 12111121, 4) 101000001. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 4: True or False – Binary numbers are base-10? "
    + "Press T or F and enter to submit",
    
    "Question 5: Multiple choice – Subtract the following binary numbers: 1110-0101. "
    + "Answers: 1) 1010, 2) 1001, 3) 1000, 4) 0100. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 6: Multiple choice – Subtract the following binary numbers: 10010011-01101100. "
    + "Answers: 1) 01101111, 2) 00010111, 3) 00100111, 4) 11011101. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 7: True or False – Binary numbers are base-2? "
    + "Press T or F and enter to submit",
    
    "Question 8: Multiple choice – the binary number 1011 equates to what base-10 number? "
    + "Answers: 1) 11, 2) 22, 3) 14, 4) 7. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 9: Multiple choice – what is the binary equivalent of the base-10 number 127? "
    + "Answers: 1) 01101111, 2) 11111111, 3) 10111011, 4) 11111110. "
    + "Select the number (1,2,3 or 4) of the correct answer and enter to submit",
    
    "Question 10: True or False: an 8-bit binary number can have a maximum value of 128 in base-10? "
    + "Press T or F and enter to submit"
};

I have no idea how you could ever edit all this text in a single line. Having it split into multiple lines has these benefits:

  • readers of your code can see all the questions on one screen
  • the strings are split at natural boundaries (using the + operator)
  • redundant information becomes clearly visible
    • the question number is written explicitly into each question. This means that the questions cannot be shuffled. Any good quiz program must shuffle the questions as much as possible to avoid learning patterns like "the first question is f".
    • each mc question has the same structure: <Question> Answer: 1) <A1>, 2) <A2>, 3) <A3>, 4) <A4>. This structure should not be encoded in a string but in the code of the program. That's something that will hopefully be covered later in class.
    • each tf question ends with the "Press T or F" sentence
    • each mc question ends with the "Select" sentence

These redundancies do not occur in good programs. The redundant text should better be handled when printing the question, in code like this:

if (questionType[questionCount] == "tf") {
    Console.WriteLine("Press T or F and Enter to submit");
} else if (questionType[questionCount] == "mc") {
    Console.WriteLine("Select the number (1, 2, 3 or 4) of the correct answer and press Enter to submit");
}

This brings me directly to the next item, which is the variable questionCount. A count by convention means the total number of something. The count of questions in this program is 10, and this count doesn't change. Therefore the variable name is misleading. It should rather be named questionIndex. By convention, an index is typically between 0 and the corresponding count, which is exactly the case here.

In the very first part of my answer I said that your code looks very compressed. This is because it doesn't contain empty lines in the usual places. Empty lines should mark all places where in traditional writing you would end a paragraph. One of these places is before a function is defined, such as string InvalidMultipleChoiceEntry(). Before each function definition there should be an empty line. The comment describing the function should be in an extra line before the function definition. Like this:

// This method will check multiple choice answers to ensure they are a number 1-4
string InvalidMultipleChoiceEntry() 
{
    var answer = Console.ReadLine();
    if (answer != "1" && answer != "2" && answer != "3" && answer != "4") //did the user input a numner between 1 and 4?
    {
        Console.WriteLine("Invalid answer type (not 1-4), try again."); //if not, try again until they do!
        return InvalidMultipleChoiceEntry();
    }

    return answer;
}

Calling InvalidMultipleChoiceEntry from inside InvalidMultipleChoiceEntry is a dangerous technique in most programming languages. It can lead to unexpected memory consumption. To test this, replace the call Console.ReadLine(); with a simple "invalid" and run the program. It will run for some seconds and then crash with this error message:

Process is terminating due to StackOverflowException.

To avoid this error, rewrite the function like this:

string InvalidMultipleChoiceEntry()
{
    while (true) {
        var answer = Console.ReadLine();
        if (answer == "1" || answer == "2" || answer == "3" || answer == "4") {
            return answer;
        }

        Console.WriteLine("Invalid answer type (not 1-4), try again.");
    }
}

When I ran the program for the first time, the 5 seconds you gave me to read the intro were way too short. Or maybe the intro was too long, who knows. If you remove the Thread.Sleep(5000); and replace the Console.Clear with Console.WriteLine, everything is fine.

There's probably more to say, but on the other hand the above is already enough work for one or two hours.