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

Hi, I need help for various labs just to modify a little bet of the code since I

ID: 3889593 • Letter: H

Question

Hi, I need help for various labs just to modify a little bet of the code since I have some confusion. For Lab 1 I need to be more precisely and after 3 hours I need to charge 50 cents more per hour. If it is 3.25 I need to charge less than 50 cents since it less than an hour and isplay the exactly hours that the car parked. For lab 2, I do not need to use setprecision, fixed, or cout.setf. Input values with more than 6 digits and they will be rounded (half-evenly) to 6 digits. For lab 3, the requirement says that If you use doubles, you may use setprecision(15). Do NOT use setprecision if you use floats. But do NOT use fixed or cout.setf. Expect input to be in the range >=0 and <=32k. You do NOT have to validate this range -- just expect users to stick to this range of input. Lastly, for lab 4, I need to expect the input to be from 1 to 6 digits in length (inclusive). The link for the labs is below. Thanks.

https://drive.google.com/open?id=0B8jPGepnXF2cQlZGZTBTMEMwdXc

Explanation / Answer

LAB 1: Here we need to just calculate the difference between parking hours and 3 and need to multiply that difference with 0.5 as 50 cents is the charge for each hour after 3 hours. Below will be the modified function calculateCharges:

double calculateCharges(double hours) {
if (hours < 3)
return 2.00;
else {
return 2.00 + (((hours) - 3) * 0.5);
}
}

Everything else if perfectly fine in the program which is printing the result precise to the two digit after decimal.

LAB 2: Here we need to round the number to the nearest whole number like 2.3 will become 2 after round and 2.8 will become 3 after round. We have a direct function round for that in C++ libarary so below will be the program:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <cmath>

//function templates
double roundNumber(double);

int main() {
//print my name and this assignment's title
cout << "LAB 2 ";
cout << "Programmer: ";
cout << "Editor(s) used: ";
cout << "Compiler(s) used: ";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  //variables
  char buf[100];
  double num = 0;

  while (true)
  {

      //prompt user for input
      cout << "Enter number to round (q or Q to quit): ";
      cin >> buf; num = atof(buf);

      //quit program if user entered a q
      if (buf[0] == 'q' || buf[0] == 'Q') break;

      cout << "Entered number: " << num << endl;
      cout << "Rounded number: " << roundNumber(num) << endl;
    }
}


double roundNumber(double number){
  return round(number)

}

LAB 3: This problem is also similar to LAB 2 problem where we have to round the number here the difference is we have to round the number to tenths place, hundredths place and thousandsths place which we can achieve by multiplying the number with 10,100 and 1000 respectively and then divide by the same number. for example:

0.843 is the number then (0.843*10) = 8.43 and if we round this then it will become 8 and if we divide this rounded number by 10 then (8/10) = 0.8 we will get in the same way we can go for 100 and 1000 also. So after modifications your program will look like below:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <cstdlib>
#include <cmath>

//Preprocessor for the decimal placement.
#include <iomanip>
using std::setprecision;

//function templates
double roundToInteger(double);
double roundToTenths(double);
double roundToHundredths(double);
double roundToThousandsths(double);

int main()
{
//print my name and this assignment's title
  cout << "Lab 3 ";
  cout << "Programmer: ";
  cout << "Editor(s) used: ";
  cout << "Compiler(s) used: ";
  cout << "File: " << __FILE__ << endl;
  cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;

  //variables
  char buf[100];
  double num = 0;

  while (true)
  {

      //prompt user for input
      cout << "Enter number to round (q or Q to quit): ";
      cin >> buf; num = atof(buf);

      //quit program if user entered a q
      if (buf[0] == 'q' || buf[0] == 'Q') break;

      cout << "Entered number: " << num << endl;
      cout << "Rounded to nearest integer: " << roundToInteger(num) << endl;
      cout << "Rounded to nearest tenth: " << roundToTenths(num) << endl;
      cout << "Rounded to nearest hundredth: " << roundToHundredths(num) << endl;
      cout << "Rounded to nearest thousandth: " << roundToThousandsths(num) << endl;
  }
}

double roundToInteger(double number)
{
  return round(number);
}

double roundToTenths(double number)
{
  return round(number * 10) / 10;
}

double roundToHundredths(double number)
{
  return round(number * 100) / 100;
}

double roundToThousandsths(double number)
{
  return round(number * 1000) / 1000;
}

LAB 4: Here we need to reverse the number which can be done by dividing the number by 10 until we get the number less than 0 and we can use the remainder to create the reversed number. Below is the program to to that:

#include <iostream>
#include <cstdlib>
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//function templates
int reverseNum(int);

int main()
{
//variables
  char buf[100];
  int num, num1, len = 0, temp, len1 = 0, ret;

  while (true)
  {
      //prompt user for input
      cout << "Enter the number to performreverse (q or Q to quit): ";
      cin >> buf;
      num = num1 = atoi(buf);
      while (num1 > 0) //find the length of the number
      {
          num1 = num1 / 10;
          len++;
      }

      //quit program if user entered a q
      if (buf[0] == 'q' || buf[0] == 'Q') break;

      cout << "Entered number: " << num << endl;
      ret = reverseNum(num); //ret gets the return type from reverseNum function
      temp = ret;

      while (temp > 0) //find the length of the reversed number
      {
          temp = temp / 10;
          len1++;
      }

      cout << "Reversed number With leading zeros : ";

      for (int i = 0; i<(len - len1); i++)
          cout << 0;

      cout << ret << endl;
      cout << "Reversed number: " << ret << endl;
  }
}

int reverseNum(int number)
{
  int reversedNum = 0;
  int remainder = 0;
  int zeros = 0;
  bool firstTime = true;

  while (number > 0)
  {
      remainder = number % 10;

      //If remainder is 0 and the first non zero number is not yet encountered
      if (remainder == 0 && firstTime)
      {
          zeros++;
      }

      //If first non zero digit is encounrered, zeros encountered later should'nt be counted
      else
      {
          firstTime = false;
      }

      reversedNum = reversedNum * 10 + remainder;
      number /= 10;

  }

  return reversedNum;

}

Please put your queries in comment section if you have any doubt in this.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote