3 Replies - 11384 Views - Last Post: 24 December 2007 - 09:27 AM Rate Topic: **--- 1 Votes

#1 manurulzforever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 07

Logic for quiz programs

Post icon  Posted 23 December 2007 - 08:21 AM

I am supposed to write a quiz program with about 10 questions and the questions must be randomized every time i run the program.
I tried a lot of stuff but it did not exactly work out.
I am using turbo C++(dos version and for some reason i am not able to copy and paste the code onto the site or maybe i dont know how)...

first i tried using a 2 dimensional array and put in 10 questions but when i tried randomizing the questions it would randomize the questions but some questions were repeated twice & sometimes more than that.

how do i correct that as each question should only appear once.

i thought of using classes and structures, but i couldn't exactly understand how they are used within a program..

would really appreciate any help....
you dont have to give me the program, jus tell me how to bout solving it.

Is This A Good Question/Topic? 0
  • +

Replies To: Logic for quiz programs

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,767
  • Joined: 16-October 07

Re: Logic for quiz programs

Posted 23 December 2007 - 10:58 AM

Make a separate array of int, with 0..9 as the values. Randomize that list. Then just loop through the list, pick the values out of the list, and use the value to pick the question.

For the questions themselves, I'd make an array of struct. Easier still, just use a vector.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 manurulzforever  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 07

Re: Logic for quiz programs

Posted 24 December 2007 - 08:47 AM

View Postbaavgai, on 23 Dec, 2007 - 10:58 AM, said:

Make a separate array of int, with 0..9 as the values. Randomize that list. Then just loop through the list, pick the values out of the list, and use the value to pick the question.

For the questions themselves, I'd make an array of struct. Easier still, just use a vector.

Hope this helps.



well the thing is i dont really know much about structures and classes and all such stuff and how they are used within a program so could you give a few examples or maybe some links through which i can understand their usage.
also could you tell what exactly is public and private bcoz i heard somethin bout it related to structures
..... thanx anyways for your help
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,767
  • Joined: 16-October 07

Re: Logic for quiz programs

Posted 24 December 2007 - 09:27 AM

Here's how I'd set up a list of questions.

#include <stdlib.h>
#include <vector>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

// A basic structure for questions
typedef struct Question {
	const char *question;
	int answer;
} Question;

// A vector list, because arrays are a pain
typedef vector<Question> QuestionList;


void addQuestion(QuestionList &list, const char *question, int answer) {
	Question q;
	q.question = question;
	q.answer = answer;
	list.push_back(q);
}


void initQuestions(QuestionList &list) {
	addQuestion(list, "M", 1000);
	addQuestion(list, "D", 500);
	addQuestion(list, "CD", 400);
	addQuestion(list, "MCM", 1900);
	addQuestion(list, "VIII", 8);
	addQuestion(list, "IX", 9);
}


void dumpList(QuestionList &list) {
	for(int i=0, j=list.size(); i<j; i++) {
		Question q = list.at(i);
		cout << q.question << " = " << q.answer << endl;
	}
}


int main(int argc, char** argv) {
	QuestionList list;
	initQuestions(list);
	dumpList(list);
	return (EXIT_SUCCESS);
}



Now all you have to do it use your questions and randomize them. Good luck.
Was This Post Helpful? 1

Page 1 of 1