Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/*==================================================================================== Name : FactorialCalculator Author : YOUR_NAME Description : Calculate fatorial of user-entered integer. Display the result in both decimal and binary format. Lab purposes:

/*==================================================================================== Name : FactorialCalculator Author : YOUR_NAME Description : Calculate fatorial of user-entered integer. Display the result in both decimal and binary format. Lab purposes: 1. Get binary by performing bitwise operations; 2. Observe integer overflow 3. Optional: Implement the calculateFactorial method recursively. Observe stack overflow. 4. Questions to answer after implementing the program: Q1: With int type in use, what is the largest number that can be correctly calculated for factorial? Q2: How to detect integer overflow in the factorial calculation? ====================================================================================

#include  #include  #include  int calculateFactorial(int n); void getBinary(int arr[], int number, int numOfBits); void printArray(int arr[], int numberOfElements); int main(void) { int numOfBits = 8 * sizeof(int); int arr[numOfBits]; //int upperBound = pow(2, numOfBits - 1) - 1; //int lowerBound = - pow(2, numOfBits - 1); //Declare integer n to hold the decimal integer int n = 0; int factorialResult = 0; puts("Enter integers and convert it to binary."); puts("Non-numeric input terminates program."); //Continually taking input and converting it to binary while (scanf("%d", &n) == 1) {//if successfully read in ONE decimal integer //Initialize the array to all 0s for(int i = 0; i < numOfBits; i ++){ arr[i] = 0; } factorialResult = calculateFactorial(n); getBinary(arr, factorialResult, numOfBits); //Output binary: printf("Factorial(%d) = %d,\t", n, factorialResult); printf("Binary: "); printArray(arr, numOfBits); } return EXIT_SUCCESS; } /* calculateFactorial of argument number n Factorial of a positive integer n is defined as the product of all positive integers less than or equal to n. Factorial zero is defined as equal to 1. */ int calculateFactorial(int n){ int result = 0; // what should be the initial value? return result; } /*getBinary - get the binary representation of number into the array arr. Arguments: arr - the integer array to hold the binary representation number - the integer to get binary from numOfBits - the size of the arr and the number of bits for the binary Algorithm requirement: make use of bitwise shifting and masking to get the bit values for the number */ void getBinary(int arr[], int number, int numOfBits){ } /* printArray: print numberOfElements in the argument integer array */ void printArray(int arr[], int numberOfElements){ int i = 0; for(; i < numberOfElements; i ++){ printf("%d", arr[i]); } puts("");//new line }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Analysis Using SQL And Excel

Authors: Gordon S Linoff

2nd Edition

111902143X, 9781119021438

More Books

Students also viewed these Databases questions