1
answer
1
watching
207
views

Programming Problem

Write a program that displays the terms from the following formula in a 2D table, where n ≥ 2:

For example, the first term when i = 2 and j = 1 is .

A term computed from the combination (i, j) is displayed in the table at row i and column j. See the sample runs below. The program prompts the user for the value n. It also prompts the user for a value to match on. Finally, the program computes and displays how many terms are equal to the value to match on, the sum of all of the terms, and the sum of the terms when i = n, i.e. the terms in the last row of the table.

Sample Runs

The following sample runs (Develop mode) illustrate program behavior (user input is in bold):

Test 1 (Note: here i = 2 and j = 1) > run Enter n: 2 Enter match value: 0.0001 i | j -> 1 i: 2 0.3333 The value 0.0001 appears 0 time(s). The sum of all values is 0.3333333333 The sum of the values in the last row (i = 2) is 0.3333333333 Test 2 > run Enter n: 2 Enter match value: 0.3333 i | j -> 1 i: 2 0.3333 The value 0.3333 appears 1 time(s). The sum of all values is 0.3333333333 The sum of the values in the last row (i = 2) is 0.3333333333 Test 3 > run Enter n: 0 Value must be between [2, 99]. Enter n: 1 Value must be between [2, 99]. Enter n: -2 Value must be between [2, 99]. Enter n: 100 Value must be between [2, 99]. Enter n: 150 Value must be between [2, 99]. Enter n: 3 Enter match value: 0.00001 Value must be between [0.0001, .9999]. Enter match value: 1.0 Value must be between [0.0001, .9999]. Enter match value: 0.5 i | j -> 1 2 i: 2 0.3333 i: 3 0.5000 0.2000 The value 0.5000 appears 1 time(s). The sum of all values is 1.0333333333 The sum of the values in the last row (i = 3) is 0.7000000000 Test 4 > run Enter n: 3 Enter match value: 0.3333 i | j -> 1 2 i: 2 0.3333 i: 3 0.5000 0.2000 The value 0.3333 appears 1 time(s). The sum of all values is 1.0333333333 The sum of the values in the last row (i = 3) is 0.7000000000 Test 5 > run Enter n: 5 Enter match value: 0.6667 i | j -> 1 2 3 4 i: 2 0.3333 i: 3 0.5000 0.2000 i: 4 0.6000 0.3333 0.1429 i: 5 0.6667 0.4286 0.2500 0.1111 The value 0.6667 appears 1 time(s). The sum of all values is 3.5658730159 The sum of the values in the last row (i = 5) is 1.4563492063 Test 6 > run Enter n: 6 Enter match value: 0.3333 i | j -> 1 2 3 4 5 i: 2 0.3333 i: 3 0.5000 0.2000 i: 4 0.6000 0.3333 0.1429 i: 5 0.6667 0.4286 0.2500 0.1111 i: 6 0.7143 0.5000 0.3333 0.2000 0.0909 The value 0.3333 appears 3 time(s). The sum of all values is 5.4044011544 The sum of the values in the last row (i = 6) is 1.8385281385 Test 7 > run Enter n: 7 Enter match value: 0.2 i | j -> 1 2 3 4 5 6 i: 2 0.3333 i: 3 0.5000 0.2000 i: 4 0.6000 0.3333 0.1429 i: 5 0.6667 0.4286 0.2500 0.1111 i: 6 0.7143 0.5000 0.3333 0.2000 0.0909 i: 7 0.7500 0.5556 0.4000 0.2727 0.1667 0.0769 The value 0.2000 appears 2 time(s). The sum of all values is 7.6262737263 The sum of the values in the last row (i = 7) is 2.2218725719 Test 8 > run Enter n: 11 Enter match value: 0.1111 i | j -> 1 2 3 4 5 6 7 8 9 10 i: 2 0.3333 i: 3 0.5000 0.2000 i: 4 0.6000 0.3333 0.1429 i: 5 0.6667 0.4286 0.2500 0.1111 i: 6 0.7143 0.5000 0.3333 0.2000 0.0909 i: 7 0.7500 0.5556 0.4000 0.2727 0.1667 0.0769 i: 8 0.7778 0.6000 0.4545 0.3333 0.2308 0.1429 0.0667 i: 9 0.8000 0.6364 0.5000 0.3846 0.2857 0.2000 0.1250 0.0588 i:10 0.8182 0.6667 0.5385 0.4286 0.3333 0.2500 0.1765 0.1111 0.0526 i:11 0.8333 0.6923 0.5714 0.4667 0.3750 0.2941 0.2222 0.1579 0.1000 0.0476 The value 0.1111 appears 2 time(s). The sum of all values is 20.3587581493 The sum of the values in the last row (i = 11) is 3.7605899175

