Question

Instructions C++ programming Your task is to write a class called Data, stored in a file...

Instructions

C++ programming

Your task is to write a class called Data, stored in a file named Data.h. Your class should be able to store a collection (vector) of integers. In addition to the appropriate constructors, your class should also have the following methods.

void add (int number); Adds a number to the data set.

void print (); Prints out the entire data set on a single line, separated by space.

void sort (); Sorts the data set in ascending order. You may implement any sorting algorithm here, for example, max sort, bubble sort, insertion sort, merge sort, quick sort, etc... There is no need to try to implement the most efficient one, any one will do. You can look at http://en.wikipedia.org/wiki/Bubble_sort for implementation details of bubble sort.

Make sure your class works as expected with the file intSort.cpp.

File do not modify only for test.

#include <iostream>
#include "Data.h"

int main(int argc, const char * argv[])
{
    Data myData;
    
    myData.add(4);
    myData.add(5);
    myData.add(2);
    myData.add(3);
    myData.add(1);
    
    myData.print();
    
    myData.sort();
    
    myData.print();
    
    return 0;
}

the result output :

4 5 2 3 1
1 2 3 4 5

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

Please give thumbs up, If it is helpful for you. Thankyou!!

#include <iostream>
#include <vector>

class Data{
private:
       std::vector<int> arr;

public:
       void add(int num){
           arr.push_back(num);
       
       }

       void sort(){
               int j, temp;   
               for (int i = 0; i < arr.size(); i++){
               j = i;   
               while (j > 0 && arr[j] < arr[j-1]){
               temp = arr[j];
               arr[j] = arr[j-1];
               arr[j-1] = temp;
               j--;
               }
               }
       }
  
       void print(){
          
           for(int i=0;i<arr.size();i++)
              std::cout<<arr[i]<<" ";
           std::cout<<'\n';
       }
};

Code and output:

Terminal 4)6:00 PM Open ▼ Save Data.h intsort.cpp Untitled Document 1 1 #include <iostream> ! #include <vector» 4 class Data

Add a comment
Know the answer?
Add Answer to:
Instructions C++ programming Your task is to write a class called Data, stored in a file...
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
  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort...

    Data Structure using C++ only Write 4 different sorting functions: Selection Sort, Insertion Sort, Merge Sort and Quick sort. For each sort you may store the numbers in an array or a linked list (this may be different for each sort). Write your program so that it accepts arguments from the command line using argc and argv in the main function call.

  • Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will...

    Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will need to name . class file A4MA5331550 also when done put both files in a zipped folder named A4MA5331550 and email the folder to [email protected] here is the program: import java.util.Scanner; /** * 09/17/2017 * Dakota Mammedaty * MA5331550 * Bubble sorted */ public class MA5331550 { public static void main(String[] args) throws IOException { Sort st =new Sort(); st.getData(); System.out.println("=================Sorting Algorithms================="); System.out.println("1. Bubble...

  • For C++ Write a main program that inputs a list of 20 numbers from a text file and then calls 4 ...

    For C++ Write a main program that inputs a list of 20 numbers from a text file and then calls 4 sort functions. Create a Sort Lab design document that include flowcharts of the main program and four sort functions Sort Functions:   Insertion, Selection, Bubble and Merge In each Sort function: count the number of compares and swaps needed to sort the numbers and display that information in the sort function. Run the program for four sets of data --...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • The code is in python. The file does not have to be the data set from...

    The code is in python. The file does not have to be the data set from HW2. What I need is a code combing selection sort and merge sort. The 2-1 is what the problem in refers to as Q1. d) [10 points] Write a code combining both selection sort and mergesort algorithms (NOT insertion and merge sorts) as described in Q1) to find out the most optimal k minimizing time efficiency of your algorithms using 1 M data set...

  • Visual C# Homework 2 You are to write a program which will create a class called...

    Visual C# Homework 2 You are to write a program which will create a class called Student Each student has a name age height and weight. These variables should be declared as private. Create the correct constructors and functions. In the main, you will create 5 students. Input the data for the 5 students from a file which already has information in it. Name the file “Information.txt”. After setting up all the data, it is time to sort based on...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

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