Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Quiz simulation

c programming

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1 negru

negru

    CC Newcomer

  • Member
  • PipPip
  • 17 posts

Posted 12 May 2016 - 12:27 PM

I need help with the following problem:

Define a quiz and add two questions in it with given possible answers. Display the question one by one and allow the user to choose between 1, 2 or 3 (numbers that represent serial numbers of possible answers). If the user enters the character which is not 1, 2 or 3, then allow the user to choose an answer again. At the end of the quiz, print the final score in percentage.

Note:  The total number of questions is dinamically allocated (not known in advance). The user can manipulate with offered answers to the given question, and with the menu that allows him whether he wants to add to the next question, displays the result, or if the user wants to terminate the program. All program is run using command prompt.

 

Use the following definitions:

#define MAX 1000000
 
typedef struct
{
    char questionText[MAX];
    char firstAnswer[MAX];
    char secondAnswer[MAX];
    char thirdAnswer[MAX];
    int snca;//serial number of the correct answer (1, 2 or 3)
}QUESTION;
 
typedef struct
{
    int tnq;//total number of questions
    QUESTION *questions;
}QUIZ;
 
void addNewQuestion(QUIZ *qz,QUESTION *qn);//adds new question
void displayQuestion(QUIZ *qz,int snq);//prints question. 'snq' is the serial number of a question
int isCorrect(QUIZ *qz,int snq,int snqa);//checks if the chosen answer with the serial number 'snqa'
                                         //on a question with the serial number 'snq' is correct 
Example program:
 
1. In what year was the C programming language created?
 
1. 1845
2. 1832
3. 1972
 
Choose an answer: 3
 
2. Who is the author of the C programming language?
 
1. Bill Gates
2. Dennis Ritchie
3. Steve Jobs
 
Choose an answer: 10
Error. Choose between 1, 2 or 3.
Choose an answer: 2
 
Final score: 100% 

I don't understand a couple of thing in this problem:

1. How to add a question in the quiz, i.e. where to define the text of questions (text is not read, it is only displayed when the user chooses to add a new question)?
2. How to define the order of questions that the user is adding one by one?

 

Thanks for replies.



#2 negru

negru

    CC Newcomer

  • Member
  • PipPip
  • 17 posts

Posted 18 May 2016 - 05:52 AM

Here is how I did it using singly linked list (I am the OP):

#include <stdio.h>
#include <stdlib.h>
#define MAX 100000

typedef struct
{
    char text[MAX];
    char first[MAX],second[MAX],third[MAX];
    int sn;//serial number of a question
    int snca;//serial number of the correct answer
}QUESTION;

typedef struct list_node
{
    QUESTION qn;
    struct list_node *next;
}LIST_NODE;

void insertion(LIST_NODE **head,QUESTION *qn)
{
    LIST_NODE *newNode=(LIST_NODE *)malloc(sizeof(LIST_NODE));
    newNode->qn=*qn;
    if(*head == 0 || (*head)->qn.sn > qn->sn)
    {
        newNode->next=*head;
        *head=newNode;
    }
    else
    {
       LIST_NODE *p;
       for(p=*head;p->next && p->next->qn.sn < qn->sn;p=p->next);
       newNode->next=p->next;
       p->next=newNode;
    }
}

LIST_NODE* searching(LIST_NODE *head,int sn)
{
   while(head && head->qn.sn != sn)
        head=head->next;
   return head;
}

int main()
{
    LIST_NODE *head=0;
    char option;
    int sn;
    int tnq=0;//total number of questions
    int tncq=0;//total number of correct questions
    float result;
    QUESTION q1={"In what year was the C programming language created?","1852","1872","1972",1,3};
    QUESTION q2={"Who is the author of C programming language?","Bill Gates","Dennis Ritchie","Steve Jobs",2,2};
    QUESTION qn;

    printf("Option 'A': Insert the first question.\n");
    printf("Option 'B': Insert the second question.\n");
    printf("\n");

    do
    {
      printf("Choose an option: ");
      scanf("%c",&option);
      fflush(stdin);

       if(option == 'A')
      {
            LIST_NODE *t1=searching(head,q1.sn);
            if(t1){
                t1->qn=q1;
                tnq++;}
            else{
                insertion(&head,&q1);
                tnq++;}
      }

      else if(option == 'B')
      {
            LIST_NODE *t2=searching(head,q2.sn);
            if(t2){
                t2->qn=q2;
                tnq++;}
            else{
                insertion(&head,&q2);
                tnq++;}
      }

      else if(option == 'P')
      {
         int answer;
         printf("Enter serial number of a question: ");
         scanf("%d",&sn);
         LIST_NODE *p=searching(head,sn);
         if(p)
        {
           printf("%d %s\n 1. %s\n 2. %s\n 3. %s\n",
                    p->qn.sn, p->qn.text, p->qn.first, p->qn.second, p->qn.third);

           do
           {
                printf("Choose an answer: ");
                scanf("%d",&answer);
                if(answer != 1 && answer != 2 && answer != 3)
                    printf("Error. Possible answer is 1,2 or 3.\n");
           }
           while(answer != 1 && answer != 2 && answer != 3);

           if(answer == p->qn.snca){
                    printf("Answer to the question with serial number %d is correct.\n",sn);
                    tncq++;
                    fflush(stdin);
                    }
           else{
                    printf("Answer to the question with serial number %d is not correct.\n",sn);
                    fflush(stdin);
               }
        }
         else
         {
            printf("No data about the question with serial number %d.\n",sn);
            fflush(stdin);
         }
      }

       else if(option != '0')
      {
        printf("Unknown option.\n");
        fflush(stdin);
      }

    }
    while(option != '0');

    result=((float)tncq * 100)/tnq;
    printf("Result: %.2f%%",result);

    return 0;
}

How to implement this program using arrays (as given in the original problem)?

Could someone modify it?



#3 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 23 May 2016 - 01:46 PM

Allocate enough memory for the number of questions to be entered - malloc(num_questions * sizeof(QUESTION));

realloc() if necessary to increase the buffer.

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image