can someone help with this C/C++ program please. Do as much as you can please. I
ID: 3754876 • Letter: C
Question
can someone help with this C/C++ program please. Do as much as you can please. I can ask the remaining in a second question if I have to .
This project has two major goals:
1. The rst is to implement various sorting and selection algorithms.
2. The second is to run experiments comparing the performance of certain algorithms, to collect and plot data, and to interpret the results.
All dynamic memory allocation must be done yourself, i.e., using either malloc() and free(), or new() and delete(). You may not use any external libraries to implement any part of this project. The use of string functions, e.g., strcat, strcmp, is allowed.
The World Series is the annual championship series of Major League Baseball in North America, contested since 1903 between the American League champion team and the National League champion team. A mountain of statistics is gathered for each Major League Baseball (MLB) game, player, player position, and team. In this project you will write a program that executes a sequence of commands that operate on MLB game statistics over a number of years. All input data is to be read from standard input (stdin). Of course, you will redirect stdin from an input le. Similarly, your output must be written to stdout, which can be redirected to an output le. Do not use le operations to process les!
You may assume the format of the input data is correct.
The format of the input data is as follows:
• The number of years y (integer) of team game statistics in this data set.
• For year y the following is provided:
– The total number of teams t in MLB for year y.
– For each of the t teams the following statistics are provided in a tab separated list:
The team name (string),
the league, either American League (AL) or National League (NL) (string),
the number of games in which a player has appeared (integer),
the number of ocial at bats by a batter (integer),
the number of runs,
the number of times a batter hits the ball and reaches bases safely,
the number of times a batter hits the ball and reaches second base,
the number of times a batter hits the ball and reaches third base,
the number of home runs,
the number of runs that score safely,
the number of walks by a batter (four balls during an at bat),
the number of strikeouts by a batter,
the number of times a player has stolen a base,
the number of times a player has been put out attempting to steal a base,
the average number of hits by a batter dened by hits divided by at bats,
the on base percentage,
the slugging percentage, and
the on base percentage plus slugging.
A structure struct mlb stats in §1.1, and in a le named defns.h, have been provided for your use. Upon reading the value of y, you must dynamically allocate an array of struct annual stats of size y, i.e., to hold statistics for each year. Then you initialize the array with the data for each year that follows. Once the data is read and the structure holding the annual statistics structure is initialized, a sequence of commands to be processed follows. The input continues with:
• The number of commands c (integer).
• c commands following the format given in §1.2. Each of the c commands must be processed as described in §1.2.
For each team, the structure mlb stats contains numerous elds associated with each given team in a given year. The structure annual stats holds team statistics for all teams in a given year. The data for this project is taken from the statistics pages for Teams on the Major League Baseball (MLB) site.
#define TEAM_NAME_LEN 50 // Maximum team name string length
#define LEAGUE_NAME 3 // Maximum league name string length,
struct mlb_stats{
char Team[ TEAM_NAME_LEN ]; // Name of the MLB team
char League[ LEAGUE_NAME ]; // American or National league, AL or NL
int G; // Number of games
int AB; // Number of official at bats by a batter
int R; // The number of times a baserunner safely reaches home plate
int H; // The number of hits
int B2; // The number of times a batter hits the ball and reaches second base
int B3; // The number of times a batter hits the ball and reaches third base
int HR; // The number of home runs
int RBI; // The number of runs that score safely
int BB; // The number of walks by a batter
int SO; // The number of strikeouts by a batter
int SB; // The number of times a player has stolen a base
int CS; // The number of times a player has been put out attempting to steal a base
float AVG; // The average number of hits by a batter
float OBP; // The on base percentage
float SLG; // Slugging percentage
float OPS; // The on base percentage plus slugging };
struct annual_stats{
int year;
int no_teams; // Number of teams in the given year
struct mlb_stats *stats; // Array size of statistics depends on number of teams in the given year };
There are six (6) commands to implement. The syntax of valid commands is:
• isort year field order, use the Insertion sort algorithm to sort team data for the given year on the given field in the given order. The output is a list of team name, and eld name, with appropriate headers on the list.
– If the year given is not one of the years that is part of the input data, then output the error message: Error: no such year.
– The field may be any eld of struct mlb stats. This structure contains elds of strings, integers, and oating point numbers. Hence the Insertion sort algorithm should be able to sort any of these types.
– The order may be either incr for increasing order, and decr for decreasing order. If there are multiple elds with the same eld value, then they should be sorted in alphabetic order by team name.
• msort year field order, use the Mergesort algorithm to sort team data for the given year on the given field in the given order. Your Mergesort algorithm should allocate and deallocate a separate array for the Merge step, for each level of recursion except the base case. See the command isort for a description of the year, field, and order, as well as the format of the output.
• ifind year field select, rst uses Insertion sort to sort the team data for the given year on the given field in increasing order. Then, a selection is made based on select. Valid select for the selection include:
– max, prints the maximum value for the given field in the given year, along with the team name achieving the maximum.
– min, prints the minimum value for the given field in the given year, along with the team name achieving the minimum.
– average, prints the average value for the given field in the given year over all teams. Only an integer or oating point field will be given for this item.
– median, prints the median value for the given field in the given year over all teams. Here, “median” refers to the lower median, i.e., the element at position b(n+1)/2c, where n is the total number of elements.
In all cases, if there is more than one team satisfying the selection criteria, output all of them. Similarly, if there are no teams satisfying the selection criteria output the message to that eect.
• mfind year field item, rst uses Mergesort to sort the team data for the given year on the given field in increasing order. See the command ifind for a description of the year, field, and item, as well as the format of the output.
• isort range start-year end-year field order, use the Insertion sort algorithm to sort team data for the range of years starting with start-year and ending with end-year, on the given field in the given order. There are two dierences between this command and isort for a single year.
– The sort is to be performed using data for all years in the given range. You may assume that start-year < end-year and that all years exist in the input data provided.
– Add the year as part of the output for clarity.
• msort range start-year end-year field order, use the Mergesort algorithm to sort team data for the range of years starting with start-year and ending with end-year, on the given field in the given order. See the command isort on a range of years for a description of the start-year, end-year, field, and order, as well as the format of the output.
Consider the following valid example command sequence. Comments will not be part of the input, and are only included here for clarication.
6 // A total of 6 commands follow
isort 2018 RBI decr // Insertion sort 2018 data on RBI in decreasing order
msort 2018 HR decr // Quicksort 2015 data on home runs in decreasing order
ifind 2018 AB max // Return the team(s) with maximum AB after Insertion sorting 2018 data
ifind 2018 SLG average // Return team(s) with the average SLG after Insertion sorting 2018 data
mfind 2018 SO min // Return team(s) with fewest SO after Mergesorting 2018 data
msort range 2014 2018 BB incr // Mergesort 2014-2018 data on BB in increasing order
PROGRAM MUST BE WRITEN IN C/C++
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
long len = 1000;
const long max_Len = 300000;
int list[max_Len];
void read()
{
ifstream fin("random.dat", ios::binary);
for (long x = 0; x < len; x++)
{
fin.read((char*)&list[x], sizeof(int));
}
fin.close();
}
void bubbleSort()
{
int tmp;
for(long x = 0; x < len; x++)
{
for(long j = 0; j < len-x-1; j++)
{
if (list[j] > list[j+1])
{
tmp = list[j];
list[j] = list[j+1];
list[j+1] = tmp;
}
}
}
}
void insertionSort()
{
int tmp;
for(long x = 1; x < len; x++)
{
tmp = list[x];
long j;
for(j = x-1; j >= 0 && list[j] > tmp; j--)
{
list[j+1] = list[j];
}
list[j+1] = tmp;
}
}
long partition(long left, long right)
{
int pivot_element = list[left];
int lb = left, ub = right;
int tmp;
while (left < right)
{
while(list[left] <= pivot_element)
left++;
while(list[right] > pivot_element)
right--;
if (left < right)
{
tmp = list[left];
list[left] = list[right];
list[right] = tmp;
}
}
list[lb] = list[right];
list[right] = pivot_element;
return right;
}
void quickSort(long left, long right)
{
if (left < right)
{
long pivot = partition(left, right);
quickSort(left, pivot-1);
quickSort(pivot+1, right);
}
}
int main()
{
double t1, t2;
for (len = 1000; len <= max_Len; )
{
cout << " Length : " << len << ' ';
read();
t1 = clock();
bubbleSort();
t2 = clock();
cout << "Bubble Sort : " << (t2 - t1)/CLK_TCK << " sec ";
read();
t1 = clock();
insertionSort();
t2 = clock();
cout << "Insertion Sort : " << (t2 - t1)/CLK_TCK << " sec ";
read();
t1 = clock();
quickSort(0, len - 1);
t2 = clock();
cout << "Quick Sort : " << (t2 - t1)/CLK_TCK << " sec ";
switch (len)
{
case 1000 :
len = 5000;
break;
case 5000 :
len = 10000;
break;
case 10000 :
len = 50000;
break;
case 50000 :
len = 100000;
break;
case 100000 :
len = 200000;
break;
case 200000 :
len = 300000;
break;
case 300000 :
len = 300001;
break;
}
}
return 0;
}
I would finish up this, that
Air pocket Sort isn't reasonable in any condition. It is an O(n2) calculation with a vast steady. In basic words, time required to perform bubble sort on 'n' numbers increments as square of 'n'. In this way it is very moderate.
Insertion Sort is reasonable for little documents, however again it is an O(n2) calculation, yet with a little consistent. Additionally take note of that it works best when the document(/numbers) is as of now relatively arranged.
Quick Sort astounded me. Didn't you get astonished?! Well it is an O(n*log(n)) calculation on a normal case and an O(n2) calculation in the most dire outcome imaginable. (Snappy sort's most pessimistic scenario happens when the numbers are as of now arranged!!) The diagram talks everything. You require this calculation when the rundown is expansive and time is premium.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.