Programming Problem

Write a program that displays the terms from the following formula in a 2D table, where n ≥ 2:

For example, the first term when i = 2 and j = 1 is .

A term computed from the combination (i, j) is displayed in the table at row i and column j. See the sample runs above. The program prompts the user for the value n. It also prompts the user for a value to match on. Finally, the program computes and displays how many terms are equal to the value to match on, the sum of all of the terms, and the sum of the terms when i = n, i.e. the terms in the last row of the table.

TASK 0:

  • The function main has been given to you. DO NOT change any code in this function.
  • The function read_input has been given to you. DO NOT change any code in this function.

You will write two functions called display_table and display_results. Study the calls to the functions in the function main. Write the function prototypes before the function main and the function definitions after the function main.

  • Use const when appropriate when defining function parameters.
  • Define a function parameter as either pass-by-value or pass-by-reference.

TASK 1: Write the function display_table that has five input parameters: 1) A label, 2) the value n, 3) the value to match on, 4) how many terms that are equal to the value to match on, and 5) the sum of the terms in the last row, i.e. row i = n. The function returns the sum of ALL of the terms.

  • Only use the for statement in your solution for any looping.

  • Use iomanip library operations to format the 2D table.

  • Write code to display the first line (header) of the table. Display the label (first input parameter of function display_table) followed by the column headings 1 to n - 1. Compile, run, and test your code.

  • Write code to display the terms of the formula for each row from i = 2 to i = n.

  • For each row, display "i:" followed by the value of i (whose maximum value is 99, i.e. two digits). Display the value of i using an iomanip library operator so that the number is right justified. Any other solution will receive no credit. Compile, run, and test your code.

  • Next, display six spaces.

  • Finally, display the terms of the formula from column 1 (j = 1) to column i - 1 (j = i - 1). Using iomanip operators, determine which display mode and precision to use to display the terms and set the width of each column (containing a single space followed by the numeric term value). Note the single space before a numeric value should be included in the column width. Compile, run, and test your code.

Using the function input parameters, your function should:

  • Count (see the 4th input parameter) the number of terms that equal the match on value (see the 3rd input parameter). In order to receive credit, your solution must use the global constant EPSILON to perform the equality.
  • Sum all of the terms (return value)
  • Sum the terms on the last row (see the .5th input parameter)

TASK 2: Write the function display_results that has five input parameters: 1) The value n, 2) the value to match on, 3) how many terms that are equal to the value to match on, 4) the sum of all of the terms, and 5) the sum of the terms in the last row, i.e. row i = n. The function does not return a value. Display the sums using the same display mode as the terms displayed in the table. Though, display these sums using 10 digits after the decimal point.

#include <iostream>
#include <iomanip>

using namespace std;

const double EPSILON(1e-4);

void read_input(int & n, double & match);

/* INSERT prototype for function display_table */

/* INSERT prototype for function display_results */

int main() {

int n, count;
double match, sum_of_all, sum_last_row;
  
read_input(n, match);
  
sum_of_all = display_table("i | j ->", n, match, count, sum_last_row);
cout << endl;
  
display_results(n, match, count, sum_of_all, sum_last_row);

return 0;
}

void read_input(int & n, double & match)
{
cout << "Enter n: ";
cin >> n;
  
while (n < 2 || n > 99) {
cout << "Value must be between [2, 99]." << endl;
cout << "Enter n: ";
cin >> n;
}

cout << "Enter match value: ";
cin >> match;
  
while (match < 0.0001 || match > .9999)
{
cout << "Value must be between [0.0001, .9999]." << endl;
cout << "Enter match value: ";
cin >> match;
}
  
cout << endl;
}

/* INSERT definition for function display_table */

/* INSERT definition for function display_results */

C++. C+++

For unlimited access to Homework Help, a Homework+ subscription is required.

Unlock all answers

Get 1 free homework help answer.
Already have an account? Log in

Related questions

Related Documents

Weekly leaderboard

Start filling in the gaps now
Log in