how to write this c++ program?(please use my skeleton)
In this program, you should first load the students' records from the input. For a specific student, the input data fields include (please see the Sample Input):
Name: the string stores the name of the student
Mark: the integer stores the mid-term mark of the student
Flag: when input is 1, the flag will be assigned as true, which means this student would like to show his/her name. Then this student's name will be shown in the output. If the intput is 0, the flag will be assigned as false. The string "******" is displayed instead
Following the above input data, in the last line of the input, there are two variables that control the sorting order(ascending or descending) and sorting keyword ( by mark or by name): the first variable controls the sorting order ('a' or 'A' means ascending-order and 'd' or 'D' means descending-order). The second variable controls the sorting keyword(0 means sort-by-mark and 1 means sort-by-name).
Then according to the input, you should sort the students' records in ascending or descending order, by mark or by name. Finally, you are required to print the sorted information to the screen. For a specific student, the output data fields include (please see the Sample Output):
Rank
Name
Mark
The input data are fed by input files. In the file, each line represents the record of a specific student. The data fields are seperated by tabs. Each file contains NUMBER(20) records. You can assume that all input test cases are valid. Your program will load the records from input file, sort them, and then display them. (See the sample input and output for examples.)
The underlying data structure of the database is several 1D arrays. The marks[i], names[i] and flags[i] store the record of the same student i
int marks[NUMBER]: it stores the marks of all students
string names[NUMBER]: it stores the names of all students
bool flags[NUMBER]: it stores display flags of all students
The basic structures for the data are all defined in the program skeleton. Within this lab, you are required to use the predefined data structure and variables without any change.
Sample Input 1
peng 50 0
lei 100 1
hongwei 100 1
tian 100 1
stu001 -321 1
a 1
In the last line, 'a' or 'A' means ascending-order and 1 means sort-by-name. We get the following output:
Rank Name Mark
1 hongwei 100
2 lei 100
3 ****** 50
4 stu001 -321
5 tian 100
Sample Input 2
peng 50 0
lei 100 1
hongwei 100 1
tian 100 1
In the last line, 'd' or 'D' means descending-order and 0 means sort-by-mark. We get the following output:
Rank Name Mark
1 lei 100
2 hongwei 100
3 tian 100
4 ****** 50
5 stu001 -321
stu001 -321 1
d 0
the skeleton
#include <iostream>
#include <string>
#define NUMBER 20
using namespace std;
/*TODO: please fill the return type*/ findPosition(int marks[], int start, int end, bool order)
{
// TODO: if start > end, exit;
int pos = start;
// TODO: find the mininum(ascending) or maximum(descending) data's position in the array
// between the start position and the end position
// Important: the function have to allow the user to choose the
// sort-order by using the parameter: bool order
return pos;
}
// TODO: implement the other overloaded function of findPosition here,
// which takes the string array as parameter instead of integer array
void swap(int& val0, int& val1)
{
// TODO: swap the two variables: val0 and val1
}
// TODO: implement the second overloaded function of findPosition for swaping boolean variables
// TODO: implement the third overloaded function of findPosition for swaping string variables
// Please deduce the default arguments and their order according to
// the given function calls in main function.
// There are six arguments, and the last two of them require
// default arguments.
// Please set the default values as mentioned in the webpage description:
// by default, the data will be sorted by name (value: 1)
// and in ascending order (value: true).
void selectionSort(/* TODO: please fill the arguments here, one of them should be "rankBy" and controls the sorting keyword*/)
{
for(int i=0; i<num-1; i ++)
{
int pos;
switch(rankBy)
{
// TODO: please take both sorting keyword types (by name or by mark) into consideration by calling the function findPosition
// and find the minimum(ascending) or maximum(descending) data's position according the sorting order
}
// TODO: swap the students' records by calling the three overloaded functions: swap
}
}
void getInput(int marks[], string names[], bool flags[], int num)
{0
for(int i=0; i<num; i ++)
cin>> names[i] >> values[i] >> flags[i];
}
void printResult(/* TODO: fill the parameters here*/)
{
// TODO: print the result according to the format in the example
}
int main()
{
int marks[NUMBER];
string names[NUMBER];
bool flags[NUMBER];
// get the input data
getInput(marks, names, flags, NUMBER);
// choose sorting order
bool sorting_order;
char temp_chr;
// set sorting_order to true if the user choose ascending order
// set sorting_order to false if the user choose descending order
cin >> temp_chr;
if('a' == temp_chr || 'A' == temp_chr) sorting_order = true;
else sorting_order = false;
// choose sorting method
// other numbers mean sort the data by name
int sortBy;
cin >> sortBy;
// sort the data according to the parameters
// please pay attention to the default arguments of selectionSort and
// deduce the their values according to the following code
// 1 means sort by name, 0 means sort by mark
// true means ascending order, false means descending order
if(1 == sortBy)
{
if(sorting_order) selectionSort(marks, names, flags, NUMBER);
else selectionSort(marks, names, flags, NUMBER, 1, false);
}
else
{
if(sorting_order) selectionSort(marks, names, flags, NUMBER, 0);
else selectionSort(marks, names, flags, NUMBER, 0, false);
}
// output the result
printResult(marks, names, flags, NUMBER);
}