Declare an array ‘list’ of length 15.
• Input the values from user.
• Pass that array to a function sorting.
• Your function should only accept a pointer as an argument.
• Sort the values of the array in ascending order using any of the sorting techniques.
#include <iostream>
using namespace std;
void sort(int *arr){
int temp;
for (int i = 1; i < 15; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr [j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
for (int i = 0; i < 15; i++) {
cout<<"\n"<<arr[i];
}
}
int main()
{
int list[15]; //Declare an array ‘list’ of length 15.
//Input the values from user.
cout<<"\nEnter 15 integers:\n";
for(int i=0;i<15;i++){
cin>>list[i];
}
sort(list);
return 0;
}
Comments
Leave a comment