I need assistance with this program. I do not understand where to begin; after g
ID: 3564562 • Letter: I
Question
I need assistance with this program. I do not understand where to begin; after getting the user to input a decimal in base 10 I can't figure out a structure to get the program to convert. Any help would be greatly appreciated, as well as a description of what's going on if you write a program that accomplishes the task.
Write a C++ Console Application that will convert a decimal (base 10) integer number to hexadecimal (base 16). Write a structured program that uses a separate function for each operation. The hexadecimal number shall be an array of char type data.Explanation / Answer
Modified answer: (without using library functions and working in c++)
#include<iostream>
#include <math.h>
#include <cstring>
#include <algorithm>
using namespace std;
void decimalToHex(int , char []);
void getHexaValue(int ,int ,char []);
int main()
{
char hexavalue[20];
int decimalValue;
cout<<"Enter decimal number: ";
cin>>decimalValue;
decimalToHex(decimalValue,hexavalue);
return 0;
}
void decimalToHex(int decimal, char hex[])
{
int index=0,remainder;
while (decimal!=0)
{
remainder=decimal%16;
getHexaValue(index,remainder,hex);
index=index+1;
decimal=decimal/16;
}
hex[index]='';
for(int i =0; i<index/2; i++)
{
char temp = hex[i];
hex[i] = hex[index-1-i];
hex[index-1-i] = temp;
}
cout<<"Hexadecimal Number:"<<hex<<endl;
}
void getHexaValue(int index,int remainder,char hex[])
{
switch(remainder)
{
case 10:
hex[index]='A';
break;
case 11:
hex[index]='B';
break;
case 12:
hex[index]='C';
break;
case 13:
hex[index]='D';
break;
case 14:
hex[index]='E';
break;
case 15:
hex[index]='F';
break;
default:
hex[index]=remainder+'0';
break;
}
}
There is an inbuilt function for conversion in c++ for converting.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char response;
do
{
cout << "Enter positive integer to convert to Hexadecimal: ";
int number;
cin >> number;
cout << "Hexadecimal representation of "<< number << " is "
<< hex << uppercase << number << dec << ' '
<< " More (Y or N)? ";
cin >> response;
}
while (response == 'Y' || response == 'y');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.