Problem comparing arrays

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 3753 Views - Last Post: 17 November 2011 - 07:17 AM Rate Topic: -----

#1 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Problem comparing arrays

Posted 16 November 2011 - 12:38 AM

Ok here is the problem:
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 answers..Your program should store these correct answers in the array. The program should read the students answers for each of the 20 questions from a text file and store the answers in another array. After the student's answers have been read from the file the program should display a message indicating whether the student passed or failed. 15 out of 20 is passing. Should then display total number of correctly and incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions...
I am having a hard time figuring out how to actually get the program to grade my text file. I am not sure how to compare them. I have looked all through my book but the only similar example uses only int data types. Any help would be greatly appreciated!

Here is what I have done so far...this time..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DriverLicenseExamProb
{
    public partial class driverExam1 : Form
    {
        public driverExam1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void GradeButton_Click(object sender, EventArgs e)
        {

            const int ANSWER = 20;
            int[] tstAns = new int[ANSWER];
            int index = 0;
            StreamReader inputFile;
            inputFile = File.OpenText("answer.txt");
            while (index < tstAns.Length && !inputFile.EndOfStream)
            {
                tstAns[index] = int.Parse(inputFile.ReadLine());
                index++;
                
            }
            inputFile.Close();
        }
            {
                const int KEY=20;
                string[] ansKey=new string[KEY];
              
                string[] ansKEY={"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", 
                                    "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}; 
            }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem comparing arrays

#2 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 01:00 AM

Please someone let me know if I have done something wrong this is the first time i have ever posted here.
Was This Post Helpful? 0
  • +
  • -

#3 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 01:28 AM

I did more work on it. Is this even in the ballpark?


{
                string ansKey = {20};
                int tstAns = { 20 };
                Boolean arraysEqual = true;
             
                if (ansKey.Length != tstAns.Length)
                {
                    arraysEqual = false;
                }
                while (arraysEqual && index < ansKey.Length)
                {
                    if (ansKey[index] != tstAns[index])
                    {
                        arraysEqual = false;
                    }
                    index++;

Was This Post Helpful? 0
  • +
  • -

#4 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Problem comparing arrays

Posted 16 November 2011 - 03:41 AM

Lets have a look, you have made a good start.

Lets simplify things.

-You need an array of answers. You have this already, which is good! You need to put this in the body of the class though (which is the form {} block)
-You need to declare an array to hold the answers in, you have done this, great! You could base the length of this on the length of the answer array though to simplify further.
-You need to open the file and read it's contents. This should always be in a using block:
using (StreamReader reader = new StreamReader("path of file"))
{
}


This is so important as it ensures the IDisposable.Dispose method is called on the StreamReader which takes care of releasing the file handle etc, and just general tidying up. Get into a habit of using this. (Check this recent topic for a great explanation of using using!)
-Itterate each line of the file and read the answer into the student answer array, which you are already doing. You are parsing to an int, is this correct as the stored answers are strings? This will throw an error if the string you are trying to parse cannot be parsed to an int, and you have no error handling.
-Once you have read all the answers, you need to itterate through all the answers, and compare them, one array to another. The simplist way of keeping score is to just have an int, say answer, and you increment answer each time the results match. You could also make a List<int> that you use to store the index of the incorrect answers so that you can tell the user which answers they got wrong.
-You will then need to display the results

Which part are you stuck on?

This post has been edited by ragingben: 16 November 2011 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#5 HariC  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 03:42 AM

Please mention what you have tried until now in your code. And my suggestion regarding logic will be to use string.Compare to compare the strings and if they are not equal, check the indexes which are not equal, and find out how many are wrong and then display the grade.
Was This Post Helpful? 0
  • +
  • -

#6 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Problem comparing arrays

Posted 16 November 2011 - 04:08 AM

Never done reading from text files myself so I'll leave that for someone else to cover.

If you create two object arrays you can compare them to each other. I'd do it with a couple of classes, creating my objects from them.
http://www.dotnetper...om/object-array


Class 1 stores all the correct answers
Array of constants that aren't gonna change

Class 2 stores stores users answers
Answer 1 { Get; Set; }
Answer 2 { Get; Set; }
Answer etc { Get; Set; }

You'd have these populated from outside the class by what ever method your using to take the answers from the text file. The answers inside and outside would be independant of each other, so you can reuse it for each user.

Then you can loop through the arrays to compare them.

// "i" is position in array, we start at the first which is 0.
// correctAnswers and studentAnswers are my object arrays containing all the answers.


// Loop through if statement, compare each one until all array members compared.
if (correctAnswers[i] != studentAnswers[i])
{
return false;
}


This post has been edited by negligible: 16 November 2011 - 04:10 AM

Was This Post Helpful? 0
  • +
  • -

#7 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Problem comparing arrays

Posted 16 November 2011 - 05:13 AM

There is more than one way of solving this problem by the way, although if this is a homework assignment you should demonstrate you understand the method you are being asked to use.
Just saying if you're working on a problem don't feel boxed into one way of doing it.

For example you could use Tuples, which are new to .NET 4.0, which I was just reminded of.
var t = new Tuple<string, string, string, string, string> ("One", "Two", "Three", "Four", "etc")
var t2 = new Tuple<string, string, string, string, string> ("One", "Two", "Three", "Four", "etc")

//Tuples will return false when compared directly like an array.
//But it has the Equals method so you can do...

t1 == t2       // False, doesn't work as you might think it should.
t1.Equals (t2) // True, works.


This post has been edited by negligible: 16 November 2011 - 05:13 AM

Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Problem comparing arrays

Posted 16 November 2011 - 07:40 AM

So it looks like you have reading from a file. You also have the answer array and student answer array. Now you just need to compare them.

for (int i = 0; i < ansKEY.Length; i++)
{
    if (ansKEY[i] == tstAns[i])
    {
       // answer is correct
    }
    else
    {
       // answer is wrong. do something here.
    }
}



Also, notice that you are reading the data from the file and converting it to a integer. However, your answer key are letters. Of course you know, '1' is not equal to 'A'. So you need to change your text file to have the answers as A/B/C/D, and you do NOT want to convert that to an integer.
Was This Post Helpful? 0
  • +
  • -

#9 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 11:46 AM

I just got home from school and saw all the posts on this. I started over earlier after I talked to my teacher and she nudged me in some places. It still isn't running but I think I am alot closer.

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

namespace Drivers_License_Exam
{
    public partial class driversLicenseExam : Form
    {
        public driversLicenseExam()
        {
            InitializeComponent();
        }


        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void driversLicenseExam_Load(object sender, EventArgs e)
        {
            const int SIZE = 20;
            string [] answers = {"B", "D","A", "A", "C", "A", "B", "A", "C", "D", 
                                    "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
            string [] inputAnswers= new string [SIZE];
            int index = 0;
            int correct=0;

            int incorrect=0;
            int totalIncorrect=0;
            

            StreamReader inputFile;
            inputFile = File.OpenText("testanswers.txt");

            while (index < answers.Length && !inputFile.EndOfStream)
            {
                inputAnswers[index] = inputFile.ReadLine();
            
                


                if (answers[index] != inputAnswers[index])
                {
                    incorrect++;
                   //answerLabel.Text= totalIncorrect + incorrect+"Which questions are incorrect?" + (index + 1) + "ln";
                   
                }
                    
                       
                else
                {
                
                        correct++;
                }

               // if
               // { 
                  //  correct >= 15; 
                //MessageBox.Show("You failed");
            //}
                    inputFile.Close();

            }
           answerlabel.txt=incorrect.tostring



Thanks to everyone for taking time to help me with this!
Was This Post Helpful? 0
  • +
  • -

#10 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 12:17 PM

If anyone can look over the new code I posted and give me suggestions it would be greatly appreciated!
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Problem comparing arrays

Posted 16 November 2011 - 01:49 PM

You need to give us more information. What does "it's not running" entail?

One thing to note, you aren't incrementing index, so your while loop will go on forever.
Was This Post Helpful? 0
  • +
  • -

#12 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 02:05 PM

View Posteclipsed4utoo, on 16 November 2011 - 01:49 PM, said:

You need to give us more information. What does "it's not running" entail?

One thing to note, you aren't incrementing index, so your while loop will go on forever.

I am not receiving any errors but it does not work. Nothing shows up in my lable. I am still working on getting all of the information to display correctly. I will work on fixing the index. Thanks a lot! I did not realize I had not incremented it.
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Problem comparing arrays

Posted 16 November 2011 - 02:09 PM

One of the most important talents of a developer is their ability to debug. Here is an awesome debugging tutorial.

http://www.dreaminco...6780-debugging/

Now try debugging your application and I guarantee you will find where your issues are.
Was This Post Helpful? 0
  • +
  • -

#14 nikkycole24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-November 11

Re: Problem comparing arrays

Posted 16 November 2011 - 02:21 PM

View Posteclipsed4utoo, on 16 November 2011 - 02:09 PM, said:

One of the most important talents of a developer is their ability to debug. Here is an awesome debugging tutorial.

http://www.dreaminco...6780-debugging/

Now try debugging your application and I guarantee you will find where your issues are.

I ran debug in microsoft visual c# 2010. It is not giving me any errors. I am going to go look at the debugging tutorial and see if I can figure anything else out. Thanks again for all your help!
Was This Post Helpful? 0
  • +
  • -

#15 eclipsed4utoo  Icon User is online

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,958
  • Joined: 21-March 08

Re: Problem comparing arrays

Posted 16 November 2011 - 02:27 PM

Debugging is not simply hitting F5 in Visual Studio and seeing if errors occur. Debugging is setting breakpoints in the code and stepping through the code while you evaluate the values of variables. This is explained in the tutorial.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2