The division algorithm states that given two integers a and d, with , there exis
ID: 3637791 • Letter: T
Question
The division algorithm states that given two integers a and d, with , there exists unique integers q and r such that a = qd + r and , where | d | denotes the absolute value of d. The integer q is the quotient, r is the remainder, d is the divisor, and a is the dividend.You are to write a program, sp02hw_b.cpp, to prompt the user for a dividend and divisor and then display the division algorithm's results in the sentence format below (four separate examples are shown):
If a = 17 and d = 3, then q = 5 and r = 2, since 17 = (5)(3) + 2.
If a = 17 and d = -3, then q = -5 and r = 2, since 17 = (-5)(-3) + 2.
If a = -17 and d = 3, then q = -6 and r = 1, since -17 = (-6)(3) + 1.
If a = -17 and d = -3, then q = 6 and r = 1, since -17 = (6)(-3) + 1.
In testing your program, consider the four possible combinations of positive and negative dividends and divisors (as shown above).
The C++ operators for integer division do not conform to the division algorithm. Explain in output displayed to the user of the program when to expect results that disagree with the division algorithm. The program should not attempt to resolve this issue.
MY program is :
===========================================
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using std::cin;
using std::cout;
using std ::endl;
using namespace std;
int main()
{
cout << "This is The division algorithm prgram."<< endl;
int a ;
cout<<"Please enter the dividend: ";
int d ;
cout<<"Please enter the the divisor ";
cin>> d;
int r; // the reminder
int q; // the quotient
//a=qd+r; the rule
q = a / d;
r= a % d;
//If a = 17 and d = 3, then q = 5 and r = 2, since 17 = (5)(3) + 2.
//If a = 17 and d = -3, then q = -5 and r = 2, since 17 = (-5)(-3) + 2.
// If a = -17 and d = 3, then q = -6 and r = 1, since -17 = (-6)(3) + 1.
//If a = -17 and d = -3, then q = 6 and r = 1, since -17 = (6)(-3) + 1.
//OUTPUT
cout<<"if a= "<< a <<" and d = "<<d <<", then q ="<<q <<"= and r = " <<r <<",since "<<a<< "=""("<<q<<")""("<<d<<")" "+" <<r<<"."<<endl ;
cout<<" "<<endl;//space
return 0;
}
Explanation / Answer
#include #include #include #include using std::cin; using std::cout; using std ::endl; using namespace std; int main() { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.