In: Computer Science
What will be the values of A, B, C and D after execution of the following procedure using the “Scores”
dataset?
Note: Consider fractions up to 2 decimal places e.g. 10 / 3 =
3.33, 20 / 3 = 6.67
Step 1. Arrange all cards in a single pile called Pile 1
Step 2. Maintain five variables A, B, C, D, percentage and initialize them to 0
Step 3. If Pile 1 is empty then stop the iteration
Step 4. Read the top card in Pile 1
Step 5. Calculate percentage: divide TOTAL marks by 3 and store the result in percentage
Step 6. If percentage ≥ 85 then increment A
Step 7. If percentage < 85 and percentage ≥ 70 then increment B
Step 8. If percentage < 70 and percentage ≥ 60 then increment C
Step 9. If percentage < 60 then increment D
Step 10. Move the current card to another pile called Pile 2 and repeat from step 3
I am writing the code in C++ and i will use stack for database or storing pile's data I am pasting the code here and in the line after this // i will explain that line:
I am explaing the logic what to do, first of all we have to create data list where we can store our score then one by one we need to take data and do operation for calculating percentage then increment of A,B,C,D variable then we will store that store into second data list we will run this program until pile1 will empty.
Program code:
#include<iostream>
#include<stack> // to create a stack it is neccessary to add stack header file in the program.
using namespace std;
int main()
{
stack <int> pile1; // intializing first stack with name of pile1.
stack <int> pile2; //intializing second stack with name of pile2
int A=0,B=0,C=0,D=0,percentage=0; // declaring these variable according to question.
float fraction; // one more variable for storing fraction value
pile1.push(280); // now adding score in pile1 list.
pile1.push(210);
pile1.push(270);
pile1.push(180);
pile1.push(150);
pile1.push(260);
pile1.push(220);
pile1.push(240);
while(!pile1.empty()) // checking for pile1 is empty or not if empty then it will terminated or stop iteration
{
fraction = pile1.top() / 3; // here we are taking stack top element or one of them score for calculation of percentage as question is asking
percentage = fraction; //giving fraction value to percentage.
if(percentage >= 85) //comparing percentage value
{
A++; // if true then it will increment A.
}
else if(percentage < 85 && percentage >= 70)
{
B++; // if true then it will increment B
}
else if(percentage < 70 && percentage >= 60)
{
C++; // if true then it will increment C.
}
else if(percentage < 60)
{
D++; //if true then it will increment D
}
pile2.push(pile1.top()); //here moving that score into pile2 list
pile1.pop(); // removing that element from plie1 list so can next element of list will access.
}
cout<<"Value of A is "<<A<<endl; // now printing all the value of A, B, C, and D
cout<<"Value of B is "<<B<<endl;
cout<<"Value of C is "<<C<<endl;
cout<<"Value of D is "<<D<<endl;
return 0;
}
and the output will be
Value of A is 3
Value of B is 3
Value of C is 1
Value of D is 1.