Problem # 1 Write a program to convert the time from 24-hour notation to 12-hour
ID: 3545575 • Letter: P
Question
Problem # 1
Write a program to convert the time from 24-hour notation to 12-hour notation and vice versa. Your program must be menu driven, giving the user the choice of converting the time between the two notations. Furthermore, your program must contain at least the following function: a function to convert the time from 24-hour notation to 12-hour notation, a function to convert the time from 12-hour notation to 24-hour notation, a function to display the choices, function(s) to get the input, and function(s) to display the results. (For 12-hour time notation, your program must display AM or PM.)
Answer:
1.#include <iostream>
2.using namespace std;
3.void input(int& hours, int& minutes);
4.void output(int hours, int minutes, char type);
5.int convertTo12Hour(int hours, char& type);
6.int main() {
7.int hours;
8.int minutes;
9.char type;
10.char answer;
11.do
12.{
13.input(hours, minutes);
14.hours = convertTo12Hour(hours, type);
15.output(hours, minutes, type);
16.cout << "Perform another calculation? (y/n): ";
17.cin >> answer;
18.} while ((answer == 'Y') || (answer == 'y'));
19.return 0;
20.}
21.void input(int& hours, int& minutes) {
22.cout << "Enter the hours for the 24 hour time: ";
23.cin >> hours;
24.cout << "Enter the minutes for the 24 hour time: ";
25.cin >> minutes;
26.}
27.//
28.// Displays a time in 12 hour notation
29.//
30.void output(int hours, int minutes, char type) {
31.cout << "The time converted to 12 hour format is: " << hours << ":";
32.//
33.// special handling for leading 0s on the minutes
34.//
35.cout.width(2);
36.cout.fill('0');
37.cout << minutes;
38.//
39.if (type == 'A')
40.cout << " A.M." << endl;
41.else
42.cout << " P.M." << endl;
43.}
44.void Convert( int& hours )
45.{
46.hours = hours % 12;
47.}
48.if( hours < 12)
49.{
50.pm = 'A';
51.}
52.else
53.{
54.hours -= 12;
55.pm = 'P';
56.}
It is showing error because may be I was not able to put that if statement inside any function. Please help me to find out the error sand complete the program with corrected code.
Problem # 2
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function.
Please comments your code-include pre-condition and post-condition for all functions that will help me to understand.
Problem #3
Determine whether the following array declarations are valid.
Explanation / Answer
#1
#include <iostream>
using namespace std;
void input24(int& hours, int& minutes);
void input12(int& hours, int& minutes,char& type);
void output12(int hours, int minutes, char type);
void output24(int hours, int minutes);
void convertTo12Hour(int& hours, char& type);
void convertTo24Hour(int& hours, char& type);
int main() {
int hours;
int minutes;
char type;
int ch;
char answer;
do
{
cout<<" Time Machine ";
cout<<" 1.12Hours To 24Hours";
cout<<" 2.24Hours To 12Hours";
cout<<" 3.Exit";
cout<<" Enter Your Choice:";
cin>>ch;
if(ch==1)
{
input12(hours, minutes,type);
convertTo24Hour(hours, type);
output24(hours, minutes);
}
else if (ch==2)
{
input24(hours, minutes);
convertTo12Hour(hours, type);
output12(hours, minutes, type);
}
else
{
break;
}
cout << " Perform another calculation? (y/n): ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
return 0;
}
void input24(int& hours, int& minutes)
{
cout << " Enter the hours for the 24 hour time: ";
cin >> hours;
cout << " Enter the minutes for the 24 hour time: ";
cin >> minutes;
}
void input12(int& hours, int& minutes,char& type)
{
cout << " Enter the hours for the 12 hour time: ";
cin >> hours;
cout << " Enter the minutes for the 12 hour time: ";
cin >> minutes;
cout << " Enter the AM/PM for the 12 hour time(A-AM/P-PM: ";
cin >> type;
}
//
// Displays a time in 12 hour notation
//
void output12(int hours, int minutes, char type)
{
cout<<" The time converted to 12 hour format is:";
if(type == 'P')
{
if(minutes < 10) cout << hours << ":0" << minutes << " P.M.";
else cout << hours << ":" << minutes << " P.M.";
}
else
{
if(minutes < 10) cout << hours << ":0" << minutes << " A.M.";
else cout << hours << ":" << minutes << " A.M.";
}
}
void output24(int hours, int minutes)
{
cout<<" The time converted to 12 hour format is:";
if(minutes < 10)
cout << hours << ":0" << minutes << " P.M.";
else cout << hours << ":" << minutes << " P.M.";
}
void convertTo12Hour(int& hours, char& type)
{
if( hours < 12)
{
type = 'A';
}
else
{
hours -= 12;
type = 'P';
}
}
void convertTo24Hour(int& hours, char& type)
{
if (type=='P')
{
hours=hours+12;
}
}
#2
#include <iostream>
using namespace std;
int main()
{
int number=0, min=0, max=0;
int arr[5];
cout<<"Enter 5 numbers: ";
for(int i=0; i<5;i++)
{
cout<<" Enter number "<<i+1<<": ";
cin>>arr[i];
}
min=arr[0];
max=arr[0];
int minindex = 0;
for(int j=0; j<5;j++)
{
if(min>arr[j])
{
min=arr[j];
minindex = j;
}
}
minindex += 1;
cout<<" Min number index: "<<minindex;
return 0;
}
#3
int a[5] = {0, 4, 3, 2, 7};//Correct Statement
int b[10] = {0, 7, 3, 12];// Array initialization must be start with { and end with }
int b[10] = {0, 7, 3, 12};//Corrected Statment;
int c[7] = {12, 13, 14, 16, , 8};//You cannot initialize int array will NULL value
int c[7] = {12, 13, 14, 16,8};//Corrected Statement
double lengths[] = {12.7, 13.9, 18.75, 20.78};
char name[8] = {'S','a','m','a','n','t','h','a'};//You can either declare char array as this statement
char name1[0]={"Samantha"};//Or you can declare characater string as this statement
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.