C programming Write a C program called div.cthat implements division on two unsi
ID: 3665529 • Letter: C
Question
C programming
Write a C program called div.cthat implements division on two unsigned integers.
1.Name your executable div.out
2.You cannot use division in your solution
3.Arguments should be accepted from the command line
1.the first argument is the dividend
2.The second argument is divisor
4.Your program should display the quotient and remainder after doing the division
5.Your program must complete in O(1) time
1.This is possible because an integer is 32 bits long and so the loop that does the division should not take longer than 32 iterations.
2.Because of this restriction the following solution is not acceptable as it does not meet the O requirements
void bad_div(unsigned int dividend,
unsigned int divisor,
unsigned int* quotient,
unsigned int *remainder){
*quotient = 0;
while(dividend >= divisor){
(*quotient)++;
dividend -= divisor;
}
*remainder = dividend;
}
Explanation / Answer
//Program to findout quotient and remainder after division
#include<stdio.h>
#inlcude<conio.h>
void main(int argc, char* argv[])
{
unsigned int divident,divisor,quotient,remainder;
unsigned int temp_quotient, final_quotient;
divident= argv[1];
divisor = argv[2];
temp_quotient = divident;
while(divident <=divisor)
{
quotient = divident - divisor;
divident = quotient;
}
final_quotient = quotient;
remainder = temp_quotient - (final_quotient * divisor);
printf(" Quotient : =", quotient);
printf(" Remainder : = ", remainder);
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.