Question

In: Computer Science

#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...

#include <iostream>

using namespace std;

int main()

{

    int hour;

    int min;

    for (hour = 1; hour <= 12; hour++)

    {

        for (min = 0; min <= 59; min++)

        {

            cout << hour << ":" << min << "AM" << endl;

        }

    }

      return 0;

}

1.      Type in the above program as time.cpp. Add a comment to include your name and date.

Compile and run.

2.      What is the bug or logic error in the above program? Add the lines to fix it.

3.      This is an example of ____________ for loops.

4.      How many total times does the outer loop execute?

5.      How many total times does the inner loop execute?

6.      Turn in a copy of the final program and output – just the first and last hours are sufficient

Solutions

Expert Solution

2) In the program there is a logical error it prints 12:00 Am after 11:59 Am but after 11:59 Am 12:00 pm comes

so i added if condition.

Code:

#include <iostream>
using namespace std;
int main()
{
    int hour;
    int min;
    for (hour = 1; hour <= 12; hour++)
    {
        for (min = 0; min <= 59; min++)
        {
           if(hour!=12){
               cout << hour << ":" << min << "AM" << endl;
           }
           else
           {
               cout << hour << ":" << min << "PM" << endl;
           }
        }
    }
      return 0;
}

3)This is a example of nested for loops.

4) 12 times outer loop executes.

5)60 times inner loop execute

6)output for first hour:

Output for last hour:


Related Solutions

#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool a = true; bool b= true;    //Print the Conjunction function cout<<"\n\nConjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(P∧Q)" <<endl; cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl; cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl; cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl; cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;    //Print the Disjunction function cout<<"\n\nDisjunction Truth Table -"<<endl; cout<< "\nP\tQ\t(PVQ)" <<endl; cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled          &nbsp
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled             float hours;   //time in hours             float milesPerHour; //calculated miles per hour             cout << "Please input the Miles traveled" << endl;             cin >> miles;             cout << "Please input the hours traveled" << endl;             cin >> hours;                         milesHours = miles / hours; cout << fixed << showpoint << setprecision(2);             cout << "Your speed is " <<...
9. #include <fstream> #include <iostream> using namespace std; int main() {     float bmi;     ifstream...
9. #include <fstream> #include <iostream> using namespace std; int main() {     float bmi;     ifstream inFile;     inFile.open("bmi.txt");     while (!inFile.eof())       {          inFile >> bmi;          if( bmi < 18.5)           {               cout << bmi << " is underweight " ;           }         else if( bmi >= 18.5 && bmi <= 24.9)           {               cout << bmi << " is in normal range " ;           }         else if( bmi >= 25.0 &&...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT