Answer to Question #295251 in C++ for Jason Roy

Question #295251

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.


1
Expert's answer
2022-02-08T10:17:28-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog