C++: I need help developing this algorithm and getting it to work (I\'m new to C
ID: 3528835 • Letter: C
Question
C++: I need help developing this algorithm and getting it to work
(I'm new to C++ so please keep it very simple (if statements, while statements and math operators are probably as far as I go)
Heres the outline to the program
- Enter values in 24 hour time (23 32 <- ex)
- Program keeps asking user to input values until the user inputs 99 99 as a value
- The program should keep track of the gap between a valid time and the valid time entered prior to it
- The program should discard any invalid times (besides 99 99) so like 43 43 or 24 00.
- At the end the program should do 4 things: 1. Display number of valid/invalid entries 2. Shortest gap length 3. Longest Gap Length 4. Average Gap Length
So I'm having issues keeping track of gap length between two entries. It only works with consecutive valid entries and I'm not entire sure if the math to calculate gap length is right either. So if you can check that, that'd be great...
--------
//Variable Declaration
int a; //1st hour time entered
int c; //2nd hour time entered
int b; //1st minute time entered
int d; //2nd minute time entered
int z = 0; //invalid entries
int y = 0; //valid entries
int x = 0; //number of back2back pairs
int m = 0; //if m = 0 no data on gap times will be released
int n; //gap time
double o = 0; //total gaps
double p; //average gaps
//enter time
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
//loops question until 99 99 entered
while ((a != 99 && b != 99) && (c != 99 && d != 99)){
//if time is invalid
if (a < 0 || b < 0 || (a >= 24 && a != 99 && b != 99) || (b >= 60 && a != 99 && b != 99)){
cout<<"INVALID TIME"<<endl;
z = z + 1;
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
}
else{
y = y + 1;
a = c;
b = d;
//enter time
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
// if time is invalid
if (a < 0 || b < 0 || (a >= 24 && a != 99 && b != 99) || (b >= 60 && a != 99 && b != 99)){
cout<<"INVALID TIME"<<endl;
z = z + 1;
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
}
else {
//math
m = m + 1;
x = x + 1;
n = (a*60+b) - (c*60+d);
if (n < 0){
n = 24*60+n;
}
o = o + n;
}
}
}
Explanation / Answer
//Variable Declaration
int a; //1st hour time entered
int c; //2nd hour time entered
int b; //1st minute time entered
int d; //2nd minute time entered
int z = 0; //invalid entries
int y = 0; //valid entries
int x = 0; //number of back2back pairs
int m = 0; //if m = 0 no data on gap times will be released
int n; //gap time
double o = 0; //total gaps
double p; //average gaps
//enter time
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
//loops question until 99 99 entered
while ((a != 99 && b != 99) && (c != 99 && d != 99)){
//if time is invalid
if (a < 0 || b < 0 || (a >= 24 && a != 99 && b != 99) || (b >= 60 && a != 99 && b != 99))
{
cout<<"INVALID TIME"<<endl;
z = z + 1; // check the number of entries
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
}//again goes to the loop to check the next entry
// if entry is valid go down
else{
y=y+1;// check number of valid entries
c=a; //copy the last valid time entry
d = b;
//enter time
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
// if time is invalid
if (a < 0 || b < 0 || (a >= 24 && a != 99 && b != 99) || (b >= 60 && a != 99 && b != 99)){
cout<<"INVALID TIME"<<endl;
z = z + 1;
cout<<"Enter 24hr time (99 99 to exit): ";
cin>>a>>b;
}
else {
//math
m = m + 1;
x = x + 1;
n = (a*60+b) - (c*60+d);
if (n < 0){
n = 24*60+n;
}
o = o + n;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.