Show a hand trace of the code; determine the output and explain, briefly, what t
ID: 3831371 • Letter: S
Question
Show a hand trace of the code; determine the output and explain, briefly, what the code accomplishes.
#include <iostream>
using namespace std;
bool isAthing(int);
int countAthing(int&,int);
void doIt(int);
int main()
{
doIt(12);
doIt(17);
doIt(144);
}
void doIt(int num0)
{
int num1 = num0;
bool first=true;
for (int i=2;num1>1;i++)
{
if(isAthing(i) && num1%i==0)
{
if (!first)
cout << " * ";
first = false;
cout << i << "^" <<countAthing(num1,i);
}
}
cout << " = " << num0 << endl;
}
bool isAthing(int num)
{
for (int i=2;i<=num/2;i++)
{
if (num%i==0)
return false;
}
return true;
}
int countAthing(int& num0,int num1)
{
int num2;
for(num2=0;num0%num1==0;num2++)
num0/=num1;
return num2;
}
Explanation / Answer
#include <iostream>
using namespace std;
bool isAthing(int);
int countAthing(int&,int);
void doIt(int);
int main()
{
doIt(12); //Calls the function doIt() with an input parameter 12.
doIt(17); //Calls the function doIt() with an input parameter 17.
doIt(144); //Calls the function doIt() with an input parameter 144.
}
void doIt(int num0) //Given the parameter num0.
{
int num1 = num0; //num1 is assigned with num0.
bool first=true; //Assigns first with true.
for (int i=2;num1>1;i++) //This loop runs as long as num1 is greater than 1.
{
if(isAthing(i) && num1%i==0) //Called isAthing(2), and num1 % 2 == 0
{
if (!first) //If false.
cout << " * "; //* will be printed.
first = false; //first is assigned with false.
cout << i << "^" <<countAthing(num1,i); //Prints i ^ countAthing(num1, i).
}
}
cout << " = " << num0 << endl; //Prints = num0.
}
bool isAthing(int num) //Given the integer value num.
{
for (int i=2;i<=num/2;i++) //The loop runs for i = 2 to num/2.
{
if (num%i==0) //If num % i is 0.
return false; //Returns false.
}
return true; //Returns true.
}
int countAthing(int& num0,int num1) //Given the parameter num0, and num1.
{
int num2;
for(num2=0;num0%num1==0;num2++) //This loop runs as long as num0 % num1 is 0.
num0/=num1; //num0 is assigned with num0 / num1.
return num2; //Returns num2.
}
//doIt(12).
//num0 = 12.
//num1 = 12.
//first = true.
//i = 2.
//num1 i.e., 12 is greater than 1.
//isAthing(2).
//num = 2.
//i = 2.
//2 <= 2/2 is false, so will come out of the loop.
//returns true.
//As, isAthing(2) is true, and 12 % 2 == 0 is also true.
//!first is !true i.e., false, so if block will not be executed.
//first = false.
//Calls countAthing(12, 2).
//num0 = 12, num1 = 2.
//num2 = 0.
//12 % 2 == 0 is true.
//num0 is updated to 12 / 2 = 6.
//num2 = 1.
//6 % 2 == 0 is true.
//num0 is updated to 6 / 2 = 3.
//num2 = 2.
//3 % 2 == 0 is false, so will return 2.
//Will prints 2 ^ 2.
//i = 3.
//num1 is 3 is greater than 1.
//isAthing(3) returns true.and and num is 3 % 3 == 0 is also true.
//if !false is true, so if block will be executed.
//Prints *
//first is set to false.
//countAthing(3, 3) returns 1, and num0 is updated to 1.
//Will prints 3 ^ 1.
//i = 4.
//1 > 1 fails, and will come out of the loop.
//Prints = 12.
//So, finally the outpu tis: 2^2 * 3^1 = 12.
//Applying the same tracing,
//doIt(17) will print 17^1 = 17.
//doIt(144) will print 2^4 * 3^2 = 144.
Simply speaking, the above program will factorize the value.