In: Computer Science
Computer Architecture and Organization(c++)
Write a C++ program that prompts the user to enter a hexadecimal value, multiplies it by ten, then displays the result in hexadecimal. Your main function should
a) declare a char array
b) call the readLn function to read from the keyboard,
c) call a function to convert the input text string to an int,
d) multiply the int by ten,
e) call a function to convert the int to its corresponding hexadecimal text string,
f) call writeStr to display the resulting hexadecimal text string.
the hint i was given was:
#include <iostream>
#include <cstring>
using namespace std;
char decToHex(int dec)
{
char hex;
switch (dec)
{
case 0: hex = '0';
break;
case 1: hex = '1';
break;
case 2: hex = '2';
break;
case 3: hex = '3';
break;
case 4: hex = '4';
break;
case 5: hex = '5';
break;
//
//
case 10: hex = 'A';
break;
case 11: hex = 'B';
break;
//
//
case 15: hex = 'F';
}
return hex;
}
void reverseArray(char arr[])
{
int j = strlen(arr) - 1;
int i = 0;
while ( i < j )
{
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
void intToHex(int intVal, char hexArr[])
{
int remainder;
int i = 0;
while (intVal > 0)
{
remainder = intVal % 16;
hexArr[i] = decToHex(remainder);
intVal = intVal / 16;
i++;
}
reverseArray(hexArr);
}
int hexToDec(char hex)
{
int dec;
switch (hex)
{
case '0': dec = 0;
break;
case '1': dec = 1;
break;
case '2': dec = 2;
break;
case '3': dec = 3;
break;
case '4': dec = 4;
break;
case '5': dec = 5;
break;
//
//
case 'A':
case 'a': dec = 10;
break;
case 'B':
case 'b': dec = 11;
break;
//
//
case 'F':
case 'f': dec = 15;
}
return dec;
}
int main()
{
char hexaInput[17];
char hexaResult[17] = { '\0' };
cout << "Enter hexadecimal: ";
cin >> hexaInput;
cout << "You typed: " << hexaInput << "\n";
int count = strlen(hexaInput);
double powerVal = 0.0;
int decValue = 0;
for (int i = count - 1; i >= 0 ; i--)
{
cout << hexToDec(hexaInput[i]) << "\n";
decValue += hexToDec(hexaInput[i]) * pow(16, powerVal);
powerVal++;
}
cout << "Decimal value is " << decValue << "\n";
intToHex(decValue*10, hexaResult);
cout << "For decimal " << decValue*10 << " Hexadecimal is: " << hexaResult <<
endl;
return 0;
}
#include <iostream>
#include<cmath>
#include <cstring>
#include<ctype.h>
using namespace std;
void reverseArray(char *hexArr,int start,int end)
{
while (start < end)
{
char temp = hexArr[start];
hexArr[start] = hexArr[end];
hexArr[end] = temp;
start++;
end--;
}
}
void decToHex(int intVal, char hexArr[])
{
int remainder;
int i = 0;
while (intVal != 0)
{
remainder = intVal % 16;
if (remainder < 10)
hexArr[i++] = remainder + 48; // numbers in hex
else
hexArr[i++] = remainder + 55; // 10 +55 = 65 = A
intVal = intVal / 16;
}
reverseArray(hexArr,0,i-1);
}
int hexToDec(char hex)
{
char ch = toupper(hex);
if (ch >= 'A' && ch <= 'F')
return 10 + ch - 'A';
else
return ch - '0';
}
int main()
{
char hexaInput[17];
char hexaResult[17] = { '\0' };
cout << "Enter hexadecimal: ";
cin >> hexaInput;
cout << "You typed: " << hexaInput << "\n";
int count = strlen(hexaInput);
int powerVal = 0;
int decValue = 0;
for (int i = count - 1; i >= 0 ; i--)
{
decValue += hexToDec(hexaInput[i]) * pow(16,
powerVal);
powerVal++;
}
cout << "Decimal value is " << decValue << "\n";
decToHex(decValue*10, hexaResult);
cout << "For decimal " << decValue*10 << " Hexadecimal is: " << hexaResult << endl;
return 0;
}
/* OUTPUT */