-1

I have two arrays: array testAnswer holds "answers to a exam" and array inputAnswers holds "students answers to the exam".

When i run my code, it displays all the common elements of the two arrays(correct answers), and the uncommon elements (incorrect answers). However, instead of actually displaying the correct/incorrect answers, i want to be able to display the total number of correct/incorrect answers.

My code so far:

private void button1_Click(object sender, EventArgs e)
{
    //Array holding answers to test
    string[] testAnswer = new string[20] { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
    string a = String.Join(", ", testAnswer);

    //Reads text file line by line. Stores in array, each line of the file is an element in the array
    string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");

    string b = String.Join(", ", inputAnswer);

    //Increments through array elements in both arrays and checks for matching elements. Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] == inputAnswer[i])
            listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
    }

    //Increments through array elements in both arrays and checks for uncommon elements. Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] != inputAnswer[i])
            listBox2.Items.Add(inputAnswer[i]);
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    Have you tried `listBox1.Items.Count`? – Peter Gluck Aug 27 '12 at 07:24
  • Peter Gluck has given you the answer already...Just think where you'd like to show your TOTAL NUMBERs and show `listBox1.Items.Count` and `listBox2.Items.Count` – horgh Aug 27 '12 at 07:29
  • Besides you are showing only the answer letters in your listboxes, while wouldn't it be better to show the question as well; say, `string.Format("{0} ({1})","Question #i",inputAnswer[i])` – horgh Aug 27 '12 at 07:32
  • Awesome, that did it! Thanks everyone for your time – user1625857 Aug 27 '12 at 07:48

3 Answers3

4

Here's how to get your results using LINQ:

var results = 
    testAnswer
        .Zip(inputAnswer, (t, i) => new { t, i })
        .Aggregate(new { Correct = 0, Incorrect = 0 },
            (a, ti) => new
            {
                Correct = a.Correct + (ti.t == ti.i ? 1 : 0), 
                Incorrect = a.Incorrect + (ti.t != ti.i ? 1 : 0)
            });

It'll produce an anonymous variable with this kind of result:

anonymous variable result

An alternative approach is:

var query = 
    testAnswer
        .Zip(inputAnswer, (t, i) => t == i)
        .ToLookup(x => x);

var results = new
{
    Correct = query[true].Count(),
    Incorrect = query[false].Count()
};
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

The following code will provide 2 integers at the end which will hold the answer:

private void button1_Click(object sender, EventArgs e)
{
    string[] testAnswer = new string[20] { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
    string a = String.Join(", ", testAnswer);



    //Reads text file line by line. Stores in array, each line of the file is an element in the array
    string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");

    string b = String.Join(", ", inputAnswer);


    //Increments through array elements in both arrays and checks for matching elements. 
    //Displays in listBox.
    for (int i = 0; i < testAnswer.Length; i++)
    {
        if (testAnswer[i] == inputAnswer[i])
            listBox1.Items.Add(inputAnswer[i]); // or testAnswer[i], as appropriate
        else 
            listBox2.Items.Add(inputAnswer[i]);
        }

    int correctAns = listbox1.Items.Count;
    int wringAns = listbox2.Items.Count;
}
SynerCoder
  • 12,493
  • 4
  • 47
  • 78
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
-1

Common answers count would be Enumerable.Intersect result item count, uncommon - Enumerable.Except result item count.

Update: as long as it was mentioned in comments that it would produce wrong answers, proof that it would not:

  var testAnswers = new[] { 1, 2, 3 };
  var inputAnswers = new[] { 3, 2, 1 };
  var commonAnswers = testAnswers
            .Select((x, index) => Tuple.Create(x, index))
            .Intersect(inputAnswers.Select((y, index) => Tuple.Create(y, index)));      
Giedrius
  • 8,430
  • 6
  • 50
  • 91
  • 2
    This answer also includes falls positives, like the following 2 arrays: { 1,2,3 } && { 3, 1, 2 } when intersecting those and counting it it says 3. While the answer would be 0. – SynerCoder Aug 27 '12 at 07:25
  • @SynerCoder - it depends how you will use it, updated answer with code sample that uses intersect and produces right answer. – Giedrius Aug 27 '12 at 07:51
  • So it's only 2 at same position and output is 1, don't see a problem – Giedrius Aug 27 '12 at 08:09