DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Dynamic memory allocation homework (http://www.daniweb.com/forums/thread8371.html)

jehdo Jul 24th, 2004 4:20 am
Dynamic memory allocation homework
 
Objective
This assignment has been designed so that you understand the concept of using dynamic memory allocation in C++. After the completion of this assignment you should have a good grasp on:

o Array of Objects
o Use of new Operator for dynamic memory allocation in C++, for user-defined types.
o Use of delete Operator in C++, to free the memory allocated.
o Making your own header file and use it in your other program.
o Use of stream manipulator(s)

Assignment

Write a C++ program that tells the teams qualified for a tournament’s semi finals.
Your program should ask the user about the number of teams included in the tournament. After taking input from the user your program should dynamically allocate the memory for the number of teams.

Take the input of number of matches won and loss during previously played rounds by the teams.





The points will be granted to each team according to the match status as follows:

Match status Points
Won 4
Loss 0
Draw 2


A Team won a match will be granted 4 points, for draw 2 points will be given to each Team whereas no point is granted if a Team losses a match.

Your program should include a Class Team with the following Private data members.

o team_id

o noOfTeams

o country //of which country the team belongs to

o matches_played

o won

o loss

o point


Where the noOfTeams should be static and will be incremented on the creation of each object. The incremented value of noOfTeams should be assigned to team_id in the constructor’s definition. This is how it generates and assigns the team_id for each new Team.
Class Team should have the following public member function.

1. points()
This function will calculate the points of each Team after taking input from the user about the number of matches_played, won and loss during the tournament.

Your program should compare points attained by teams with each other and hence find out the top four teams and display them as a qualifying team for the semi finals.

Write the Setter functions to set or assign values to country, matches_played, won, loss and point of the Class Team.

Write the Getter functions to get value of country, matches_played, won, loss, and point of the Class Team.

Write constructor of the Class Team to initialize the data members.

Also write destructor for the Class Team.

Define the Class Team in a separate header file and include it into your main program.


Use stream manipulator, setw(5), to format your output.


How to create and include header file?


Define your class named Team in one file and save this file with a .h extension and you include this file as a Header File in your .cpp file


How you can create your own header file:

Create a folder with a name as “Assignment8�

Open your Dev c++ and write this program


EXAMPLE FOR CREATING YOUR OWN HEADER FILE:

Class Team
{
private:
//data members

public:

//member functions
};

o After writing this program save this file in a folder “Assignment8� as myHeaderFile.h, You can also compile the program to check errors..


Suppose if you create the folder “Assignment8� in Drive D:

The path will be “D:\Assignment8\myHeaderFile.h�.

FireNet Jul 24th, 2004 12:40 pm
Re: Dynamic memory allocation homework
 
Sorry, we are not your homework doers.

Anyway new and delete are tech info so here is an example

int *n=NULL;

n = new int [10];

if(n==NULL)cout<<"Error:Not enough Memory";

delete[] n;

If you use a new statement then you should always delete it else your comp will soon run out of ram. Brrrr (Memory leaks).

Dave Sinkula Jul 24th, 2004 8:14 pm
Re: Dynamic memory allocation homework
 
Doesn't new throw an execption instead of returning NULL unless you explicitly use 'new(std::nothrow)'?


All times are GMT -4. The time now is 10:12 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC