Question

In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...

In C++:
A. Write 3 functions that are called in the program.
 1. Function readInput prompt user to enter an integer to store to the parameter.
 2. Function isPerfectSquare take an integer parameter and checks whether it's a perfect square, that it's square root is an integer.
 3. Function min3 return the mainimum value of the parameter values, it shouldn’t use any if statement to compare the parameters but call min2 to do the comparison.
 Extra Credit: Function printPrimeFactorization print out parameter as a product of prime factors. 

B.
Write a function to print a diamond which calls following printChars function to print each characters.
void printChars(int x, char c)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answers below.

A.

#include <iostream>
#include <cmath>
using namespace std;

void readInput(){
int number;
// Gettig user input
cout<<"Enter an integer value ";
cin>>number;
cout<<"You have entered "<<number<<endl;
}

void isPerfectSquare(int number){
int t=0;
for(int i=1 ; i<50 ; i++){
if (number == (i*i)){
cout<<"\nThe number "<<number<< " is a perfect square";
t++;
}
}
if (t==0){
cout<<"\nThe number "<<number<< " is not a perfect square";   
}
}
void min3 (int x, int y, int z){
int c = 0;
// In a loop, repeatedly subtract x, y and z by 1 and increment c.
// The number which becomes 0 first is the smallest.
// After the loop terminates, c will hold the minimum value of three numbers.
while (x && y && z) {
x--;
y--;
z--;
c++;
}
cout<<"\nThe minimum among three numbers is "<< c<<endl;
}
void printPrimeFactorization(int n)
{
cout<<"\nThe prime factors of "<<n<<" is "<<endl;
// Print the number of 2s that divide n
while (n % 2 == 0)
{
cout << 2 << " ";
n = n/2;
}
  
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
cout << i << " ";
n = n/i;
}
}
if (n > 2)
cout << n << " ";
}
  

int main()
{   
readInput();
isPerfectSquare(16);
isPerfectSquare(1000);
min3(3,8,2);
printPrimeFactorization(315);
  

return 0;
}

OUTPUT :

Enter an integer value 9 You have entered 9 The number 16 is a perfect square The number 1000 is not a perfect square The min

B.

Please find the program, output below.

#include <iostream>
using namespace std;

void printChars(int r, char c){
int i,j;
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<c;
cout<<endl;
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
cout<<" ";
for(j=1;j<=2*i-1;j++)
cout<<c;
cout<<endl;;
}
}
  

int main()
{   
int value;
char ch;
cout<<"Enter the integer value : ";
cin>>value;
cout<<"Enter the character value : ";
cin>>ch;
printChars(value,ch);
return 0;
}

1 13 17 #include <iostream> 2 using namespace std; 3 4 void printChars(int r, char c){ 5 int i,j; 6 for(i=0;i<=r;i++) 7 { 8 f

Enter the integer value : 5 Enter the character value :

Enter the integer value : 5 Enter the character value : $ s $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $

Add a comment
Know the answer?
Add Answer to:
In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C-programming In activity 1, a function will accept an input string composed of a command and...

    C-programming In activity 1, a function will accept an input string composed of a command and a parameter. The function will extract the command and convert the parameter into an integer. The conversion of the string to an integer can be done using atoi()- alphanumeric to integer. stdlib.h needs to be included. Note that the parameter characters, e.g., “10”, need to be extracted from the input string, assign to an array, and fed to atoi(). Write a function that accepts...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

  • Write a Python program to prompt the user for an integer number. Assign this integer to...

    Write a Python program to prompt the user for an integer number. Assign this integer to a variable X. Then assign a variable Y to the X**3 (X to the power 3). Finally, assign the variable Z to the square root of Y and print the values of X, Y, and Z as shown in the example below. Use main() function to define and call the program. Example: Enter an integer number: 3 X = 3 if Y=X**3 then Y...

  • C++ Write a void function called getAge that has no formal parameter. The function should prompt the user for their...

    C++ Write a void function called getAge that has no formal parameter. The function should prompt the user for their age. An example of the call to the function could be as follows getAge();

  • 4. Write a logical function perfect Square that receives a positive integer number and checks if ...

    write the code in C please 4. Write a logical function perfect Square that receives a positive integer number and checks if it is a perfect square or not. Note: perfect square numbers are 4, 9,16,25,36 etc.... Write a main function that makes use of the perfect Square function to find and print all perfect squares between nl and n2. nl and n2 are end values of a range introduced by the user. ■ (inactive CAT EXE) Enter end values...

  • USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters...

    USING C++ PROGRAM Instructions - Part C Write a program that has three functions: All characters printed should be in White ► Call the first function from main().Print "I'm in main" (as shown in example) > Print "I'm in function 1" in the first function. Call the second function. ► Print "I'm in function 2” in the second function and then ask the user to enter a float value. Enter 25 Call a third function and pass the value to...

  • How can i print a diamond implementing these methods public static void printNChars(int n, char c))....

    How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write...

    Write a program (a3.py) that has 3 function definitions: magic-sequential(), magic-binary(), and main(). You will write the code for the above functions. Both magic functions (magic-sequential() and magic-binary()) will look for a magic index in a given sorted list of distinct integers. A magic index in a list myList[0 ... n-1] is defined to be an index i such that myList[i] = i . Both functions receive the list as a parameter and return an integer: either the magic index,...

  • C Question: Write a function p3 which receives a C string as a parameter, and an...

    C Question: Write a function p3 which receives a C string as a parameter, and an array of integers which will serve as indices into the string. The third parameter is the length of the integer array. The function jumbles the characters in the string according to the indices in the array of numbers. For example: char course [] = "CSC 373 Computer Systems I"; int indices[ ] = {0,1,4,5,6,7,0,1,25}; Then the function call p3(course, indices, 9); would cause course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT