Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, so i am having a logical error i cant seem to store and incrament the questions variable

Program Specifications The human resources department has asked you to write a program that grades the candidate’s technical skills. The exam has 20 multiple choice questions. Here are the correct answers: 1. E 2. D 3. D 4. B 5. A 6. C 7. E 8. B 9. D 10. C 11. D 12. A 13. A 14. D 15. E 16. E 17. A 18. E 19. A 20. D A candidate must correctly answer 15 out of 20 questions to pass the exam and advance to the next level of interviews. The exam questions are printed on paper and the candidate enters their answers to the questions using the program you write. After the candidate enter all the answers to the questions, the program will either display a message indicating that they passed the exam and the total number of correct answers or a message indicating that they failed the exam and the total number of incorrect answers and the list of question numbers that the candidate answered incorrectly.

Sample data Expected result
Passed with 20 correct answers.
Passed with 15 correct answers.
Failed with 10 incorrect answers. Incorrect questions: 6, 7, 8, 9, 10, 16, 17, 18, 19, 20 if i need to make alterations to my code/ questions please LET ME KNOW, i will try and make it readable as possible



Thank you

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPSC1012_CorePortfolioAssignment3_EmilyJohnson
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare variables
            char[] anwserKey = { 'E', 'D', 'D', 'B', 'A', 'C', 'E', 'B', 'D', 'C', 'D', 'A', 'A', 'D', 'E', 'E', 'A', 'E', 'A', 'D' };
            char anwser;

            int[] questions = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
            int correct = 0;
            int incorrect = 0;
            
            int question = questions.Length;
            int wrong = 0;
           
            for (int i = 0; i < questions.Length; i++)
            {
                question = i + 1;
                Console.Write("{0}: ", question);
                anwser = char.Parse(Console.ReadLine().ToUpper());

                if (anwser == anwserKey[i])
                {
                    correct++;
                }
                else
                {
                    incorrect++;
                    
                    if(anwser != anwserKey[i])
                    {                        
                        questions[i] = i + 1;
                        question = questions[i];
                    }
                }
            }
            if (correct >= 15)
            {
                Console.WriteLine("Passes! you got {0} questions correct", correct);
            }

            else
            {
                Console.WriteLine("Failed! you got {0} questions wrong", incorrect);
                Console.WriteLine("Incorrect Question: ");
                for (int n = 0; n < questions.Length; n++)
                {

                    Console.Write("{0} ", questions[incorrect]);
                }


            } Console.ReadLine();

        }

    }
}
Posted
Updated 24-Nov-16 20:23pm

1. Look at this part:
C#
if(anwser != anwserKey[i])
{
    questions[i] = i + 1;
    question = questions[i];
}

do you mean:
C#
if(anwser != anwserKey[i])
{
    i = i + 1;
    question = questions[i];
}

? Even that, it is wrong. The increment of question have nothing to do with the correctness of answer.
2. The answerKey is an array, each of its answer element is identified by an index number, why not use this index number as the question number rather than creating another array called questions?

3. The two variables correct and incorrect always add up to total number of questions, do you need both?
+++++ [Round 2 ] +++++
4. If you want to be able to output the question numbers that have the wrong answers, then you will need to store these question numbers, I suggest using a List, e.g.
C#
List<int> questionWithWrongAnswer = new questionWithWrongAnswer<int>();
// other code omitted
 
if (anwser == anwserKey[i])
{
    correct++;
}
else
{
    incorrect++;
                    
    questionWithWrongAnswer.Add(i);
}

Learn C# List[^]
5. Last but not least, what do you think of this:
incorrect = answerKey.Length - correct;
 
Share this answer
 
v6
Comments
SpazticUnicorn 25-Nov-16 2:21am    
tht is a good point, i will change that to an index....
good point i canadd that to a count ... thank you

as for the look at point i orignally did the same as the "do you mean"...it seems to not want to store the question number.....
SpazticUnicorn 25-Nov-16 2:26am    
just tested the code ...i do need incorrect and correct, due to the result
Peter Leow 25-Nov-16 3:16am    
More added in my solution.
SpazticUnicorn 25-Nov-16 3:44am    
Thank you so much .... i havent completed the code yet ... however, i think this will work ...i cant thank you enought ... ill keep posted if i have not made progress
First I would simplify
C#
for (int i = 0; i < questions.Length; i++)
{
    question = i + 1;
    Console.Write("{0}: ", question);
    anwser = char.Parse(Console.ReadLine().ToUpper());

    if (anwser == anwserKey[i])
    {
        correct++;
    }
    else
    {
        incorrect++;

        if(anwser != anwserKey[i])
        {
            questions[i] = i + 1;
            question = questions[i];
        }
    }
}

by removing weird useless code
C#
for (int i = 0; i < questions.Length; i++)
{
    question = i + 1;
    Console.Write("{0}: ", question);
    anwser = char.Parse(Console.ReadLine().ToUpper());

    if (anwser == anwserKey[i])
    {
        correct++;
    }
    else
    {
        incorrect++;
    }
}

Then, I would kick the debugger to see what the code is doing.
Then you have to think how to halter questions, which already contain questions numbers, to be able to tell which ones was failed.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
The problem is in your mind.
Storing the question number at question position in questions would ne a good idea if questions was not already containing the question number at same position.
You have to define what you store in questions and when, in order to know which question was failed.
 
Share this answer
 
v2
Comments
SpazticUnicorn 25-Nov-16 2:35am    
i beenusing the debugger, and i been putting that back in and out ...the issue is the stroing of the question number ... no matter what i do it will not store the number i been redoing steps, using the debugger for hours... i been adding and taking away useless code and methods .... however, thank you for your input

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