Question

In: Computer Science

***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. Please include comments to understand code.

Requirements
Implement the following functions:

  1. int readData( int **arr)
    arr is a pointer to 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

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <iostream>

#include <fstream>

using namespace std;

//this function reads data from data.txt

int readData(int **arr)

{

    //open file for input

    ifstream infile("data.txt");

    //if can't open file

    if (!infile.is_open())

    {

        cout << "Can't open input file\n";

        exit(1);

    }

    //number of integers in data.txt

    int count = 0, temp;

    //read integer 1 by 1

    while (infile >> temp)

    {

        *(*(arr) + count) = temp;

        count++;

    }

    //close file

    infile.close();

    return count;

}

//this function sorts given array of size last using bubble sort

void bsort(int *arr, int last)

{

    //for i from 0 to last -1

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

    {

        //for j from 0 to last-i-1

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

        {

            // if current element is greater than next element

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

            {

                //this performs swap of jth with j+1th element

                int temp = *(arr + j);

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

                *(arr + j + 1) = temp;

            }

        }

    }

}

//this function writes sorted array to output

void writeToConsole(int *arr, int last)

{

    cout << "Sorted Data: ";

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

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

    cout << endl;

}

int main()

{

    //create array

    int *array = new int[100];

    int last = readData(&array);

    bsort(array, last);

    writeToConsole(array, last);

    delete[] array;

}

data.txt:

Output:


Related Solutions

C++ Write a program for sorting a list of integers in ascending order using the bubble...
C++ Write a program for sorting a list of integers in ascending order using the bubble sort algorithm Requirements 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. The first integer number in the file is the number of intergers. After the first number,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers....
using c++ 10. Sorting Orders Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using an ascending order selection sort, modified to print out the...
Write C program that reorders elements of an array of integers such that the new order...
Write C program that reorders elements of an array of integers such that the new order is in descending order (first number being the largest). Must have a main function and a swap function. - int main() will declare an array with the values { 32, 110, 79, 18, 22, 2}. This array will be passed to the swap function. - the void swap function will perform the necessary operations to reorder the elements of the array. - After swap()...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Given a queue of integers, write an algorithm and the program in c++ that, using only...
Given a queue of integers, write an algorithm and the program in c++ that, using only the queue ADT, calculates and prints the sum and the average of the integers in the queue without changing the contents of the queue.
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
C++ code please: Write a program that first gets a list of integers from input. The...
C++ code please: Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates how much to multiply the array by. Finally, print out the entire array with each element multiplied by the last input. Assume that the list will always contain less than 20 integers. Ex: If the input is 4 4 8 -4 12...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT