Question

In: Computer Science

Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...

Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only one .cpp file

Solutions

Expert Solution

#include <iostream>
using namespace std;

//prototypes
void input (double x[], int length);
void copy (double source[], double dest[], int length);
void sort (double x [], int length);
void display (double x[], int length);

int main()
{
double data[20],sdata[20];
int length;
   cout<<"\nEnter data item count <1-20>";
cin>>length;
if(length <0 || length > 20)//validate length
cout<<"Item count is NOT within required range. Required range is 1 to 20.";
  
input(data,length);

copy(data,sdata,length);
  
sort(sdata,length);
  
cout<<"\nOriginal Data: ";
display(data,length);
  
cout<<"\nSorted Data: ";
display(sdata,length);
  
   return 0;
}

void input (double x[], int length)
{
int i;
for(i=0;i<length;i++)
{
cout<<"\nEnter score";
cin>>x[i]; //input score
}
}
void copy (double source[], double dest[], int length)
{
int i;
for(i=0;i<length;i++)
{
dest[i] = source[i]; //copy array elements
}
}
void sort (double x[], int length) //bubble sort
{
double temp;
int i,j;
for(i=0;i<length;i++)
{
for(j=0;j<length-i-1;j++)
{
if(x[j]> x[j+1]) //swap if not in ascending order
{
temp = x[j];
x[j] = x[j+1];
x[j+1] = temp;
}
}
}
}
void display (double x[] ,int length)
{
int i;
for(i=0;i<length;i++)
{
cout<<x[i]<<" ";
}
}

output:

Enter data item count <1-20> 9
Enter score 5.5
Enter score 3.3
Enter score 4.4
Enter score 2.2
Enter score 9.9
Enter score 6.6
Enter score 8.8
Enter score 7.7
Enter score 1.1

Original Data: 5.5 3.3 4.4 2.2 9.9 6.6 8.8 7.7 1.1   
Sorted Data: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9

Do ask if any doubt.


Related Solutions

Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from...
1.   Bubble Sort Implement a bubble sort program that will read from a file “pp2.txt” from the current directory a list of intergers (10 numbers to be exact), and the sort them, and print them to the screen. You can use redirection to read data from a given file through standard input, as opposed to reading the data from the file with the read API (similar to Lab #1). You can assume the input data will only have 10 numbers...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that...
C++ Bubble Sort Write a program that ask user to enter 7 numbers and store that in array. Display that all numbers before and after performing Bubble sort. You must have to create new function with required parameter to perform Bubble sort. Sample Run :- Enter 1 number :- 1 Enter 2 number :- 5 Enter 3 number :- 7 Enter 4 number :- 45 Enter 5 number :- 90 Enter 6 number :- 6 Enter 7 number :- 55...
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
Implement a program in C++ that does the following: Ask the user and read at least...
Implement a program in C++ that does the following: Ask the user and read at least 10 numbers as input from the keyboard and stores them in a an array Displays the array of numbers on the screen (as is, before sorting) Sorts those numbers in the array using Selection Sort Algorithm Displays the array of numbers on the screen (AFTER sorting)
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Must implement a simple program that asks the user for a width and a height, storing...
Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height. Area and Perimeter Calculator Enter width: 3 Enter height: 5 If your shape is a rectangle, its area is 15.0...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT