(C++ Programming) Write a program that will take in a unsigned short integer and
ID: 3825862 • Letter: #
Question
(C++ Programming)
Write a program that will take in a unsigned short integer and then swap the contents of the upper eight bits with the lower eight bits. After it has done this, it will then invert all of the bits of the number and display the result...
You are considering buying a new car, and in order to figure out if your budget can afford the monthly payments, you want to write a program that will print out a table of payments. In order to do this, you will need the following information:
1. The total value of the loan
2. The lowest and highest interest rate you wish to pay.
3. The step size between interest rates.
Car loans are normally one, two, three, four, five, or six years in length. Given this information, your program will create a table (that is nicely formatted!) that will display the monthly payment for each duration of the loan and interest rate.
Example:
4 5 6 7
______________________
12| xxx
24| xxx
36|
48|
60|
72|
As with all programs written in class, the user must be prompted if they wish to to the task again, and all error checking must be performed.
Explanation / Answer
//below program will take the decimal input, print the binary output, swap the bytes then again convert back to //decimal and print the final result
#include<iostream>
using namespace std;
int bytearrayin [16] = { 0, 0, 0, 0,0, 0, 0, 0,0, 0, 0, 0,0, 0, 0, 0 };
int bytearrayout [16] = { 0, 0, 0, 0,0, 0, 0, 0,0, 0, 0, 0,0, 0, 0, 0 };
int i =15;
void convertbinary(int num)
{
unsigned short rem;
if (num <= 1)
{
cout << num;
bytearrayin[i]=num;
return;
}
rem = num % 2;
bytearrayin[i]=rem;
i--;
convertbinary(num / 2);
cout << rem;
}
int main()
{
int dec, bin;
cout << "Enter the number : ";
cin >> dec;
cout << "The binary form of " << dec << " is ";
convertbinary(dec);
cout << endl;
for(int i=0;i<16;i++)
{
cout<< bytearrayin[i];
}
cout << " " << endl;
cout << " reverse of byte array is" << endl;
// for(int i=0;int j=8;i<8;j<16;i++;j++)
for(int i=0;i<8;i++)
{
bytearrayout[i] = bytearrayin[i+8];
}
for(int i=8;i<16;i++)
{
bytearrayout[i] = bytearrayin[i-8];
}
for(int i=0;i<16;i++)
{
cout<< bytearrayout[i];
}
cout << " " << endl;
//below will convert the swapped bytes back to decimal
int decimal = 0;
for(int i=0;i<16;i++)
{
decimal = decimal * 2 + bytearrayout[i];
}
bytearrayout[i] = bytearrayin[i-8];
cout << "The decimal equivalent of binary bytearrayout is" << decimal << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.