Tuesday, December 16, 2014

Random Quiz Maker in C Language (Course Project)

It is a project that we have shown in our CSE115 C programming course of North South University.

You can find the project slides HERE

Questions.txt file should be placed in the project folder directory.

Source Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
int main()
{
    system("COLOR AC");
    printf("\n\n\t\t\t***************************");
    printf("\n\t\t\t**** Random Quiz Maker ****");
    printf("\n\t\t\t***************************");
    printf("\n\n\t\t\t***************************");
    printf("\n\n\t\tWelcome to the random quiz maker program.");
    printf("\n\t\tPlease be patient during the process.\n");
    printf("\n\n\t\t\t**** Random Quiz Maker ****\n");
    printf("\n\tPlease input your answer by inserting character value of the number:");
    printf("\n\n\t\t\t**** Random Quiz Maker ****\n");
    printf("\n");
    int i,j,random_question[20],k,flag,random;
    double exam_score=0, final_score;
    char students_answer;
    struct exam
    {

        int question_serial_no;
        char question[500];  //  Question string
        char option_no_1[10];
        char option_1_ans[30];
        char option_no_2[10];
        char option_2_ans[30];
        char option_no_3[10];
        char option_3_ans[30];
        char option_no_4[10];
        char option_4_ans[30];
        char answer;

    } question_details[40];

    srand(time(NULL));

    FILE *fp=fopen("Questions.txt","r");

    for(j=0; j<10; j++)
    {

        do
        {
            random= rand()%20+1;
            random_question[j]=random;
            flag=0;

            for(k=0; k<=j-1; k++)
            {
                if(random_question[k]==random_question[j]) flag=1;
            }

        }
        while(flag==1);

        for(i=0; i<20; i++)
        {

            fscanf(fp,"%d %[^\n] %s %[^\n] %s %[^\n] %s %[^\n] %s %[^\n] %c",
                   &question_details[i].question_serial_no, question_details[i].question,
                   question_details[i].option_no_1, question_details[i].option_1_ans,
                   question_details[i].option_no_2, question_details[i].option_2_ans,
                   question_details[i].option_no_3, question_details[i].option_3_ans,
                   question_details[i].option_no_4, question_details[i].option_4_ans, &question_details[i].answer);
            if(random==question_details[i].question_serial_no)
            {
                printf("%d %s\n %s %s\n %s %s\n %s %s\n %s %s\n",
                       j+1,question_details[i].question, question_details[i].option_no_1,
                       question_details[i].option_1_ans, question_details[i].option_no_2,
                       question_details[i].option_2_ans, question_details[i].option_no_3,
                       question_details[i].option_3_ans, question_details[i].option_no_4,
                       question_details[i].option_4_ans );

                printf("Your Answer:\t");
                fflush(stdin);
                scanf("%c",&students_answer);
                if(students_answer!=question_details[i].answer)
                {
                    printf("\nWrong!! correct answer is number %c\n", question_details[i].answer);
                }
                else
                {
                    printf("\n** Correct Answer **\n");
                }
                fflush(stdin);
                printf("\n\n");

                if(students_answer == question_details[i].answer) exam_score=exam_score+10;

            }

        }
    }

    printf("Total Score: %0.2lf",exam_score);
    fclose(fp);
    printf("\n\tClick To Collect Reward\t\n");
    printf("\n\tChoose The Right Value:: ");
    int reward;
    scanf("%d", &reward);
    if(reward==1)
    {
        int gd = DETECT, gm, x, y, color, angle = 0;
        struct arccoordstype a, b;
        initgraph(&gd, &gm, "C:\\TC\\BGI");
        delay(2000);
        while(angle<=360)
           {
              setcolor(YELLOW);
              arc(getmaxx()/2,getmaxy()/2,angle,angle+2,100);
              setcolor(RED);
              getarccoords(&a);
              circle(a.xstart,a.ystart,25);
              setcolor(YELLOW);
              arc(getmaxx()/2,getmaxy()/2,angle,angle+2,150);
              getarccoords(&a);
              setcolor(GREEN);
              circle(a.xstart,a.ystart,25);
              angle = angle+5;
              delay(50);
              outtextxy(265,230,"CONGRATULATION!");


           }
           getch();
           closegraph();
    }
    else
    {
        int gd = DETECT, gm, x, y, color, angle = 0;
        initgraph(&gd, &gm, "C:\\TC\\BGI");
        delay(1000);
        outtextxy(275,235,"CONGRATULATION!");
        getch();
        closegraph();
    }


    return 0;
}


0 comments:

Post a Comment

Hi ! I'm Raad. And this my personal blog. Welcome to my blog.