Hello can you help solve this problem in C++? thank you Practise Problem-5: writ
ID: 3589802 • Letter: H
Question
Hello
can you help solve this problem in C++?
thank you
Practise Problem-5: write a C++ program to read a list of values from the user and then sort them in ascending order using Bubble sort. Your program should perform the following tasks. First your program should ask the user for the number of values in the list. Next, you should call a function to read the values from the user - you should define a function for reading the input values the function should the array to store user input values as input parameter. The second parameter should be number of values to input. (You can use the function readArray from Exercise -1). . . Write a function named bubblesort which should take the list and its length as input parameters. The function should sort the values of the list and print the updated list.Explanation / Answer
#include <iostream.h>
#include <conio.h>
using namespace std;
Class sort
{
public:
int arr[100];
void readarray(int n1)
{
for(i = 0; i < n1; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
BubbleSort(arr, n1);
}
void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 1; i < n; ++i)
{
for (j = 0; j <( n-i); ++j)
{
if (arr[j] > arr[j+1])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Display the sorted data.
cout<<" Sorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
}
};
int main()
{
int n;
sort s;
cout<<" Enter the number of data element to be sorted: ";
cin>>n;
s.readarray(n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.