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 and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
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...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
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 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.
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT