Question

In: Computer Science

C++ (data structure using c++). please send me copyable file. Write a program and test a...

C++ (data structure using c++). please send me copyable file.

Write a program and test a program that translates the following Bubble Sort algorithm to a bubblesort function. The function's prototype is,

void bubblesort(int a[], int size);

Bubble Sort

The inner loop moves the largest element in the unsorted part of the array to the last position of the unsorted part of the array; the outer loop moves the last position of the unsorted part of the array.

The Bubble sort exchanges elements with adjacent elements as it moves the largest element in the unsorted part of the array to its correct position in the sorted part of the array. Once at its correct position an element never moves again. An element may change its position many times before moving to its correct position.

BubbleSort(A)

  for outer := A.length - 1 to 1

    for inner := 0 to outer –1

      if A[inner] > A[inner + 1] then

         temp := A[inner]

         A[inner] := A[inner + 1]

         A[inner + 1 := temp

      end if

    end for

  end for

end BubbleSort

program:

#include <ctime>

#include <iomanip>

#include <iostream>

#include <random>

#include <string>

using namespace std;

bool fill(int a[], int size,

uniform_int_distribution<int>& u,

default_random_engine& e)

{

if (size < 1)

return false;

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

a[i] = u(e);

return true;

}

void show(int a[], int size)

{

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

cout << a[i] << ' ';

}

void bubblesort(int a[], int size)

{

}

int main()

{

const int size = 8;

default_random_engine e;

uniform_int_distribution<int> u(10, 99);

int a1d[size];

fill(a1d, size, u, e);

show(a1d, size); cout << endl;

bubblesort(a1d, size);

show(a1d, size);

cout << endl;

system("pause");

return 0;

}


please help me i have to submit this homework today before 5pm

Solutions

Expert Solution

function:

void bubblesort(int a[], int size)

{

    int temp;

    for (int i = size-1; i > 0; i--)

    {

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

        {

            if(a[j]>a[j+1])

            {

                temp=a[j];

                a[j]=a[j+1];

                a[j+1]=temp;

            }

        }

    }   

}

complete code:

#include <ctime>
#include <iomanip>
#include <iostream>
#include <random>
#include <string>
using namespace std;

bool fill(int a[], int size,
    uniform_int_distribution<int>& u,
    default_random_engine& e)
{
    if (size < 1)
        return false;
    for (int i = 0; i < size; ++i)
            a[i] = u(e); 
    return true;
}

void show(int a[], int size)
{
    for (int i = 0; i < size; ++i)
        cout << a[i] << ' ';
}
void bubblesort(int a[], int size)
{
        int temp;
        for (int i = size-1; i > 0; i--)
        {
                for (int j = 0; j < i; j++)
                {
                        if(a[j]>a[j+1])
                        {
                                temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
            }
                }
        }       
}
int main()
{
    const int size = 8;
    default_random_engine e;
    uniform_int_distribution<int> u(10, 99);
    int a1d[size];
    fill(a1d, size, u, e);
    show(a1d, size); cout << endl;
    bubblesort(a1d, size);
    show(a1d, size);
    cout << endl;
    system("pause");
    return 0;
}

(For any doubt in the solution just leave a comment otherwise please press the like button)


Related Solutions

Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Can someone please write me a header file in c++ using the quadratic probing hash method...
Can someone please write me a header file in c++ using the quadratic probing hash method that will work with this program? #include "hash.h" #include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <list> using namespace std; const size_t KEYS_COUNT = 1000; // Number of keys to generate for testing string random_key(); class HashCheck { private: size_t count; Hash hash; public: HashCheck(Hash& h) { count = 0; hash = h; } void operator() (const string key) { if (hash[key] !=...
Write a program using interrupts to get data serially and send it to P2 while at...
Write a program using interrupts to get data serially and send it to P2 while at the same time Timer 0 is generating a square wave of 5 kHz. We've been basing our code on the Intel 8051 microntroller and assembly language.
write a Program in C++ Using a structure (struct) for a timeType, create a program to...
write a Program in C++ Using a structure (struct) for a timeType, create a program to read in 2 times into structures, and call the method addTime, in the format: t3 = addTime(t1, t2); Make sure to use add the code to reset and carry, when adding 2 times. Also, display the resultant time using a function: display(t3);
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a...
C++ Data Structure Write a program to change following infix expressions to postfix expressions using a stack a) D-B+C b) C*D+A*B c) (A*B)*C+D*F-C d) (A-4*(B-C)-D/E)*F
Write a C++ program that reads a file consisting of students’ test scores in the range...
Write a C++ program that reads a file consisting of students’ test scores in the range 0–200. It should then determine the number of students having scores in each of the following ranges: 0–24, 25–49, 50–74, 75–99, 100–124, 125–149, 150–174, and 175–200. Output the score ranges and the number of students. (Run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176,...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT