Question

C++

2.3 Activity 3: Bubble Sort For this activity, you are required to provide files called act3.cpp as well as a makefile to com

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

Here is the code:

# include <iostream>
# include <fstream>
# include <sstream>
using namespace std;

string bubSort(double arr[8]);

int main() {
string iFileName = "list.txt";
//open the file for reading
ifstream iFile(iFileName);
double arr[8];

//read line by line from the file
string line;
while (getline(iFile, line)) {
//from the line, read the 8 values
stringstream l(line);
for (int i=0; i<8; i++) {
l >> arr[i];
}
//print the sorted values string
cout << bubSort(arr) << endl;
}
return 0;
}

string bubSort(double arr[8]) {
bool swapped = true;

//do this while some element has been swapped
while (swapped) {
swapped = false;

//go over the array
for (int i=0; i<7; i++) {
//if current element is more than the next element
if (arr[i] > arr[i+1]) {
//swap them
double t = arr[i];
arr[i] = arr[i+1];
arr[i+1] = t;
swapped = true;
}
}
}

//prepare the string from the sorted array
string s="";
char a[100];
for (int i=0; i<7; i++) {
sprintf(a, "%.3f,", arr[i]);
s += string(a);
}
sprintf(a, "%.3f", arr[7]);
s += string(a);;
return s;
}

Here is a screenshot of the input file "List.txt":
1.43.24.57.19.67.49.34 0.543 -3.2349.92 103.42 5.98764 - 12.45 8567192.345 -3452

Here is a screenshot of the output:
Ꮎ .543,1.4ᎾᏫ ,3.2ᎾᎾ , 4.5ᎾᏫ ,7.1ᎾᏫ ,7.4ᎾᏫ ,9.34Ꮎ ,9.6ᏫᎾ -3452.ᎾᎾᎾ , -12.45Ꮎ , -3.234 ,5.988 ,9.92Ꮎ ,92.345 ,103.42Ꮎ ,85671.ᎾᎾ

Comment in case of any doubts.

Add a comment
Know the answer?
Add Answer to:
C++ 2.3 Activity 3: Bubble Sort For this activity, you are required to provide files called...
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
  • For this activity, you are required to provide files called fun.cpp as well as a makefile to comp...

    For this activity, you are required to provide files called fun.cpp as well as a makefile to compile and run it. In your file, fun.cpp, should have a skeleton of a main program as per normal which you will then fill it in as per the following. The objective of this activity is to demonstrate the basics of functions and their use. For this activity, you will need to declare your functions alongside your main program skeleton. You will use...

  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

    Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.

  • Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent...

    Bubble Sort Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Example: First Pass: ( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4 ( 1 4 5 2 8...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional...

    in C please 20. Change the bubble sort algorithm (Program 12-5) as follows: Use two directional bubbling in each pass. In the first bubbling, the smallest ele. ment is bubbled up; in the second bubbling, the largest element is bubbled I down. This sort is known as the shaker sort. 12-5 Bubble Sort (continued) // Statements // Each iteration is one sort pass for (int current = 0, sorted = 0; current <= last && !sorted; current++) for (int walker...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Write a c++ program to implement bubble sort of the data point given in the file...

    Write a c++ program to implement bubble sort of the data point given in the file data_points.txt file found in the Files area under assignments. Submit the source code in canvas. Do not change the data file name in your program if you are using file operations to load the values and should read from the same directory where your program reside

  • C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts...

    C++ SORTING – BUBBLE SORT METHOD Use the below Text File and write code that sorts it based on the users sort method selection. Please provide a separate .cpp file wit hthe code containing the sort method. The sorting method uses the text file below and sorts it accordingly. Seperate the sorting method into an additional C++ file. ********************************* Text File Below is a text file called classes.txt. This text file lists, by course number and section number a series...

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