1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Create a simple quiz program

Discussion in 'Mac Programming' started by Azrak, Apr 2, 2013.

  1. Azrak, Apr 2, 2013
    Last edited: Apr 3, 2013

    macrumors newbie

    I have a history exam and for preparation i wanted to create a quiz (not a multiple answer one). I searched on many websites for an easy way to create a quiz but none of them corresponded to what i really want.

    What I want is a quiz with questions in a random order (I prefer the order to vary each time i take the quiz) and I have to answer the questions by typing my result, not by clicking on it (i don't want the quiz to be a multiple answer quiz). Right after typing my result i want the quiz to display the correct answer (whether my result is correct or false).

    Can anyone of you help me create a program that could do just that? I have done some programming before so I do know how it works but I'm not good enough to create my own program by myself. If not, does anyone know of a site that lets you create your own quiz but this type of quiz explained above (I want the quiz to work this way)?

    All your help will appreciated
  2. macrumors member

    I think it helps if you write down for yourself, what this program is supposed to do.

    To start with, your quiz is a collection of questions, objects. And each question has multiple answers to pick, and the correct andwer.

    How are these answers stored? How do you want the quiz to appear on screen?

    These are things to tuink about.
  3. macrumors 603


    If its console based, I think the quickest and easiest way to do this would be:

    Make a text file with your Questions and Answers. Have it follow a simple pattern like:

    "Question? Answer.
    Another question? Another answer."

    with one question? Answer pair per line.

    Read the lines into a container (IE, a vector in C++), then shuffle the container. A simple shuffle algorithm I've used is:

    Seed a random number generator.
    For the amount of items (n) in the container, generate a random number between 0 and n-i-2, where i is how many iterations already completed. Then swap the item at index n-i-1 with the item at the index randomly generated.

    Now loop with:
    Get the substring up to and including the question mark and print it.
    Wait for the user input.
    Get the substring past the question mark and print it.
  4. macrumors newbie

    What do you mean by
    I will create all my questions and associate an answer to each question.
    I want the program to ask me those questions in a random order and prompt me to type the answer. After i type my answer i want the program to show me the correct answer whether or not my answer is correct.
  5. macrumors newbie

    I know i can find a site where i can create my own quiz but none of those sites work the way i want them to. What I want, that is different from all websites that offer me to create a quiz, is the quiz to display the correct answer after answering each question. This is the important part for me and that is why im trying to create my own program.
  6. macrumors regular


  7. ytk
    macrumors regular

    What language are you trying to do this in?

    Here's a program that will do it in Ruby:
    #!/usr/bin/ruby -w
    quiz_data = File.read( ARGV[0] ).split "\n"
    questions = []
    questions << [quiz_data.shift, quiz_data.shift] while quiz_data.size > 0
    questions.each do |q|
      print q[0] + " "
      puts q[1]
    Save your questions in a text file, one question per line followed by the answer on a separate line. Then invoke the program with “./quiz.rb <filename>”
  8. macrumors 603


    Ruby is the language for which my knowledge is weakest (of all languages I know at all) - what is the << operator doing and how do you end up shuffling while keeping the questions and answers matching up?
  9. macrumors newbie

    I am on a mac. Is it possible for me to use Ruby? How can i do that?
    And Ytk what do you mean exactly by "Then invoke the program with “./quiz.rb <filename>” "?

    Thanks a lot
  10. macrumors 603


    ... I'm coming to the conclusion you have virtually no programming experience given you don't know C++, Ruby, or how to use Terminal.

    I suggest reading this free ebook and doing the exercises if you're interested in learning how to program with Ruby. I haven't read it, but I've read other books in the series by the same author:

  11. ytk
    macrumors regular

    In this context, << appends to an array. Note that I am appending an array to the main array, with each subarray containing a question/answer pair. So the call to shuffle! (the ! is a Ruby convention that signifies an in-place operation) only shuffles a bunch of arrays, but doesn't break out each individual array. So the question and answer pairs stay synchronized.
  12. macrumors 603

    When is the history exam? That date will determine the deadline date for making the quiz program. Having a program too close to, or even after, the exam date is worthless. One needs sufficient time to actually use the program to learn the material.

    If the deadline is anything less than maybe a month away, you should probably forget about writing a program. It seems like writing a program would be a significant diversion from the actual goal, which is studying history. In particular, all the time spent getting the program to work does nothing to help you memorize any history material.

    Instead of a quiz program, maybe make "flashcards" with the question on one side and the answer on the reverse. This is fairly easy to do in a word-processor, where you layout the questions on one page, and put the answers opposite the question on the next page. Print it on card stock (visit an office-supply store) using a double-sided (duplexing) printer, and the answers will end up on the reverse sides of the questions. I recommend printing a trial run on plain paper first, to confirm that print settings, layout, etc. are correct. Then cut apart with scissors or a paper cutter, shuffle, and use.

    The page layout of questions and answers can be reused fairly easily with any number of new questions and answers. There may even be specialized apps that let you make your own flashcards, either printed or entirely computerized. If you insist on having a program to do this, I'd start by looking for apps with those keywords: flash card or flashcard.

  13. macrumors G4

    Just a minute. If it ignores what you type this is TRIVIAL Here is the logic.

    1) Set N to (random number) * 2 - 1

    2) Display text number "N"

    3) accept any input and discard it with no processing

    4) display text number N+1

    5) go to #1

    Set up the data file so that odd numbered records are questions and even numbers are answers.


    Hate to say but if you have to ask about what is "./quiz.rb" then you are NOT going to be able to write a program of any kind. That question is as basic as it gets. You will have some months of study before you can do this yourself.

    Why not buy a pack of 200 index cards at the dollar store and be done with this?
  14. ytk
    macrumors regular

    Aaaand here's the same code as a one-liner, just for fun: :)

    #!/usr/bin/ruby -w
    File.read( ARGV[0] ).split("\n").each_slice(2).to_a.shuffle.each{ |x| puts x[1] if STDIN.gets unless print x[0] + " "  }
  15. macrumors regular


    You really ought to do it in Scratch. It is very powerful for those with no programming experience.

    My 12 year old son won a regional programming contest making a game in Scratch.

    If he can do it, so can you.
  16. macrumors 603


    I dislike things like Scratch. They have their limitations, and once you hit them, you're back to square 1: needing to know an actual programming language.

    That or you end up doing convoluted things just to work around the limitations. You're better off learning some actual programming languages.

    At 12, I was learning C. I taught myself C before I learned Algebra (and it helped immensely with understanding variables and functions.)
  17. macrumors regular


    I do not disagree with your thinking, but I'm not sure the OP is really interested in programming as a career choice. I think he/she just wants to make a quiz.

    Scratch is very well suited for making a quiz.

    And as for variables, Scratch has great support.
  18. macrumors newbie

    I'm sorry guys I didn't think it would be that complicated to create a program with a bit of your help.

    I actually am interested in programming, i'm thinking of applying for software engineering for college. I also do have a bit of experience in programming, but not enough to create my own program, I guess. I've learned a bit of xcode, visual basic and java u but only the basics. My priority now is actually my quiz, so i did try out Scratch. It worked pretty well, i finally create my quiz and it's up and working. The only thing that is missing for my quiz is that I can't make the questions asked in a random order, it is always asked in the same order from a to z. It's not a big deal though, I'm fine with my quiz.

    Thanks a lot.
  19. macrumors 603

    I don't understand this: "not enough to create my own program, I guess." You apparently wrote a program, and it's working, so based on that, you do have enough experience to create your own program. It may not do everything you wanted (yet), but the fact that you were able to learn a new language (Scratch) and write a program with it says a lot.

    Read this:

    Points 1-3 are especially relevant.
  20. macrumors regular


    You can randomize using a script like the attached image.

    Attached Files:

  21. macrumors member

    It is an interesting program. The screenshot shows, that you can program it, although there is no programming language, nor with a syntax and grammar and so on.

    Quartz Composer is something similar.


    I think you have to dive into the possibilities of Scratch.
  22. macrumors newbie

    Wow! That's exactly what i needed! Thanks a lot!
  23. macrumors regular


    Glad to help. If you need any more advice let me know.
  24. macrumors newbie

    Actually there is more to my scratch quiz than i thought.
    I have 18 questions and every time i start the program it asks me questions in a random order. How do I make it so it asks me the questions without repeating one? I mean if it already asked me question 4 i don't want it to ask me that question again.

    I set my variable "random" to pick a random number from 1 to 18 (I have 18 questions. How do it so next time "random" picks a random number from 1 to 18, it doesn't pick the last(s) number(s) previously picked?
  25. macrumors 603


    Keep an array of previously picked numbers and make sure the new number isn't on the list. Or do what I suggested and actually shuffle the cards. These may not be possible or unnecessarily difficult on account of the fact you've chosen Scratch.

Share This Page