Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Write a program that uses a recursive function to convert a number in dec

ID: 3581479 • Letter: P

Question

Part 1

Write a program that uses a recursive function to convert a number in decimal to a given base b, where b is 2, 8, or 16. Your program should prompt the user to enter the number in decimal and the desired base.

Test your program on the following data:

9098 and base 8

692 and base 2

753 and base 16

Sample output:

Please enter a number? 4598

Please enter a base between 2 and 16? 8

The decimal number 4598 is equal to 10766 in base 8.

Please enter a number? 25354

Please enter a base between 2 and 16? 16

The decimal number 4598 is equal to 630A in base 16.

Part 2

Write a program that reads time and date of birth in numeric form such as

8-27-1980 10:56:59 pm or 8-27-1980 22:56:59.

If the time is in 12-hour notation and the date is in numeric form. The program then outputs the time in 24-hour notation and the date of birth in the form: August 27th, 1980 hh:mm:ss.

If the time is in 24-hour notation and the date is in numeric form. The program then outputs the time in 12-hour notation and the date of birth in the form: August 27th, 1980 hh:mm:ss am or pm.

Your program must contain at least 5 exception classes: InvalidHour, InvalidMinute, InvalidSec, InvalidDay, and InvalidMonth.

If an invalid value for hour, minute, second, day or month was read, then the program should throw and catch the correct matching object. Don't worry about the Leap Year. An example may look like this:

8-27-1980 10:56:59 pm or 8-27-1980 22:56:59.

You were born on August 27th, 1980 at 22:56:59

If invalid information was read, make sure you display a message stating that error, for example:

8-32-1980 15:61:00 am

will result into:

Invalid day:        32

Invalid hour:       15

Invalid minute:     61

Invalid timeperiod: am

Please upload the following:

The class .cpp file

The main program

The class .h file

Output File

Explanation / Answer

#include<iostream>
#include<stdlib.h>
using namespace std;
//Convert Decimal to binary
void decToBinary( const int num1 )
{
//Checks if the numebr is zero
if( num1 == 0 )
{
return;
}
//Recursively calls the function by passing quotient to the function
decToBinary( num1/2 );
//Displays the remainder
cout << num1 % 2;
}
// Convert Decimal to Octal
int decToOctal(int num)
{
//Checks if the numebr is zero
if( num == 0 )
{
return 0;
}
//Recursively calls the function by passing quotient to the function
decToOctal( num/8 );
//Displays the remainder
cout << num % 8;
}

// Convert Decimal to Hexadecimal
string decToHex(int decimalNumber, string buffer)
{
//Find out the remainder by dividing the number by 16
int x = decimalNumber % 16;
//Convert the remainder to character from
char x_to_char = x + '0';
//If the given number is zero
if (decimalNumber == 0)
{
return buffer;
}
//Checks the character form of the remainder
switch (x)
{
case 10: buffer = 'A' + buffer; break;
case 11: buffer = 'B' + buffer; break;
case 12: buffer = 'C' + buffer; break;
case 13: buffer = 'D' + buffer; break;
case 14: buffer = 'E' + buffer; break;
case 15: buffer = 'F' + buffer; break;
default: buffer = x_to_char + buffer;
}
//Returns the hexadecimal string
return decToHex(decimalNumber / 16, buffer);
}
//Displays menu and accept the choice from the user
int menu()
{
int ch;
cout<<" 2) Base 2 (Binary)";
cout<<" 8) Base 8 (Octal)";
cout<<" 16) Base 16 (Hexadecimal)";
cout<<" 0) Exit";
cout<<" Please enter a base between 2 and 16? ";
cin>>ch;
return ch;
}
//Main function
int main()
{
string ss;
int no, choice;
//Loops till user choice
do
{
choice = menu();
//Match the choice entered by the user
switch(choice)
{
//Base 2
case 2:
//Accepts a number from the user
cout<<" Please enter a number? ";
cin>>no;
cout<<" The decimal number "<<no<<" is equal to ";
decToBinary(no);
cout<<" in base "<<choice;
break;
//Base 8
case 8:
//Accepts a number from the user
cout<<" Please enter a number? ";
cin>>no;
cout<<" The decimal number "<<no<<" is equal to ";
decToOctal(no);
cout<<" in base "<<choice;
break;
//Base 16
case 16:
//Accepts a number from the user
cout<<" Please enter a number? ";
cin>>no;
cout<<" The decimal number "<<no<<" is equal to "<<decToHex(no, ss)<<" in base "<<choice;
ss= "";
break;
//Exit
case 0:
exit(0);
//Invalid choice
default:
cout<<" ERROR: Invalid choice!";
}
}while(1);
}

