Question

How to solve this Problem in C++

. The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set

Sample Output: X Microsoft Visual Studio Debug Console Example 1: Set of integers--- Adding: 10 Adding: 5 Adding: 15 Adding:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//set.h

#include<iostream>
using namespace std;

template<typename T>
class set{
   private:
       T *arr;
       int size;
      
   public:
       set(int capacity){
           arr = new T[capacity];
           size = 0;
       }
       ~set(){
           delete [] arr;
           size = 0;
       }
      
       void add(T data){
           cout<<" Adding : "<<data<<endl;
           int i;
           for(i=0;i<size;i++){
               if(arr[i] == data) break;
           }
           if(i==size)arr[size++] = data;
       }
      
       int getCount(){
           return size;
       }
      
       T* getArray(){
           return arr;
       }
};

//main.cpp

#include "set.h"

int main(){
   set<int> set1(10);
   set<double> set2(10);
   set<string> set3(10);
  
   cout<<"--------------------------------\n";
   cout<<"---Example 1: Set of integers---\n";
   cout<<"--------------------------------\n";
   set1.add(10);
   set1.add(5);
   set1.add(15);
   set1.add(25);
   set1.add(15);
   cout<<"--------------------------------\n\n";
   cout<<"The first set has "<<set1.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set1.getCount();i++){
       cout<<"\t"<<set1.getArray()[i]<<"\n";
   }
  
   cout<<"\n--------------------------------\n";
   cout<<"---Example 2: Set of doubles---\n";
   cout<<"--------------------------------\n";
   set2.add(1.5);
   set2.add(5.6);
   set2.add(12.8);
   set2.add(1.5);
   set2.add(12.8);
   cout<<"--------------------------------\n\n";
   cout<<"The second set has "<<set2.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set2.getCount();i++){
       cout<<"\t"<<set2.getArray()[i]<<"\n";
   }
  
   cout<<"\n--------------------------------\n";
   cout<<"---Example 3: Set of strings---\n";
   cout<<"--------------------------------\n";
   set3.add("John Smith");
   set3.add("Jane doe");
   set3.add("John Smith");
   set3.add("Jack black");
   cout<<"--------------------------------\n\n";
   cout<<"The third set has "<<set3.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set3.getCount();i++){
       cout<<"\t"<<set3.getArray()[i]<<"\n";
   }
  
   return 0;
}

//sample output

--Example 1: Set of integers- Adding : 10 Adding : 5 Adding : 15 Adding : 25 Adding : 15 The first set has 4 items The are: 1

Add a comment
Know the answer?
Add Answer to:
How to solve this Problem in C++ . The Problem Write program that uses a class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • what would be the solution code to this problem in c++? The Problem Write program that...

    what would be the solution code to this problem in c++? The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class...

    (C++ exercise) 5. Write a new Checkbook class that uses a dynamic array. Call the class a different name, since it will be different from the latest version that uses a static array. Start off with a dynamic array size of 2, made by the constructor. Every time the dynamic array becomes full and a new check has to be written, double the current size of the array. Write a doubleArray function for the class to do this, but place...

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...

  • Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the...

    Write C++ program (studentsGpa.cpp) uses dynamic allocation to create an array of strings. It asks the user to enter a number and based on the entered number it allocates the array size. Then based on that number it asks the user that many times to enter student’s names. What you need to do:  Add another array of doubles to store the gpa of each student as you enter them  You need to display both the student’s name and...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT