Question
C++ 8.9 Formatted output QUICK SUMMARY: You only need to write the function PrintFunctionValues in the file print_values.cpp In this lab, you will write a
C++
8.9 Formatted output
QUICK SUMMARY: You only need to write the function PrintFunctionValues in the file print_values.cpp
In this lab, you will write a function called PrintFunctionValues() that takes two arguments, an istream and an ostream. The function will read integer values from the istream until it reaches the end, and for each value it will output to the ostream a single line of text showing the values of various functions at that value. The output will be described in detail below.
The function signature is:
void PrintFunctionValues(std::istream & is, std::ostream & os);
Your function will be in the file print_values.cpp. I have provided print_values.h.
There is no need to check for any errors; just read until EOF is hit. Assume any stream failure is EOF.
You do NOT need to store the values; just print out one line as each value is read.
For each value read, you will output the values of the following functions, all on one line, using the field width, precision, and format provided in the following table:
Function | Type | Field Width | Precision (format) |
---|---|---|---|
log2(n) | double | 5 | 2 (fixed) |
n | int | 4 | n/a |
n * log2(n) | double | 8 | 2 (fixed) |
n^2 | int | 7 | n/a |
n^2 * log2(n) | double | 9 | 3 (scientific) |
n^3 | int | 10 | n/a |
Note that ^ is used here as an exponentiation operator; that is, n^2 means n*n. log2(n) is the log base 2 function.
Your function also has to print a two-line header above the output; see the examples below for how that must look.
If the input from the istream is:
1 2 4 6 10
then the output to the ostream should be:
lg(n) n n*lg(n) n^2 n^2*lg(n) n^3 ----- - ------- --- --------- --- 0.00 1 0.00 1 0.000e+00 1 1.00 2 2.00 4 4.000e+00 8 2.00 4 8.00 16 3.200e+01 64 2.58 6 15.51 36 9.306e+01 216 3.32 10 33.22 100 3.322e+02 1000
Notes:
There is one space between each field, so print out an extra space after every value (except for the last value on a line).
For the log2(n) function, you'll need to include the
Do NOT assume that the input stream is std::cin or that the output stream is std::cout.
I have provided print_values.h.
Since the whole point of this lab is to get output formatted exactly correctly, there is no partial credit for getting all but the spacing correct; it's all or nothing.
The first line of print_values.cpp should be to include the print_values.h` header.
Since you don't need to do error checking, you can use a while (is >> i) loop as shown in template provided.
print_values.cpp
#include
#include "print_values.h"
using namespace std;
void PrintFunctionValues(istream & is, ostream & os) { // TODO: output two line header to os
int i; while (is >> i) { // define some intermediate variables to hold some of the calculations int i2 = i * i; int i3 = i2 * i; double d = i; double ld = log2(d); // output the next row to os os << fixed << setprecision(2); os << setw(5) << ld << ' '; // TODO: output the rest of the row // HINT: You will need to use std::scientific at one point } } print_values.h
#pragma once
#include
// PrintFunctionValues prints the values of various functions // for the integral input values read fro is. The results are // printed to the given ostream. // // The following functions are used: // // function type field width precision // log2(n) double 5 2 (fixed) // n int 4 n/a // n * log(n) double 8 2 (fixed) // n^2 int 7 n/a // n^2 * log(n) double 9 4 (scientific) // n^3 int 10 n/a // // There is a space between each field.
print_values_test.cpp
#include
#include "print_values.h"
int main(int argc, char * argv[]) { if (argc != 3) { std::cout << "Usage: " << argv[0] << "infile outfile "; std::cout << "If infile is \"stdin\", standard input is used. "; std::cout << "If outfile is \"stdout\", standard output is used. "; return 1; } bool use_stdin = argv[1] == std::string{ "stdin" }; bool use_stdout = argv[2] == std::string{ "stdout" };
if (use_stdin && use_stdout) PrintFunctionValues(std::cin, std::cout); else if (use_stdout) { std::ifstream in{ argv[1] }; if (!in) { std::cout << "Could not open " << argv[1] << ' '; return 1; } PrintFunctionValues(in, std::cout); } else if (use_stdin) { std::ofstream out{ argv[2] }; if (!out) { std::cout << "Could not open " << argv[2] << ' '; return 1; } PrintFunctionValues(std::cin, out); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started