Question

In: Computer Science

For c++, please do not recycle other peoples code as they do not satisfy the requirements....

For c++, please do not recycle other peoples code as they do not satisfy the requirements.

Write a program for sorting a list of integers in ascending order using the bubble sort algorithm.

Requirements
Implement the following functions:

  1. Implement a function called readData
    int readData( int *arr)
    arr is a pointer for storing the integers. The function returns the number of integers.
    The function readData reads the list of integers from a file call data.txt into the array arr. The first integer number in the file is the number of intergers. After the first number, the file lists the integers line by line.
  2. void bsort(int *arr, int last)
    arr is a pointer to an array of integers to be sorted. last is the number of elements in the array. The function bsort sorts the list of integers in ascending order.
  3. writeToConsole(int * arr, int last)
    arr is a pointer to an array of integers. last is the number of elements in the array. The function writeToConsole displays the sorted list.
  4. Do not use the array notation in your solution.

Here is the content of the file data.txt.
9
8
4
7
2
9
5
6
1
3

Then reimpelment a function called bubble_sort that has the following prototype.

bubble_sort(int *array, int size, pointer to a function)
Pre condition
array - a pointer to an array of size element.
pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order)
Post condition
Sort the array in ascending or descending based on the the pointer to a function.

Write the main function to perform the following:

  • Dynamic input array of n integer elements
  • Call the function bubble_sort to sort the array in ascending
  • Display the sorting array.
  • Call the function bubble_sort to sort the array in descending
  • Display the sorting array.

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int readData( int *&arr);

void bsort(int *arr, int last);

void writeToConsole(int * arr, int last);

void bubble_sort(int *arr, int n, int (*func)(int, int));

int less_than(int v1, int v2);

int greater_than(int v1, int v2);

int main()

{

    int n, i;

    int *arr;

    n = readData(arr);

    bsort(arr, n);

    writeToConsole(arr, n);

    //method 2

    printf("\n\nAscending order:\n");

    bubble_sort(arr, n, &greater_than);

    for (i = 0; i < n; i++)

        printf("%d ", *(arr+i));

    printf("\n\nDescending order:\n");

    bubble_sort(arr, n, &less_than);

    for (i = 0; i < n; i++)

        printf("%d ", *(arr+i));

    printf("\n");

    delete arr;

    return 0;

}

int readData( int *&arr){

    int n, num, i;

    ifstream in;

    in.open("data.txt");

    in>>n;

    arr = new int[n];

    for(i=0; i<n; i++){

        in>>num;

        *(arr+i) = num;

    }

    in.close();

    return n;

}

void bsort(int *arr, int last){

    int i, j, temp;

    for(i=0; i<last; i++){

        for(j=0; j<last-i-1; j++){

            if(*(arr+j)>*(arr+j+1)){

                temp = *(arr+j);

                *(arr+j) = *(arr+j+1);

                *(arr+j+1) = temp;

            }

        }

    }

}

void writeToConsole(int * arr, int last){

    cout<<*(arr+0);

    for(int i=1; i<last; i++){

        cout<<", "<<*(arr+i);

    }

    cout<<endl;

}

void bubble_sort(int *arr, int n, int (*func)(int v1, int v2))

{

    int i, j, temp;

    for (i = 0; i < n; i++)

    {

        for (j = 0; j < n-i-1; j++)

        {

            if ((*func)(*(arr+j), *(arr+j+1)))

            {

                temp = *(arr+j);

                *(arr+j) = *(arr+j+1);

                *(arr+j+1) = temp;

            }

        }

    }

}

int less_than(int v1, int v2)

{

    return v1 < v2;

}

int greater_than(int v1, int v2)

{

    return v1 > v2;

}


Related Solutions

Please see the java code below and amend it according to these requirements: * Do not...
Please see the java code below and amend it according to these requirements: * Do not remove any of the code within the public interface to the class, only add code to test the additional functionality *Limit items in each instance *Allow the last item entered to be deleted The CashRegister class should be modified to limit the number of items that can be added to an instance of the CashRegister. The limit should be declared as a constant in such a...
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create...
Please dont copy from other answers. Do as simole as possible (C programming). code 1: create a program that will copy the contents of a text file called (input.txt) to a file called (copied.txt). After the program runs the contents of both files should be the same. (im creating the input manually) code 2: Change the code from fist part so that instead of always copying from input.txt to copied.txt it instead asks the user to provide both file names....
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND...
PROVIDE CODE ONLY IN C++ / NO OTHER LANGUAGES PLEASE ADD SELECTION SORT/ INSERTION SORT/ AND BUBBLE SORT FUNCTION TO THIS PROGRAM #include <iostream> #include<vector> #include <algorithm >   #include <chrono>    #include <ctime> using namespace std; void bubblesSort() { // Please create Bubble Sort function// Make another for Selection Sort and  Insertion Sort } int main() { // empty vector vector<int> data; // data [0], data [1]... data[N-1] <-- end(data) // set of values to test N for (auto N :...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has...
Please code in C# (C-Sharp) Assignment Description A pirate needs to do some accounting and has asked for your help. Write a program that will accept a pirate’s starting amount of treasure in number of gold pieces. The program will then run one of two simulations, indicated by the user: 1) The first simulation runs indefinitely, until one of two conditions is met: the pirate’s treasure falls to 0 or below, or the pirate’s treasure grows to 1000 or above....
Original C code please. Part 1: You can do A, B, and C in one program...
Original C code please. Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7. 14        28       ...
Requirements: Code in C++. With given information, write the solution to this problem so that it...
Requirements: Code in C++. With given information, write the solution to this problem so that it is understandable to someone with basic knowledge of C++ (ex: only keep basic libraries, keep coding shortcuts to a minimum). Also leave comments in the code (plz), the logic behind solving this problem if possible, and explanation of what the keys to solving this problem is and how to run test cases to ensure correctness of code. Problem: For this problem you will compute...
Please do this in C++ only. Please Show your code and your output too. Would you...
Please do this in C++ only. Please Show your code and your output too. Would you focus on number 2 in the Required functions:? Please use all the variables in the program. The assignment problem: You are asked to develop an application that prints a bank’s customers' names, IDs, and accounts balances in different ways. In this program, you need to read a file of customer’s data into different arrays, pass these arrays to functions as (array parameter), calculate the...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write...
For C code: Do not use any function other than getchar, scanf, and printf Q2.A) Write a program to do the following: Read in two values into variables X   and y from the keyboard (they may be decimal values like 2.3). Read in a 3rd value (which is really small < 1.0 ) into variable E. Using a loop find out the value of N in the below expression. i.e. Each iteration of the loop reduced the value of Y...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT