I really need help for this C assignment Rewrite your league

I really need help for this C++ assignment.

Rewrite your league application from Assignment 6 so that each team/wins pair is stored in a struct named winRecord. Your program must meet the following requirements:

1. 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.

2. Instead of using two parallel arrays, 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 [].

3. 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)

4. Note that the name field of each winRecord struct is just a char* which you need to use to store a C-String. For this assignment, 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! I encourage you to develop a function to do this on your own, 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!

5. As usual, you must provide sample output demonstrating your program works correctly.

Solution

#include <iostream>
#include<cstring>
using namespace std;
// read in a line of text and return it in a dynamically allocated array
char* getLine()
{
const int BUFFER_SIZE = 1000;

// Allocate a buffer local to this function
char buffer[BUFFER_SIZE];
// Use cin.getline() to input the buffer from the user
cin.getline(buffer, BUFFER_SIZE);

// Find the length of the string in buffer
int length = strlen(buffer);
// Dynamically allocate an array, to be returned
char *rValue = new char[length + 1];
// Copy the string into the dynamically allocated array
strncpy(rValue, buffer, length);
// Return the address of the dynamically allocated array
   return rValue;

}

//winRecord struct defintiom
struct winRecord
{
    int wins;
    char* name;
};

//function to prompt the user for league data
void initializeData(winRecord* standings, int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<\"Enter wins: \";
        cin>>standings[i].wins;
        cout<<\"Enter name: \";
        cin.ignore();
       standings[i].name=getLine();
    }
}

//funciton to sort the array contents in ascending order
void sortData(winRecord* standings, int size)
{
    winRecord temp;
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size-1;j++)
        {
            if(standings[j].wins>standings[j+1].wins)
            {
                temp=standings[j];
                standings[j]=standings[j+1];
                standings[j+1]=temp;
            }
        }
    }

}

//function to display the array contents
void displayData(winRecord* standings, int size)
{
    cout<<\"\ ******Win Records*****\";
     for(int i=0;i<size;i++)
    {
        cout<<endl;
        cout<<\"Wins: \"<<standings[i].wins<<\"\\t\";
        cout<<\"Name: \"<<standings[i].name;
    }
}


int main()
{
    //prompt for size
    int size;
   cout << \"Enter no of teams in your league: \";
   cin>>size;

   //allocate array of winrecords
   winRecord *winRecords=new winRecord[size];
   //initialize array
   initializeData(winRecords,size);
   //sort array
   sortData(winRecords,size);
   //diaplsy sorted array
   displayData(winRecords,size);

   //delete memory allopcated
   for(int i=0;i<size;i++)
   {
       delete [] winRecords[i].name;
       winRecords[i].name=NULL;
   }
   delete [] winRecords;
   winRecords=NULL;
   return 0;
}

Output:

Enter no of teams in your league:  3                                                                                                                                                                                                                    

Enter wins: 213                                                                                                                                                                                                                                         

Enter name: ABC                                                                                                                                                                                                                                         

Enter wins: 1090                                                                                                                                                                                                                                        

Enter name: DEF                                                                                                                                                                                                                                         

Enter wins: 98                                                                                                                                                                                                                                          

Enter name: PQR                                                                                                                                                                                                                                         

                                                                                                                                                                                                                                                        

******Win Records*****                                                                                                                                                                                                                                  

Wins: 98        Name: PQR                                                                                                                                                                                                                               

Wins: 213       Name: ABC                                                                                                                                                                                                                               

Wins: 1090      Name: DEF

I really need help for this C++ assignment. Rewrite your league application from Assignment 6 so that each team/wins pair is stored in a struct named winRecord.
I really need help for this C++ assignment. Rewrite your league application from Assignment 6 so that each team/wins pair is stored in a struct named winRecord.
I really need help for this C++ assignment. Rewrite your league application from Assignment 6 so that each team/wins pair is stored in a struct named winRecord.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site