Question

***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...

***C++ CODING***

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( )

  The function returns a pointer that points to the locations with integers reading from the file data.txt.

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.
Here is the Link to the Bubble Sort.

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

Homework Answers

Answer #1

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

#include<iostream>

#include<fstream>

#include<cstdlib>

using namespace std;

int *readData(int &n){

    ifstream in;

    in.open("data.txt");

    if(in.fail()){

        cout<<"unable to open file"<<endl;

        return 0;

    }

    in>>n;

    int *arr = new int[n];

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

        in>>*(arr+i);

    }

    in.close();

    return arr;

}

void bsort(int *arr, int last){

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

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

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

                int temp = *(arr+j);

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

                *(arr+j+1) = temp;

            }

        }

    }

}

void writeToConsole(int *arr, int last){

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

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

    }

    cout<<endl;

}

int main(){

    int *arr;

    int n;

    arr = readData(n);

    cout<<"Before sorting: "<<endl;

    writeToConsole(arr, n);

    bsort(arr, n);

    cout<<"After sorting: "<<endl;

    writeToConsole(arr, n);

    delete[] arr;

    return 0;

}

Know the answer?
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
Similar Questions
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: 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....
In C, write a function void (int number[ ]) sorts the given array of integers into...
In C, write a function void (int number[ ]) sorts the given array of integers into non-decreasing order, storing the result in the given array. The array of integers may have any number of elements, but the last element of the array must be zero
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
PLEASE SOLVE THIS C++ PROGRAM IN AN EASY LANGUAGE WAY DIFFERENT THEN THE TEXTBOOK ANSWER The...
PLEASE SOLVE THIS C++ PROGRAM IN AN EASY LANGUAGE WAY DIFFERENT THEN THE TEXTBOOK ANSWER The bubble sort is another technique for sorting an array. A bubble sort compares adjacent array elements and exchanges their values if they’re out of order. In this way, the smaller values “bubble” to the top of the array (toward element 0), while the larger values sink to the bottom of the array. After the first pass of a bubble sort, the last array element...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values to an array and the other that uses the insertion method to sort the array. And of course I have a main method. I need to call both of these methods in the main method. I have successfully called the random assigned method from the main method, but I am confused on how to call the second sort method. Here is what I have--...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
For this assignment you may use regular pointers. You are to write a program that performs...
For this assignment you may use regular pointers. You are to write a program that performs basic statistical analysis on a 500 element array of integers. Your program should include the following programmer defined functions: int* generateArray(int size){ This function should dynamically create an array of 501 elements and provide random numbers to fill the array. The random numbers should range from 10 to 100. The function should return the created array to the array generated in the main program....
You are given a list, L, and another list, P, containing integers sorted in ascending order....
You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L, P). You may use only the public STL container operations. What is the running time of your procedure?
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT