Question

In: Computer Science

c++. please read this task carefully. First, try given examples of input to your code, if...

c++. please read this task carefully.

First, try given examples of input to your code, if it works, then send it here.

Create structure Applicants with following fields:

struct applicants{

int id;

string name;

string surname;

int subject1,subject2,subject3,selectedSubject;

string specialCase;

int total; };

SpecialCase (if applicant has "Awardee of Olympiads", then this field is "true", otherwise "false").

Do not forget that all "Awardees of Olympiads" will gain grants automatically.
If the total points are same your algorithm have to choose an applicant with higher SelectedSubject (even with SpecialCase)
Assuming that you have only K grants to distribute.

Note: You have to design your program using structures and functions.


Input:
First line contains M(total amount of applicant) and K(amount of grants that have to be distributed) (0 < M < 1001) and (1<=K<=M).
Then N lines are provided in format described above.


Output:
K lines sorted by total points( Total Points = Subject1 + Subject2 + Subject3 + SelectedSubject ) in format:
ID Name Surname Total_sum_of_points

example of input:

(There can be any inputs, it's just one of an examples)

(No means that they are not "Awardee of Olympiads")

10 5
1 Kimmy Marrow 12 23 4 14 NO
2 Baythen Mcclay 6 6 19 3 NO
3 Christen Stew 11 7 3 5 NO
4 Plein Phillip 15 19 8 4 NO
5 Beist Constant 15 3 17 11 NO
6 Sam Said 7 17 18 1 NO
7 Maclay Istan 5 5 16 17 NO
8 Sara Agit 15 17 24 18 YES
9 Nicolya Kim 24 12 23 23 NO
10 Neit Dimond 22 1 18 7 YES

Output:

9 Nicolya Kim 82
8 Sara Agit 74
1 Kimmy Marrow 53
10 Neit Dimond 48
5 Beist Constant 46

 

Solutions

Expert Solution

Algorithm :
Step 1 :
Take all the applicants data in a vector of applicants type.
Step 2 : Then , sort that vector using your own compare function.
Step 3 : In compare func , we compare the total values if they are not equal , otherwise we compare selectedsubject values.
Step 4 : Now , print first k values of the vector.



Output :

Here's Code :

#include<bits/stdc++.h>
using namespace std;

struct applicants{
    int id;
    string name;
    string surname;
    int subject1,subject2,subject3,selectedSubject;
    string specialCase;
    int total;
};

static bool compare(applicants a1,applicants a2){

    if(a1.total == a2.total)
    return a1.selectedSubject > a2.selectedSubject;
    return a1.total > a2.total;
}
int main()
{
    vector<applicants> v,ans;
    int n,k;
    cin>>n>>k;
    int id,sub1,sub2,sub3,selsub;
    string name ,surname,special;
    for(int i=0;i<n;i++)
    {
        cin>>id>>name>>surname>>sub1>>sub2>>sub3>>selsub>>special;
        applicants a1 ;
        a1.id  = id;
        a1.name = name;
        a1.surname = surname;
        a1.subject1 = sub1;
        a1.subject2 = sub2;
        a1.subject3 = sub3;
        a1.selectedSubject = selsub;
        a1.total = sub1 + sub2 + sub3 + selsub;
        a1.specialCase = special;
        if(special == "YES")
            ans.push_back(a1);
        else
            v.push_back(a1);
    }
    int z = ans.size();
    sort(v.begin(),v.end(),compare);
    cout<<"Output : "<<endl;
    for(int i=0;i<k-z;i++){
        ans.push_back(v[i]);

    }
    sort(ans.begin(),ans.end(),compare);
    for(int i=0;i<ans.size();i++){
        cout<<ans[i].id<<" "<<ans[i].name<<" "<<ans[i].surname<<" "<<ans[i].total<<endl;
    }

    return 0;
}

Related Solutions

C++ Please read the question carefully and make sure that the function prototypes given are used...
C++ Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part. The output example is provided at the end of the question. First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array. Part 1: Write a function...
C++ Please read the question carefully and match the output example given at the end! Question...
C++ Please read the question carefully and match the output example given at the end! Question In this project you will implement operator overloading for a two dimensional Vector class. Begin with the declaration of the Vector class on the final page. There are three operators that must be overloaded insertion (​<<​), addition (​+​), and subtraction (​-​). insertion (​<<​): The insertion operator is used to send the values of a vector to an ostream object, if a vector has two...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT...
****NEED CODED IN C++, READ THE INSTRUCTIONS CAREFULLY AND PAY ATTENTION TO THE INPUT FILE, IT IS REQUIRED FOR USE IN THE PROBLEM**** You are to generate a list of customers to serve based on the customer’s priority, i.e. create a priority queue/list for a local company. The company has been receiving request and the request are recorded in a file, in the order the request was made. The company processes each user based on their priority, the highest priority...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
C++ Read first a user's given name followed by the user's age from standard input. Then...
C++ Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata (which you must declare) to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do. Declare any variables that you need.
Please carefully review the code to fill in after the given instructions and complete the code...
Please carefully review the code to fill in after the given instructions and complete the code for me. Thank you in advance. In this problem, we will implement a simple dictionary of common words in the English language, represented as an array of words paired with their lengths. You will need to implement each of the below methods in the Dictionary class. In this problem, the first line of input represents the method to call. It will be one of...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully...
Please read the instructions and  find attached for the first wiki . Instructions for students: Read carefully the attached document and then post your comments bearing in mind the following questions: 1- What are the pros and cons of rent controls? 2- Why economists disagree on the usefulness of rent control? 3- Do you believe rent control can help the poor? Edit Wiki Content rent control Rent regulation can take various forms, including rent control (the placing of a cap on...
Write code to read a list of song names and durations from input. Input first receives...
Write code to read a list of song names and durations from input. Input first receives a song name, then the duration of that song. Input example: Time 424 Money 383 quit. #include <iostream> #include <string> #include <vector> using namespace std; class Song { public: void SetNameAndDuration(string songName, int songDuration) { name = songName; duration = songDuration; } void PrintSong() const { cout << name << " - " << duration << endl; } string GetName() const { return name;...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give...
INPUT FILE INTO ARRAY. CHECKING FOR COMMAS AND SUCH. PLEASE READ CAREFULLY. alot of people give me either partial answers, or incorrect skeleton. PLEASE YOU CAN'T CHANGE WHAT IS THERE, YOU CAN ONLY ADD. void readFile(Candidate candidates[]) – reads the elections.txt file, fills the candidates[] array. Hint: use substr() and find() functions. Set Score to 0. void List(Candidate candidates[]) – prints the array of Candidate structs. One candidate per one line, include all fields. Use setw() to display nice looking...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT