Provide a program that takes the digits of your roll number from the user, modify each digit by adding 4 in it
then sort the modified digits using Selection sort.
#include<iostream>
#include<string>
using namespace std;
void SelectionSort(int integersSelectionSort[],int n)
{
//pos_min is short for position of min
int pos_min, temp;
for (int i = 0; i < n - 1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j = i + 1; j < n; j++)
{
if (integersSelectionSort[j] < integersSelectionSort[pos_min])
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = integersSelectionSort[i];
integersSelectionSort[i] = integersSelectionSort[pos_min];
integersSelectionSort[pos_min] = temp;
}
}
}
void main()
{
int rollNumber;
int digits[10];
int n=0;
cout<<"Enter your roll number: ";
cin>>rollNumber;
while (rollNumber > 0) {
digits[n]= (rollNumber % 10)+4;
n++;
rollNumber = rollNumber / 10;
}
SelectionSort(digits,n);
cout<<"Digits: ";
for (int i = 0; i < n; i++){
cout<<digits[i]<<" ";
}
cin>>n;
}
Comments
Leave a comment