Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
There are the 2 files header(.h file) and a .cpp file. We have to execute some command for testing this. Here are the commands :



• Create  list of integers

• Insert four items 5 7 6 9

• Print the list (expected 5 7 6 9)

• Print the length of the list (expected 4)

• Insert one item 1

• Print the list (expected 5 7 6 9 1)

• Retrieve 4 and print whether found or not (expected Item is not found)

• Retrieve 5 and print whether found or not (expected Item is found)

• Retrieve 9 and print whether found or not (expected Item is found)

• Retrieve 10 and print whether found or not (expected Item is not found)

• Print if the list is full or not (expected List is full)

• Delete 5

• Print if the list is full or not (expected List is not full)

• Delete 1

• Print the list (expected 7 6 9)

• Delete 6

• Print the list  (expected 7 9)

• Write  class studentInfo that represents a student

record. It must have variables to store the student ID,

student's name and student's CGPA. It also must have a

function to print all the values. You will also need to

overload a few operators.

• Create list of objects of class studentInfo.

• Insert 5 student records :

15234 Jon 2.6

13732 Tyrion 3.9

13569 Sandor 1.2

15467 Ramsey 2 3.1

16285 Arya 3.1

• Delete the record with ID 15467

• Retrieve the record with ID 13569 and print whether found or not along with the entire record (expected Item is found 13569, Sandor, 1.2)

• Print the list:

(expected

 5234, Jon, 2..6

13732, Tyrion, 3.9

13569, Sandor, 1.2

16285, Arya, 3.1 )


What I have tried:

C++
//unsortedtype.h
#ifndef UNSORTEDTYPE_H_INCLUDED
#define UNSORTEDTYPE_H_INCLUDED
const int MAX_ITEMS = 5;
template <class ItemType>
class UnsortedType
{
public :
UnsortedType();
void MakeEmpty();
bool IsFull();
int LengthIs();
void InsertItem(ItemType);
void DeleteItem(ItemType);
void RetrieveItem(ItemType&, bool&);
void ResetList();
void GetNextItem(ItemType&);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#endif // UNSORTEDTYPE_H_INCLUDED


C++
//unsortedtype.cpp
#include "UnsortedType.h"
template <class ItemType>
UnsortedType<ItemType>::UnsortedType()
{
length = 0;
currentPos = -1;
}
template <class ItemType>
void UnsortedType<ItemType>::MakeEmpty()
{
length = 0;
}
template <class ItemType>
bool UnsortedType<ItemType>::IsFull()
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int UnsortedType<ItemType>::LengthIs()
{
return length;
}
template <class ItemType>
void UnsortedType<ItemType>::ResetList()
{
currentPos = -1;
}
template <class ItemType>
void
UnsortedType<ItemType>::GetNextItem(ItemType&
item)
{
currentPos++;
item = info [currentPos] ;
}
<pre>template <class ItemType>
void
UnsortedType<ItemType>::RetrieveItem(ItemType&
item, bool &found)
{
int location = 0;
bool moreToSearch = (location < length);
found = false;
while (moreToSearch && !found)
{
if(item == info[location])
{
found = true;
item = info[location];
}
else
{
location++;
moreToSearch = (location < length);
}
}
}
template <class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType
item)
{
info[length] = item;
length++;
}
template <class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType
item)
{
int location = 0;
while (item != info[location])
location++;
info[location] = info[length - 1];
length--;
}
Posted
Updated 14-Aug-20 12:23pm
v9
Comments
OriginalGriff 13-Aug-20 7:03am    
And?
What have you tried?
Where are you stuck?
What help do you need?
[no name] 13-Aug-20 7:06am    
I have built the header and the .cpp file. Now I am stuck with the driver .cpp file . How do I execute all the tasks in them.
Richard MacCutchan 13-Aug-20 8:03am    
You have created a header file and lots of templates and now you are stuck. A better way is to start from the beginning and do each task one at a time: add any required code to the header, then to the implementation (.cpp). Test the code to make sure it works. Go to the next step and repeat the process, each time testing all the code that you have written. That way, when you hit a problem you can be reasonably sure which part of the code it is in.
KarstenK 14-Aug-20 5:12am    
Nice, now start implementing your tasks in some function. tip: use functions to give your code some structure.

Quote:
I have built the header and the .cpp file. Now I am stuck with the driver .cpp file . How do I execute all the tasks in them.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
You may learn from some tutorial like Learn C++ to solve your homework. You should inform yourself whether you can use classes like vector to make your work easier. For your student you should create an own class with a constructor.

Else you must solve each task carefully. Use a IDE like Visual Studio to have a good debugger.
 
Share this answer
 
Comments
Patrice T 14-Aug-20 5:39am    
+5 to counter the downvote :)
I see two tasks. One is for integers and another is for the class. Given that you tagged it as C++11 both problems can be solved in approximately ten lines of code for both cases
 
Share this answer
 
Comments
Patrice T 14-Aug-20 20:10pm    
Are you sure saying it is easy is a valid solution ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900