In: Computer Science
This is a C++ assignment
The necessary implementations:
Requirements to meet:
Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order.
Include in your program the following functions:
Main calls each function once, in turn and passes the array and it's size to each function. Creates the array. Does not interact with the user itself.
Note: You must implement the sorting algorithm yourself, either selection sort or bubble sort
ANSWER: Here I am giving you the code and output please like it or comment on your problem before going to downvote.
CODE:
#include <iostream>
using namespace std;
void sortArray(double arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1]) {
double temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
void printArray(double arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
void fillArray(double arr[], int size){
cout<<"Enter 5 numbers:"<<endl<<endl;
int j=1;
for(int i=0;i<size;i++){
cout<<"Enter number "<<j<<": ";
cin>>arr[i];
j++;
}
}
int main()
{
double arr[5], n=5;
fillArray(arr,5);
sortArray(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT: