Cubic Problem

Issue The small program below calculates and presents the volume of a cube with a certain side length. Your task is to rewrite the program so that (at least) three functions are used instead. The following are the requirements for the functions:

    • A function for loading data - note that it shall be without return value.
      void getSide(parameter);
    • A function to calculate and return the calculation result:
      double cubeVolume(parameter);
    • A function for printing the result. The above calculation function should be called from within this function:
      void printVolume(parameter);

    Note! Global variables are not allowed to be used!

    #include < iostream>
    #include < cctype> // toupper

    using namespace std;

    int main()
    {
    double side = 0.0, volume = 0.0;
    char answer = 'Y';

    cout << "Cubes" << endl;
    cout << "=====" << endl << endl;

    do
    {
    // Read side
    cout << "Enter the side length of the cube to be computed: ";
    cin >> side;

    // Calculate volume
    volume = side * side * side;

    // Print volume
    cout << "Volume of cube is: " << volume << " volume units" << endl;

    cout << endl << "One more time (Y/N)? ";
    cin >> answer;
    } while (toupper (answer) == 'Y');

    return 0;
    }

    Fahrenheit to Celsius one more time

    Issue

    Write a program that prompts user to enter a temperature in Fahrenheit, computes equivalent temperature in Celsius and prints on screen, as shown in the run example given below. This program is based on the task Temperature in Step 1, which converts a given temperature in Fahrenheit to Celsius. The conversion will this time take place in a separate function, which returns true if the temperature is greater than (or equal to) 0 degrees C, otherwise it returns false.

    The function for temperature conversion should have two input parameters, first for temperature value in Fahrenheit and second for temperature value in Celsius. The converted value of temperature (from Fahrenheit to Celsius) should be returned via the second parameter. Of course it would have been smarter to use return statement to return the converted temperature value in Celsius, but this time it is a requirement to send data via parameters

    Note! - No reading or printing should be done in the function defined for temperature conversion. The program should be able to do multiple calculations without restarting.

    Test case

    Run example

    Temperature conversion
    ====================
    Enter day temperature in Fahrenheit: 1.0
    -17,2 degrees Celsius.
    One more time (Y/N)? Y

    Enter day temperature in Fahrenheit: 73.5
    23,1 degrees Celsius.
    One more time (Y/N)? N

    Tips

    On the Internet you can certainly find information about how to do the conversion, if you are not already familiar with the procedure or have done a similar task before.

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    By default

    Issue

    There are many features to use in conjunction with parameter transfer. In this task, a default parameter should be used, which is a convenient way to let functions implement default values, where the parameter value is omitted.

    Declare and define a function that calculates x, i.e., value of x raised to the power of y. The function can be named power and should be able to be used with the test program below. The default value to use should be 2.

    Note: The library function pow() in < cmath> may not be used, you shall implement the power function yourself.

    #include < iostream>
    using namespace std;

    // Write the function declaration (prototype) for power here...

    int main()
    {
    for (int i = 0; i < 10; i++)
    cout << power(2, i) << " "; // prints 2 raised to the power of i

    cout << endl;
    cout << power(3) << endl; // will print 9
    cout << power(4) << endl; // will print 16
    cout << power(5) << endl; // will print 25

    return 0;
    }

    // Write the function definition for power here...

    Test case

    Run example

    1 2 4 8 16 32 64 128 256 512
    9
    16
    25

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    Encryption

    Issue

    Write a program that asks for a text and encrypts it by replacing each character with the character placed three steps further on in the alphabet. Use the English alphabet, which consists of the characters 'a' - 'Z'. The program should print the encrypted string and the encryption should be done in a function according to the following function prototype:

    char *encrypt (char *pEncryptedText, const char *pText);

    Here pText is the c-string to be encrypted and pEncryptedText is the string where the encrypted text is saved. As can be seen from the prototype, the original text should be kept unmodified. The return value shall be a pointer to the encrypted text.

    Guidance

    • a is replaced with d, and so on until... Z, which is replaced with c (when the alphabet "ends" - start over from the beginning)
    • It's not that difficult to handle 'A' - 'Z' in the same way. Encrypt these too it if you want to!

    Test case

    Run example

    Enter a text to encrypt: donald lives on an island
    grqdog olyhy rq dq lvodag
    One more time (Y/N)? Y

    Enter a text to encrypt: abc xyz
    def abc
    One more time (Y/N)? N

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    Heights

    Issue One way to estimate the height of a child is to use the following formula, which uses the height of the parents:

    Hmale_child = ((Hmother 13/12) + Hfather) / 2
    Hfemale_child = ((Hfather 12/13) + Hmother) / 2

    Write a function that takes as input parameters the gender of the child, height of the mother in inches, and height of the father in inches, and outputs the estimated height of the child in inches. Embed your function in a program that allows you to test the function over and over again until telling the program to exit.

    The user should be able to input the heights in feet and inches, and the program should output the estimated height of the child in feet and inches. Use the integer data type to store the heights.

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    Discount

    Issue

    The following program calculates what a customer will pay, depending on different discount rates. The input will be the amount paid by the customer. The output will be how big discount the customer will get (in SEK) and the remaining amount the customer shall pay when the f. All output data should be rounded off to the nearest krona. Complete the program with what's missing according to the comments in the code. You should understand what is expected to happen in the program, by analyzing the existing code (which must not be changed!). The different discount rates will be found in the table below. Amounts on the boundaries are given the higher discount rate.

    Sum, Discount
    0 - 500, 0%
    500 - 1000, 5%
    1000 - 5000, 10%
    5000 -, 15%
    // Include the standard libraries needed!
    using namespace std;

    // Remember to declare all functions (prototypes)!
    void print(double sum, double discountkr);

    int main()
    {
    double sum = 0.0, discountKr = 0.0;
    char answer;
    do {
    system("CLS"); // Clear screen
    cout << "Discount" << endl;
    cout << "======" << endl << endl;
    load(sum);

    // To read the input sum from user - complete this function call,
    // by writing the necessary function definition!
    discountKr = sum * discount(sum) / 100;

    // To calculate the discount - complete the function call ("discount(sum)")
    // with a function definition

    sum = sum - discountKr;
    print(sum, discountkr);
    cout << endl << "One more time (Y/N)?",
    cin >> answer;
    }
    while (toupper(answer == 'Y');

    return 0;
    }

    // Add necessary function definitions here
    void print(double sum, double dkr)
    {
    // Write code for the output! The result should look like run example below, // that is, with fixed number notation, right alignment and some padding character.
    // (Note: with manipulators – that shall be reset to default after use!)
    }

    Test case

    Run example

    Discount
    ========

    Enter the sum: 1324
    ------------------------------
    Discount : .....132.00 kr
    Net sum : ....1192.00 kr
    ------------------------------
    One more time (Y/N)?

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    A lot of fractions

    Issue

    You will make a small "calculator" application, that can handle the four basic arithmetic operations for fractions. The user shall enter the values for two fractions, and then get the four arithmetic operations (addition, subtraction, multiplication, and division) presented on the screen. A menu for choice of arithmetic operations should be presented to choose among the arithmetic operations and the user can make repeated choices.

    The program should have at-least four functions - one for each arithmetic operation, and use a structure to represent each fraction (numerator and denominator).

    The program should be able to do multiple calculations without restarting, and result of the arithmetic operations should be presented by reducing fractions to lowest terms, for example 9/6 should be reduced to 3/2, 18/4 should be reduced to 9/2.

    Tips

    Following arithmetic operations applies to fractions:

    (t1/n1) + (t2/n2) = (t1 * n2 + t2 * n1) / (n1 * n2)

    (t1/n1) - (t2/n2) = (t1 * n2 - t2 * n1) / (n1 * n2)

    (t1/n1) * (t2/n2) = (t1*n1) * (t2*n2)

    (t1/n1) / (t2/n2) = (t1/n2) * (n1*t2)

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.

    Date functions

    This application is a "1-3 points task with three steps, a-c, each step having one point. It is up to you either complete all the three steps or just the first one or two steps. However, the steps must be performed in the order a to c, as each subsequent program part is built on the previous one. 3

    Step a)

    Write a program that reads a year, and investigate whether this year is a leap year or not. The investigation should be put in a function leapYear(), which takes the year (integer) as an argument and returns true or false depending on the outcome. No printing may be done in the function, which is to be called by the main program as follows:

    cin >> year;
    if (leapYear (year))
    cout << "This is a leap year!" << endl;
    else
    cout << "This is not a leap year!" << endl;

    Tips

    In the Gregorian calendar three criteria must be taken into account to identify leap years:

    • The year can be evenly divided by 4;
    • If the year can be evenly divided by 100, it is NOT a leap year, unless;
    • The year is also evenly divisible by 400. Then it is a leap year.

    This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

    Test case

    Run example

    Enter a year: 2000
    This is a leap year!

    Enter a year: 1900
    This is not a leap year!

    Step b)

    Complement the program in step a) with the function int daysInMonth(int year, int month); which returns the number of days in a given month. Year and month are sent as arguments to the function. The function leap Year() above should be used to find if it's a leap year or not to compute number of days for the month of February.

    Test case

    Run example

    Enter year and month: 2003 4
    The number of days in this month is 30.

    Tips

    Remember the school rhyme?

    "Thirty days have November, April, June and September. February has twenty-eight alone, all the rest have thirty-one. Excepting leap year, that's the time, when February's days are twenty-nine".

    Issue c)

    Write a function tomorrow() which, given a date as an integer (via parameter) on the form 20030401, returns the date for tomorrow on the same form. Change the main application to get a dialogue according to the test case below.

    Test case

    Run example

    Enter a date on the form YYYYMMDD: 20031231
    Tomorrow it is: 20040101

    Enter a date on the form YYYYMMDD: 20030228
    Tomorrow it is: 20030301

    Tips

    Split the date in year, month and day with / and %. Then increase day and adjust for any transition to new month or year. The daysInMonth() function in step b) is useful to complete this task.

    Presentation

    The task is to be solved as follows:

    • A fully working program that meets the specified criteria.
    • A program code that is well written and easy to read, according to the coding standard specified in step 3.
    Academic Honesty!
    It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
    Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.