943,707 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6913
  • C++ RSS
Jul 24th, 2004
0

Dynamic memory allocation homework

Expand Post »
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�.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jehdo is offline Offline
1 posts
since Jul 2004
Jul 24th, 2004
0

Re: Dynamic memory allocation homework

Sorry, we are not your homework doers.

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

C++ Syntax (Toggle Plain Text)
  1. int *n=NULL;
  2.  
  3. n = new int [10];
  4.  
  5. if(n==NULL)cout<<"Error:Not enough Memory";
  6.  
  7. 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).
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jul 24th, 2004
0

Re: Dynamic memory allocation homework

Doesn't new throw an execption instead of returning NULL unless you explicitly use 'new(std::nothrow)'?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 1st, 2010
0
Re: Dynamic memory allocation homework
You have collected reviews from four movie reviewers where the reviewers are numbered 0-3. Each reviewer has rated six movies where the movies are numbered 100-105. The ratings range from 1 (terrible) to 5 (excellent).
The reviews are shown in the following table:
100
101
102
103
104
105
0
3
1
5
2
1
5
1
4
2
1
4
2
4
2
3
1
2
4
4
1
3
5
1
4
2
4
2
Write a program that stores this data using a 2D array. Based on this information your program should allow the user to enter ratings for any three movies. The program should then find the reviewer whose ratings most closely match the ratings input by the user. It should then predict the user’s interest in the other movies by outputting the ratings by the reviewer for the movies that were not rated by the user. Use the Cartesian distance as the metric to determine how close the reviewer’s movie ratings are to the ratings input by the user. This technique is a simple version of the nearest neighbor classification algorithm.
For example, if the user inputs a rating of 5 for movie 102, 2 for movie 104, and 5 for movie 105, then the closest match is reviewer 0 with a distance of sqrt((5-5)^2 + (2-1)^2 + (5-5)^2) = 1. The program would then predict a rating of 3 for movie 100, a rating of 1 for movie 101, and a rating of 2 for movie 103.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arhab194 is offline Offline
1 posts
since Oct 2010
Oct 1st, 2010
0
Re: Dynamic memory allocation homework
You obviously didn't read the rules when you signed up.
  1. Don't hijack threads (especially 6-year old ones).
  2. You need to do your own homework.
You'll need nested loops to do this, but I'm not going to demonstrate until you try yourself.
Last edited by Fbody; Oct 1st, 2010 at 12:32 pm.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dictionary
Next Thread in C++ Forum Timeline: access an object's method from another class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC