Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have spent multiply hours trying to solve with no luck. using C#

The local driver's license office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions. Here are the correct answers:

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
Your program should store these correct answers in an array. The program should read the student's answers for each of the 20 questions from a text file and store the answers in another array. (Create your own text file to test the application). After a student's answers have been read from the file, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It 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.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WernerCh7_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int score = 0;
int missed = 0;
string[] lines = File.ReadAllLines("Answers/Answers.txt");
string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
string[] userAnswers = null;

try
{

if (lines.Length != 0)
{
userAnswers = lines[0].Split(new char[] { ' ' });
}

if (userAnswers != null)
{

List<int32> wrongQuestions = new List<int32>();

for (int index = 0; index < correctAnswers.Length; index++)
{
if (userAnswers[index].Equals(correctAnswers[index])) score++;
else wrongQuestions.Add(index + 1);
}
if (score >= 15)
{
label1.Text = ("Pass");
}
else
{
label1.Text = ("Fail");

}
label2.Text = score.ToString("N");
missed = 20 - score;
label3.Text = missed.ToString("N");

MessageBox.Show("Incorrect questions: " + string.Join(",", wrongQuestions.ToArray()));

}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Posted
Updated 9-Jun-17 20:29pm

Quote:
have spent multiply hours trying to solve with no luck.

Programming is not a matter of luck, it is a matter of learning lessons and techniques and to apply them.
State your problem in code to get some help.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 
Comments
Member 13251426 10-Jun-17 11:34am    
i normal would not ask for help with this but the teacher admitted to us he has no clue on how to do this because he was just thrown in the class to teach it and I have been at it for a hours. i just wanted to be pointed in the right direction to know what i am doing wrong and not what the answer is. i should of worded my question a bit better sorry about that.
Patrice T 10-Jun-17 14:36pm    
"he teacher admitted to us he has no clue on how to do this because he was just thrown in the class to teach it"
This imply that your teacher is not qualified to teach your class and that a failure is expected.
Start by breaking this into chunks: Write a couple of methods which do discrete bits of the code: Read the student answers, process them, display the results.
Start by writing the "read" method - you've got that, pretty much:
C#
private string[] ReadStudent(string path)
    {
    return File.ReadAllLines(path);
    }

Then write the "process" method:
C#
private int[] ProcessStudent(string[] shouldBe, string[] was)
    {
    if (shouldBe.Length != was.Length)
        {
        throw new ArgumentException("Incorrect number of solutions!");
        }
    ...
    return new int[0];
    }
(We'll look at filling that in in a moment)
And the "display" method:
C#
private void DisplayResults(string[] shouldBe, int[] StudentErrors)
    {
    ...
    }

Your code to use them then becomes a lot more readable:
C#
string[] correctAnswers = { "B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A" };
string[] studentAnswers = ReadStudent("Answers\\Answers.txt");
int[] studentErrors = ProcessStudent(correctAnswers, studentAnswers);
DisplayResults(correctAnswers, studentErrors);

Display results can calculate the numbers it needs to print from just the two parameters:
The number of correct answers is the length of the correctAnswers array, minus the length of the studentErrors array.
The number of incorrect answers is the length of the studentErrors array.
The question numbers that were wrong are in the studentErrors array.

I'll leave that as an exercise for the reader - it's pretty simple!

The "real work" is done in the ProcessStudent method - and it's pretty simple if you have been introduced to C# Collections such as the List<> class - but I'm going to assume you haven't, as arrays are a poor way to do this whole exercise if you are aware of Collections!

So we'll do this with arrays.
Start by declaring a temporary array to hold the wrong answer numbers - we'll make it big enough to hold a really bad student:
C#
int[] wrong = new int[shouldBe.Length];

Now, we want to look at the answers given, and fill out the wrong answers. For that we need to indexes:
C#
int[] wrong = new int[shouldBe.Length];
int wrongIndex = 0;
for (int i = 0; i < shouldBe.Length; i++)
    {
    //...
    }
Inside the loop it's pretty simple as well: check the answer, and if it's wrong, add the number to the errors array:
C#
if (shouldBe[i] != was[i])
    {
    wrong[wrongIndex++] = i + 1;
    }
The index is zero-based, so we need to add one to get a one-based question number.
Now all we need to do is return only the error question numbers in the "wrong" array:
C#
int[] justTheWrongOnes = new int[wrongIndex];
for (int i = 0; i < wrongIndex; i++)
    {
    justTheWrongOnes[i] = wrong[i];
    }
return justTheWrongOnes;
And we're done!

However, I'd suggest that you read and understand this rather than copy'n'paste it into your code and hand it in: if you hand that in exactly as I wrote it, your tutor will know that you didn't write it...so read it, understand it, and think of a similar solution that you can write yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900