answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Butoxors
2 months ago
10

A method countDigits(int num) of class Digits returns the remainder when the input argument num(num > 0) is divided by the nu

mber of digits in num. The function compiles successfully but fails to return the desired result due to logical errors. Your task is to debug the program to pass all the test cases.
Computers and Technology
1 answer:
ivann1987 [1K]2 months ago
7 0

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.

You might be interested in
You have configured your firewall to authenticate a group of 100 users who are in your company. You set up the database of users
zubka84 [1067]
Network Access Control (NAC) is essential to ensure that the network is accessed only by authenticated users. It verifies the identity of users, granting network access solely to those who are verified. Various NAC solutions such as Impulse SafeConnect, Extreme Networks, ExtremeControl, Auconet BICS, ForeScout CounterACT, and Pulse Policy Secure can be integrated into the system. By implementing basic profiling, these solutions ensure that only authenticated users engage with the network. Additionally, employing encryption-decryption algorithms can enhance security. Algorithms like RSA and DES can be utilized for encrypting data from the database effectively. Once the information reaches the network component, it can be decrypted by generating the necessary key through the same algorithm. This method ensures that only authorized users can access sensitive information while also preventing data leaks during transmission due to encryption.
4 0
1 month ago
Consider a satellite orbiting the earth. Its position above the earth is specified in polar coordinates. Find a model-view matri
zubka84 [1067]

Answer:

[1 0 0 0]

[0 1 0 0]

[0 0 1 -d]

[0 0 0 1]

Explanation:

6 0
2 months ago
This question involves the creation of user names for an online system. A user name is created based on a user’s first and last
Rzqust [1037]

Response:

Refer to the explanation

Clarification:

import java.util.*;

class UserName{

ArrayList<String> potentialNames;

UserName(String fName, String lName){

if(this.isValidName(fName) && this.isValidName(lName)){

potentialNames = new ArrayList<String>();

for(int j=1;j<fName.length()+1;j++){

potentialNames.add(lName+fName.substring(0,j));

}

}else{

System.out.println("firstName and lastName should only consist of letters.");

}

}

public boolean isTaken(String name, String[] array){

for(int j=0;j<array.length;j++){

if(name.equals(array[j]))

return true;

}

return false;

}

public void removeUnavailableUserNames(String[] takenNames){

String[] namesArray = new String[this.potentialNames.size()];

namesArray = this.potentialNames.toArray(namesArray);

for(int j=0;j<takenNames.length;j++){

if(isTaken(takenNames[j],namesArray)){

int idx = this.potentialNames.indexOf(takenNames[j]);

this.potentialNames.remove(idx);

namesArray = new String[this.potentialNames.size()];

namesArray = this.potentialNames.toArray(namesArray);

}

}

}

public boolean isValidName(String str){

if(str.length()==0) return false;

for(int j=0;j<str.length();j++){

if(str.charAt(j)<'a'||str.charAt(j)>'z' && (str.charAt(j)<'A' || str.charAt(j)>'Z'))

return false;

}

return true;

}

public static void main(String[] args) {

UserName user1 = new UserName("john","smith");

System.out.println(user1.potentialNames);

String[] existing = {"harta","hartm","harty"};

UserName user2 = new UserName("mary","hart");

System.out.println("potentialNames prior to removal: "+user2.potentialNames);

user2.removeUnavailableUserNames(existing);

System.out.println("potentialNames following removal: "+user2.potentialNames);

}

}

8 0
3 months ago
Other questions:
  • Sites like Zillow get input about house prices from a database and provide nice summaries for readers. Write a program with two
    12·1 answer
  • Q) Select the two obstacles for data parsing
    5·2 answers
  • A technician with a PC is using multiple applications while connected to the Internet. How is the PC able to keep track of the d
    8·2 answers
  • A client has macular degeneration resulting in moderate visual impairment. The client works as a data entry clerk and wants to c
    15·1 answer
  • Write a program with total change amount as an integer input, and output the change using the fewest coins, one coin type per li
    11·1 answer
  • Define a new object in variable sculptor that has properties "name" storing "Michelangelo"; "artworks" storing "David", "Moses"
    12·1 answer
  • A ____ partition contains the data necessary to restore a hard drive back to its state at the time the computer was purchased an
    8·2 answers
  • 2) Complete the get_num_of_characters() function, which returns the number of characters in the user's string. We encourage you
    11·1 answer
  • When performing actions between your computer and one that is infected with a virus, which of the following offers NO risk of yo
    11·2 answers
  • Which of the following image file formats uses lossy file compression?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!