Question

Write the following functions: 1.Write a function, to be included in a template sorted/unsorted list class,...

Write the following functions:

1.Write a function, to be included in a template sorted/unsorted list class, called smaller, that will receive an xtype parameter. The function will return how many items in the list are smaller than the parameter.

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

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

#define MAX_ELEMENTS 5

template<class smaller>

string SortedList(smaller data[MAX_ELEMENTS])

{

// Sort the list

for(int count1=0; count1 < MAX_ELEMENTS;count1++)

{

for(int count2=0; count2 < MAX_ELEMENTS; count2++)

{

if(data[count2] > data[count2+1])

{

smaller temp=data[count2+1];

data[count2+1]=data[count2];

data[count2]=temp;

return temp;

}

}

}

}

int main()

{

string data[5];

string temp;

ifstream infile;

infile.open("data.txt");

if(!infile)

cout<<"Your file was not opened correctly";

else

while(!infile.eof())

{

for(int i=0; i < MAX_ELEMENTS && !infile.eof(); i++)

{

infile>> data[i];

}

cout<<"The initial list is ";

for(int i=0; i < MAX_ELEMENTS; i++)

{

cout<<data[i]<<" ";

}

cout<<endl;

cout<<"The sorted list is ";

cout<<SortedList(data);

infile.close();

system("pause");

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write the following functions: 1.Write a function, to be included in a template sorted/unsorted list 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
  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • with python Write a function called linear_search which consumes a sorted list of integers and a...

    with python Write a function called linear_search which consumes a sorted list of integers and a number and linearly searches for the number in the list. Your function should return how many comparisons were made in order to find the element in the list. If the element is not in the list, your linear search should return -1 For example:

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...

    MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as defined by the following skeleton: #define MAX_ITEMS 10 typedef char ItemType; class UnsortedList {        private:             int length; ItemType values[MAX_ITEMS]; int currentPos;        public:             SortedList( ); // default constructor: lenght=0, currentPos=-1             void MakeEmpty;    // let length=0             void InsertItem(ItemType x);   // insert x into the list                 void DeleteItem(ItemType x); // delete x from the list bool IsFull( );   // test...

  • Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A...

    Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of size n. For example, given the array |3, 2, 4, 3], the function should return the value 3. For the array 11 2,3 5,5,5,66,81, the function should return an array (or list) 5,6 In this task, you will develop two alternative solutions for the FINDDUPLICATES(Ao..n 1 func- tion. In your implementations, you may call any of the algorithms introduced in...

  • In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of...

    In PYTHON! Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...

  • Define a function named insert that takes an integer atom and a sorted list as parameters....

    Define a function named insert that takes an integer atom and a sorted list as parameters. The function should return a list with the integer in the correct position in the sorted list. For example: insert [3, (2 4 6 8)] = (2 3 4 6 8) insert 9, ()] = (9) insert[3, (2 3 4 5)] = (23 3 4 5) Note that in the case of that last example, it does not matter which 3 comes first in...

  • Python program - Write a Python program, in a file called sortList.py, which, given a list...

    Python program - Write a Python program, in a file called sortList.py, which, given a list of names, sorts the names into alphabetical order. Use a one dimensional array to hold the list of names. To do the sorting use a simple sorting algorithm that repeatedly takes an element from the unsorted list and puts it in alphabetical order within the same list. Initially the entire list is unsorted. As each element is placed in alphabetical order, the elements in...

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

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