Using a magnifying glass.
Answer:
#include <iostream>
using namespace std;
class Digits
{
public:
int num;
int read() //method to read num from user
{
cout<<"Enter number(>0)\n";
cin>>num;
return num;
}
int digit_count(int num) //method to count number of digits of num
{
int count=0;
while(num>0) //loop till num>0
{
num/=10;
count++; //counter which counts number of digits
}
return count;
}
int countDigits(int num) //method to return remainder
{
int c=digit_count(num); //calls method inside method
return num%c;
}
};
int main()
{
Digits d; //object of class Digits is created
int number=d.read(); //num is read from user
cout<<"\nRemainder is: "<<d.countDigits(number); //used to find remainder
return 0;
}
Output:
Enter number(>0)
343
Remainder is: 1
Explanation:
The program has a logical error that needs rectification. A correctly structured program calculates the remainder when a number is divided by the count of its digits. A class named Digits is created, consisting of the public variable 'num' and methods for reading input, counting digits, and calculating the remainder.
- read() - This function asks the user to enter the value for 'num' and returns it.
- digit_count() - This function accepts an integer and counts how many digits it has, incrementing a counter until 'num' is less than or equal to 0. It ultimately returns the digit count.
- countDigits() - This function takes an integer and delivers the remainder from dividing that number by its digit count. The digit count is computed using the 'digit_count()' method.
Finally, in the main function, a Digits object is instantiated, and its methods are utilized to produce an output.
Answer:
Below is the explanation provided.
Elaboration:
In the first array => 8, 5, -9, 14, 0, -1, -7, 3.
- 8, 5, -9, 14, 0, -1, -7, 3 For the first iteration, find the smallest from the first to the eighth and reposition the third element in the first place.
- -9, 8, 5, 14, 0, -1, -7, 3 In the second round, identify the smallest from the second to eighth and adjust the penultimate element in the second slot.
- -9, -7, 8, 5, 14, 0, -1, 3 For the third pass, find the least from third to eighth and move the second last item from that segment.
- -9, -7, -1, 8, 5, 14, 0, 3 For the fourth pass, find the minimum from the fourth to eighth and shift the last second item from that section.
- -9, -7, -1, 0, 8, 5, 14, 3 For the fifth pass, seek for the smallest from the fifth to eighth and place the last element from that portion.
- -9, -7, -1, 0, 3, 8, 5, 14 For the sixth pass, identify the smallest from the sixth to eighth and shift the last second element from that group.
- -9, -7, -1, 0, 3, 5, 8, 14 are in order now.
In the second array => 15, 56, 24, 5, 39, -4, 27, 10.
- 15, 56, 24, 5, 39, -4, 27, 10 In the first iteration, identify the smallest item from the first to the eighth and reposition the last third item in that section.
- -4, 15, 56, 24, 5, 39, 27, 10 In the second round, find the minimum item from second to eighth and interchange the last fourth item in that segment.
- -4, 5, 15, 56, 24, 39, 27, 10 For the third pass, select the smallest from the third to eighth and shift the last item from that set.
- -4, 5, 10, 15, 56, 24, 39, 27 For the fourth stage, determine the smallest from the fourth to eighth and do not shift its position as it is already sorted.
- -4, 5, 10, 15, 56, 24, 39, 27 For the fifth iteration, check for the smallest from the fifth to the eighth and intersperse the last third item from that section.
- -4, 5, 10, 15, 24, 56, 39, 27 For the sixth phase, find the smallest from the sixth to the eighth and exchange the last item in the sixth.
- -4, 5, 10, 15, 24, 27, 56, 39 are in order now.