Output:


2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 8

Please enter a number? 9098

The decimal number 9098 is equal to 21612 in base 8
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 2

Please enter a number? 692

The decimal number 692 is equal to 1010110100 in base 2
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 16

Please enter a number? 753

The decimal number 753 is equal to 2F1 in base 16
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 8

Please enter a number? 4598

The decimal number 4598 is equal to 10766 in base 8
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 16

Please enter a number? 25354

The decimal number 25354 is equal to 630A in base 16
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 12

ERROR: Invalid choice!
2) Base 2 (Binary)
8) Base 8 (Octal)
16) Base 16 (Hexadecimal)
0) Exit
Please enter a base between 2 and 16? 0

Part 2:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
//Class MyDate defined
class MyDate
{
//Data member for date, month and year
int date, month, year;
//Data member for hour, minute, and second
int hh, mm, ss;
//Data member for am / pm
string ampm;
public:
//Default constructor
MyDate()
{
date = month = year = 0;
hh = mm = ss = 0;
ampm = " ";
}
//Overloading << operator to Display DOB
friend ostream & operator << (ostream &out, MyDate &m)
{
string ss;
//Sets the month in String format
switch(m.month)
{
case 1:
ss = "January";
break;
case 2:
ss = "February";
break;
case 3:
ss = "March";
break;
case 4:
ss = "April";
break;
case 5:
ss = "May";
break;
case 6:
ss = "June";
break;
case 7:
ss = "July";
break;
case 8:
ss = "August";
break;
case 9:
ss = "September";
break;
case 10:
ss = "October";
break;
case 11:
ss = "November";
break;
case 12:
ss = "December";
break;
}
//If hour is in 24 hour format
if(m.hh > 12)
{
m.hh = m.hh - 12;
m.ampm = " pm";
}
else
m.ampm = " am";
//Returns the DOB format
out<<ss<<" "<<m.date<<" th, "<<m.year<<" "<<m.hh<<" : "<<m.mm<<" : "<<m.ss<<m.ampm;
return out;
}
//Overloading >> operator to Accept DOB
friend istream & operator >> (istream &in, MyDate &md)
{
cout<<" Enter Month: ";
in>>md.month;
fflush(stdin);
//Validates month
if(md.month >= 1 && md.month <= 12)
{
cout<<" Enter Date: ";
in>>md.date;
fflush(stdin);
//Validates Date
if(md.date >= 1 && md.date <= 31)
{
cout<<" Enter Year: ";
in>>md.year;
cout<<" Enter Hour: ";
in>>md.hh;
if(md.hh <= 12)
{
cout<<" Enter am / pm: ";
cin>>md.ampm;
}
fflush(stdin);
//Validates Hour
if(md.hh >= 1 && md.hh <= 24)
{
cout<<" Enter Minute: ";
in>>md.mm;
fflush(stdin);
//Validates Minute
if(md.mm >= 0 && md.mm <= 60)
{
cout<<" Enter Second: ";
in>>md.ss;
//Validates Second
if(md.ss >= 0 && md.ss <= 60)
return in;
else
cout<<" Invalid Second: ";
}
else
cout<<" Invalid Minute: ";
}
else
cout<<" Invalid hour: ";
}
else
cout<<" Invalid Date: ";
}
else
cout<<" Invalid Month: ";
exit(0);
}
};
int main()
{
//Creates an object of MyDate class
MyDate st, en;
cout<<" Enter Your Date of birth (8-27-1980 10:56:59 pm or 8-27-1980 22:56:59): ";
cin>>st;
cout<<" DOB: ";
cout<<st;
}

Output 1:

Enter Your Date of birth (8-27-1980 10:56:59 pm or 8-27-1980 22:56:59):
Enter Month: 10

Enter Date: 12

Enter Year: 2003

Enter Hour: 18

Enter Minute: 11

Enter Second: 45

Date: October 12 th, 2003 6 : 11 : 45 pm