Question:
C# comparing two string arrays?
Utkarsh
2012-08-23 07:15:06 UTC
I need to compare 2 arrays. Array testAnswer[] contains answers to an exam (e.g. A, D, C,), and array inputAnswer[] reads from a text file which has a student's answers to the exam (e.g. A, B, C). A message should then display
the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions. Finally, a message should display whether the student passed or failed the exam based on:

The student must answer 15 out of 20 questions correctly.

This is my code so far:
private void button1_Click(object sender, EventArgs e)
{


//Array holding answers to test
string[] testAnswer = new string[20] { "1. B", "2. D", "3. A", "4. A", "5. C", "6. A", "7. B", "8. A", "9. C", "10. D", "11. B", "12. C", "13. D", "14. A", "15. D", "16. C", "17. C", "18. B", "19. D", "20. A" };

//Reads text file line by line and stores it in array
string[] inputAnswer = System.IO.File.ReadAllLines(@"C:\Users\Momo\Desktop\UNI\Software tech\test.txt");

}

Help would be awesome! :D
Six answers:
AnalProgrammer
2012-08-23 08:39:46 UTC
This is going to sound like a stupid question and I am not so sure why I am having to ask it.

Why have you got a number in the answer array?



Here are three strings. Which one is equal to the first?

"1. B"

"1 B"

"1 B "



From a human point of view the answer is obvious. They are all equal. From a computers point of view they are all un-equal.

You already have the question number in the element number. so you do not need to have the number in the answer array. You only need the answer, "B". Now you can compare "B" with "b" and find them equal if you ignore the case.

So a simple for loop.



for (int i = 0; i < inputAnswer.Length && i < testAnswer.Length; i++) { //not all answers may be done.

if (String.Compare(inputAnswer[i], testAnswer[i], true) == 0) {

score++;

} //End if

} //End for



Have fun.
Mohammed Ansari
2012-08-26 00:13:49 UTC
Instead of this you can just Declare 4 arrays in your Program....



1st will save all the true Options of your Questions....

2nd Will save all the Options Answered by the user...

3rd You will compare both the Arrays and true Options will be save in this Array...

4th the Remaining Options which is not equal(by comparing 1st & 2nd Array) will save in this Array....



so your 3rd Array will provide you all right Answers and for counting you can use the length property of the Array. Similarly, 4rd Array will provide you all Wrong Answers and for counting you can use the length property of the Array
anonymous
2016-10-17 09:37:20 UTC
What does it advise to get the ASCII equivalent? Characters are saved(on maximum implementations) as ASCII (or utf-8, of which ASCII is a subset), so there is not something to 'get', this is already there. you could evaluate characters quickly, in a for loop. make specific to attempt for the null terminator('0' or basically 0) to renowned while the string is ended.
AJ
2012-08-23 13:19:59 UTC
Why are you using arrays? Classes are a much better choice.



Also you need to reformat the text file, they are junk.
Kevin B
2012-08-23 07:22:58 UTC
If "textbox1.text" = a d c, = false



You put the 3 letters that are incorrect.
Anna Claire
2012-08-23 07:22:53 UTC
I am batman


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...