Transform this exercise of sorting into functions: #include #include #include using namespace std; int main() { srand(time(0)); const int size=5; int grades[size]={0}; int temp=0; for(int i=0; igrades[i+1]) { temp = grades[i]; grades[i] = grades[i+1]; grades[i+1] = temp; } } for(int i=0; i
Transform this exercise of sorting into functions:
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
srand(time(0));
const int size=5;
int grades[size]={0};
int temp=0;
for(int i=0; i<size;i++)
grades[i]= rand()%101;
for(int i=0; i<size; i++)
cout<<grades[i]<<" ";
cout<<endl;
//sorting at most you need n-1 iterations to sort the array.
for(int iteration=1; iteration<=size-1;iteration++) // external loop
for(int i=0; i<size-1; i++)
{
if(grades[i]>grades[i+1])
{
temp = grades[i];
grades[i] = grades[i+1];
grades[i+1] = temp;
}
}
for(int i=0; i<size; i++)
cout<<grades[i]<<" ";
cout<<endl;
return 0;
}
Step by step
Solved in 3 steps with 1 images