Dsa Assignment-1
Dsa Assignment-1
BATCH – D-1
TOPIC
Design and develop a question paper formation tool using an efficient data structure. Create three
categories for pool of questions namely EASY, MEDIUM and HARD and store in separate files with 25
questions in each category for different topics of data structure subject. System should select questions
randomly based on the count and difficulty level entered by the user from each category. Finally, your
system should generate a question paper (Nirma University Format) for specified marks entered by user.
Constraints:
METHODOLOGY
1.
#include <bits/stdc++.h>
#include <fstream>
#include <fstream> - is used in C++ to include the standard C++ library header for file input
and output operations.
2.
using namespace std;
constant integer named MAX_LINES with a value of 10,000. It limits the maximum number of
lines that can be read from each file.
This function is responsible for saving a vector of selected question indices to a file. It
takes two parameters:
indices: A constant reference to a vector of integers, which contains the question indices that
were selected for a particular category (easy, medium, or hard).
It iterates through the indices vector and writes each index to the file.
The purpose of this function is to store the selected question indices so that they can be
reused in future runs of the program. This way, the program avoids selecting the same
questions in subsequent runs.
4.
// Function to load selected question indices from a file
vector<int> loadSelectedIndices(const string &loadselInd)
{
vector<int> indices;
ifstream file(loadselInd);
int index;
while (file >> index)
{
indices.push_back(index);
}
return indices;
}
This function is responsible for loading previously selected question indices from a file.
It reads integers from the file, assuming one index per line, and stores them in a vector.
It returns the vector containing the loaded indices.
The purpose of this function is to retrieve the previously selected question indices that were
saved during previous runs. These indices are used to prevent the selection of the same questions
in the current run of the program. By comparing the current selections with previously saved
indices, the program ensures that questions are not duplicated across different runs, maintaining
variety in the generated question paper.
So basically, saveSelectedIndices is used to store the selected question indices for future
reference.
And loadSelectedIndices is used to retrieve these indices from previous runs.
This functionality helps maintain the integrity of the generated question paper and prevents the
same questions from being selected repeatedly.
5.
int main()
{
ifstream easy_file("/Users/nishitasolanki/Downloads/EASY (1).txt");
ifstream med_file("/Users/nishitasolanki/Downloads/MEDIUM.txt");
ifstream hard_file("/Users/nishitasolanki/Downloads/HARD.txt");
string easy[MAX_LINES];
string med[MAX_LINES];
string hard[MAX_LINES];
In the above ,
easy_file, med_file, and hard_file, each opened with a specific file path. They are used
for reading the contents of text files.
if condition – checks if any of them failed to open and it prints an error message and
returns with an exit code of 1 to indicate an error.
Declares arrays of strings named easy, med, and hard. These arrays are used to store the
lines of questions read from the respective files.
6.
// Read questions from the easy file
while (getline(easy_file, ques) && cnt < MAX_LINES)
{
easy[cnt] = ques;
cnt++;
}
easy_file.close();
cnt = 0;
string ques1;
cnt = 0;
string ques2;
Initializes an integer variable cnt to 0, which will be used to count the number of lines
read.
Defines a string variable ques to temporarily store each line of a question.
Reads questions from the "EASY (1).txt" file It reads lines using getline, stores them in
the ques variable, and assigns them to the easy array at the position indicated by cnt. It
also increments cnt. The loop continues as long as there are lines to read (getline returns
true) and the number of lines read is less than MAX_LINES.
And at last after reading all the questions, it closes the easy_file stream.
7.
int n1, n2, n3, marks;
cout << "Enter the number of easy questions: " << endl;
cin >> n1;
cout << "Enter the number of medium questions: " << endl;
cin >> n2;
cout << "Enter the number of hard questions: " << endl;
cin >> n3;
cout << "Enter the marks" << endl;
cin >> marks;
Declares integer variables n1, n2, n3, and marks to store the user's input for the number of easy,
medium, and hard questions, as well as the total marks for the question paper.
8.
int m1 = (2 * n1) + (5 * n2) + (10 * n3); // check for marks validity
if (m1 > marks)
{
cout << "ERROR!" << endl;
}
Checks if the total marks entered by the user doesn’t exceeds the marks calculated of the total
ques entered by the user.
9.
else
{
string head = "NIRMA UNIVERSITY QUESTION PAPER";//padding to print it in the middle.
int totalWidth = 120;
int padding = (totalWidth - head.length()) / 2;
For designing – It prints the Nirma University Question Paper in the middle.
10.
srand(static_cast<unsigned>(time(nullptr))); // Seed with current time
It seeds the random number generator using the current time, ensuring that the generated
random numbers will be different in each program run.
It initializes the variable qno to 1, which represents the question number on the question
paper.
11.
vector<int> selectedEasyIndices, selectedMediumIndices, selectedHardIndices;
13.
for (int i = 0; i < n1; i++)
{
int q;
do
{
q = rand() % cnt;
} while (find(selectedEasyIndices.begin(), selectedEasyIndices.end(), q) != selectedEasyIndices.end() ||
find(previousEasyIndices.begin(), previousEasyIndices.end(), q) != previousEasyIndices.end());
selectedEasyIndices.push_back(q);
cout << qno++ << easy[q] << endl;
}
q = rand() % cnt; - This line generates a random number q within the range [0, cnt) using
the rand() function. The variable cnt represents the total number of questions available in
the "easy" category.
The do-while loop begins here. The purpose of this loop is to keep generating a random
number q until two conditions are met:
find(selectedEasyIndices.begin(), selectedEasyIndices.end(), q) !=
selectedEasyIndices.end() - This condition checks if the randomly generated q is already
present in the selectedEasyIndices vector. If it is, it means that this question has already
been selected in the current run, and the loop continues to generate a new random
number.
find(previousEasyIndices.begin(), previousEasyIndices.end(), q) !=
previousEasyIndices.end() - This condition checks if the randomly generated q is
present in the previousEasyIndices vector, which stores indices of questions selected in
previous runs. If it is found in the previous indices, it means the question was selected in
a previous run, and the loop continues to generate a new random number.
The loop continues until it finds a random number q that satisfies both conditions, ensuring that
the selected question is neither a duplicate within the current run nor a repeat from previous runs.
This code selects new questions that haven't been chosen in the current run or any previous runs.
It helps maintain variety and prevents the same questions from being included in the generated
question paper.
14.
cout << endl;
string head2 = "SECTION - B(5 Marks)";
int padding2 = (totalWidth - head2.length()) / 2;
return 0;
}
It allows the program to remember which questions were selected in the current run. This
prevents the same questions from being selected again in the same session, ensuring that
the question paper has variety.
It enables the program to maintain a history of selected questions across different runs.
By checking against the indices stored in these files, the program ensures that questions
selected in previous runs are not duplicated in future runs.
This part of the code is responsible for saving the selected question indices to files, both for
maintaining session-to-session consistency and avoiding repetition of questions in future runs of
the program.
OUTPUT
PAPER -1
PAPER -2
PAPER- 3
From the above we can see that there are no questions repeated in the 3 paper generated.