Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a c++ program that records and displays league standings for a baseball le

ID: 3605853 • Letter: W

Question

Write a c++ program that records and displays league standings for a baseball league.

THE DATA MUST BE STORED IN A SINGLE ARRAY -- A DYNAMICALLY ALLOCATED ARRAY OF WINRECORD STRUCTS.

*************************DO NOT USE "stdio.h file" **********************************

*************PLEASE USE ONLY #inlcude <iostream> and #include <cstring> as a header file *******************

The program will ask the user to enter five team names, and five win amounts. It will store the data in memory, and print it back out sorted by wins from highest to lowest.

The winrecord struct must have two fields: an int named wins, and a char* named name. name will point to a dynamically allocated array of characters, see requirement 4 below.

the data must be stored in a single array -- a dynamically allocated array of WinRecord structs. You must ask the user how many teams are in the league to determine how big this array needs to be and then dynamically allocate memory for it using new. It must deallocate the memory when it is done with the array using delete [].

Your program must use three functions that accept the array of WinRecord structs by address (i.e., pass a WinRecord pointer):

void initializeData(WinRecord* standings, int size)

void sortData(WinRecord* standings, int size)

void displayData(WinRecord* standings, int size)

Note that the name field of each WinRecord struct is just a char* which you need to use to store a C-String. you must use C-strings, not C++ string objects. Unlike a C++ string object, the memory to store the actual character array for the C-String is not allocated for you automatically! but I have provided the getLine() function getLine.cpp to use if you wish. Note that this function returns a line of text from the keyboard contained in a dynamically allocated array. You will thus need to deallocate this memory using delete [] when you are done using any arrays you allocated using this function. Note that this is in addition to de-allocating the array of WinRecord structs discussed in step 2 above!

The sample output from your program should look approximately like this (user input in orange):

Enter team #1: Padres

Enter the wins for team #1: 75

Enter team #2: Dodgers

Enter the wins for team #2: 91

Enter team #3: Giants

Enter the wins for team #3: 92

Enter team #4: Rockies

Enter the wins for team #4: 65

Enter team #5: Diamondbacks

Enter the wins for team #5: 70

League Standings:

Giants: 92

Dodgers: 91

Padres: 75

Diamondbacks: 70

Rockies: 65

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

//define number

struct rec

{

int wins;

char *name;

};

typedef struct rec WinRecord;

void initializeData(WinRecord* standings, int size);

void sortData(WinRecord* standings, int size);

void displayData(WinRecord* standings, int size);

int main()

{

WinRecord *standings;

int n,i;

char name[20];

printf(" How many teams are in the league ? ");

scanf("%d", &n);

//allocate memory for array of structs

standings = (WinRecord*)malloc(sizeof(WinRecord) * n);

initializeData(standings, n);

sortData(standings, n);

displayData(standings, n);

}

void initializeData(WinRecord* standings, int size)

{

char name[20];

int i;

//now allocate memory for name in each struct

//now fill the struct with info entered by user

for (i = 0; i < size; i++)

{

printf("Enter team #%d: ", i+1);

scanf("%s", name);

//now allocate memory for name in the struct

standings[i].name = (char*)malloc(strlen(name) + 1);

strcpy(standings[i].name, name);

printf("Enter the wins for team #%d: ", i+1);

scanf("%d", &standings[i].wins);

}

}

void sortData(WinRecord* standings, int size)

{

WinRecord tmp;

int c, d;

for (c = 0; c < (size - 1); c++)

{

for (d = 0; d < size - c - 1; d++)

{

if (standings[d].wins < standings[d+1].wins) /* For decreasing order use < */

{

tmp.wins = standings[d].wins;

tmp.name = (char*)malloc(strlen(standings[d].name) + 1);

strcpy(tmp.name, standings[d].name);

standings[d].wins = standings[d+1].wins;

strcpy(standings[d].name, standings[d + 1].name);

standings[d+1].wins = tmp.wins;

strcpy(standings[d+1].name, tmp.name);

}

}

}

}

void displayData(WinRecord* standings, int size)

{

int i;

printf("League Standings: ");

for (i = 0; i < size; i++)

{

printf("%s :%d ", standings[i].name, standings[i].wins);

}

}

-------------------------------------------------------

//output

How many teams are in the league ? 5
Enter team #1: Padres
Enter the wins for team #1: 75
Enter team #2: Dodgers
Enter the wins for team #2: 91
Enter team #3: Giants
Enter the wins for team #3: 92
Enter team #4: Rockies
Enter the wins for team #4: 65
Enter team #5: Diamondbacks
Enter the wins for team #5: 70
League Standings:
Giants :92
Dodgers :91
Padres :75
Diamondbacks :70
Rockies :65

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote