Different possible marks for n questions and negative marking in C++ Program

C++Server Side ProgrammingProgramming

In this tutorial, we are going to write a program that finds different possible marks for the given n questions with positive and negative marking.

Let's say we have 10 questions and each carries 2 marks for correct answers and -1 marks for a negative answer. Our aim is to find all the possible ways in which a student can score in the exam.

Let's see the steps to solve the problem.

  • Initialize the number of questions, positive marks for the correct answer and negative marks for the wrong answer.

  • Initialize a set to store the possible marks.

  • Write two inner loops from 0 to a number of questions for all possible ways.

  • Let's assume the first loop variable is the correct answer, the section loop variable is not answered, and the remaining questions are incorrect.

  • Add the marks to the set.

  • Print the size of the set.

Example

Let's see the code.

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int findPossibleMarksCount(int n, int x, int y) {
   set<int> marks;
   for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
         // i = correct
         // j = not_answered
         marks.insert((x * i) - ((n - i - j) * y));
      }
   }
   return marks.size();
}
int main() {
   int n = 20, x = 2, y = -1;
   cout << findPossibleMarksCount(n, x, y) << endl;
}

Output

If you run the above code, then you will get the following result.

41

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

raja
Published on 27-Jan-2021 12:36:01
Advertisements