Question

Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...

need help on C++ (User-Defined Function)

Format all numerical decimal values with 4 digits after the decimal point.

Process and sample run:

a) Function 1: A void function that uses parameters passed by reference representing the name of a data file to read data from, amount of principal in an investment portfolio, interest rate (any number such as 5.5 for 5.5% interest rate, etc.) , number of times the interest is compounded, and the number of years the money is invested.

b) Function 2: A double function that accepts the name of a file as its parameter and returns the average of the numbers . Note, you must open the file, read data from it and close the file. If the file does not exist, return - 1000 for the average.

c) Function 3: A double function that accepts the name of a file and a variable representing the average of the numbers as its parameters and returns the variance of the numbers. Note, you must open the file, read data from it and close the file. If the file does not exist, return - 1000 for the variance. The ∑ symbol represents the total of the values. X (with a hat) is the average of the numbers.

d) Function 4: A double function that accepts the variance of numbers as its parameter and returns the standard deviation of the numbers. Standard deviation is the square root of variance. If the variance is negative, return -1000 for the variance.

e) Function 5: A double function to calculate and return the future value of investment using compound interest. This function accepts what it needs as parameters and will return the future value. Use the header: double getFutureValue(double principal, double rate, int times, int years ). Here is the formula to calculate Future Value of an investment:

Future value = principal (1 + rate / times)(times * years)

Note: In the code, it must convert the rate to a decimal value by dividing rate by 100. Otherwise, the calculation will be wrong.

f) Function 6: A void function to display the values of all input and calculated values using a format similar to the output shown in the sample run.

In a main function, call your functions to process data. Check for the existence of the input data file. If the file does not exist, the statistics about your data file will not be displayed and an error message will be displayed. Make sure to ASK the user for the necessary input. For example, ask the user for the name of a file rather than hard -coding it in your code. This makes your testing life much easier.

Here is a sample run:

Assuming our data file is the following, we can expect the following results. Remember that the code must work on any input data file. NOTE that the first line of the input data file is always a string of characters used as the header of the file that should be read before you read the data.

Content of the DATA FILE

STUDENT DATA

23.87

15

19.87

-78

10.01

55

///////(below should display the results function 6)

Average = 7.6250

Variance = 2009.9900

Standard Deviation = 44.8329

Initial balance = 1000.0000

Interest rate = 5.5000

Compounded times = 4

Number of years = 20

Future Value = 2981.7373

////(this is the return result of function 1 and 2)

If the data file does not exist, we could expect the following results for the sample statistics:

Average = -1000.0000

Variance = -1000.0000

Standard Deviation = -1000.0000

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

uick Launch (Ci+a visual Studio Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help //Header files 曰#include 6 <io--------------------------------------------------------

Program

//Header files
#include <iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
//Function prototype
void function1(string&, double&, double&,int&,int&);
double function2(string);
double function3(string,double);
double function4(double);
double getFutureValue(double, double, int, int);
void function6(double, double, double, double, double, double, int, int);
int main()
{
   //Variables to store values
   string filename;
   double principalAmt, interestRate,avg,variance,sd,fv;
   int compundedTime, years;
   //Call methods
   function1(filename, principalAmt, interestRate, compundedTime, years);
   avg = function2(filename);
   variance = function3(filename, avg);
   sd = function4(variance);
   fv = getFutureValue(principalAmt, interestRate, compundedTime, years);
   function6(principalAmt, interestRate, fv, avg, variance, sd, compundedTime, years);
}
//Method to read file name and other values
void function1(string& filename, double& p, double& i,int& c,int& y) {
   cout << "Enter file name: ";
   cin >> filename;
   cout << "Enter principal amount: ";
   cin >> p;
   while (p < 1) {
       cout << "Error!!!Principal amount could not be negative!!!" << endl;
       cout << "Enter principal amount: ";
       cin >> p;
   }
   cout << "Enter interest rate: ";
   cin >> i;
   while (i<0) {
       cout << "Error!!!Interest rate could not be negative!!!" << endl;
       cout << "Enter interest rate: ";
       cin >> i;
   }
   cout << "Enter compount times: ";
   cin >> c;
   while (c < 1) {
       cout << "Error!!!compound time could not be negative!!!" << endl;
       cout << "Enter compount times: ";
       cin >> c;
   }
   cout << "Enter years: ";
   cin >> y;
   while (y < 1) {
       cout << "Error!!!years could not be negative!!!" << endl;
       cout << "Enter years: ";
       cin >> y;
   }
}
//Function to get average
double function2(string filename) {
   ifstream in(filename);
   if (!in) {
       return -1000;
   }
   double avg=0,val;
   int count=0;
   while (!in.eof()) {
       in >> val;
       avg += val;
       count++;
   }
   return avg / count;
}
//Function to get variance
double function3(string filename, double avg) {
   ifstream in(filename);
   if (!in) {
       return -1000;
   }
   double variance = 0, val;
   int count = 0;
   while (!in.eof()) {
       in >> val;
       variance += pow((val-avg),2);
       count++;
   }
   return (variance / (count-1));
}
//Function to get standard deviation
double function4(double variance) {
   double sd = sqrt(variance);
   if (sd < 0) {
       return -1000;
   }
   return sd;
}
//Function to get future value
double getFutureValue(double principal, double rate, int times, int years){
   return (principal*(pow((1 + ((rate / 100) / times)),(times * years))));
}
//Function to display all values
void function6(double p, double i, double fv, double avg, double var, double sd, int c, int y) {
   cout << fixed << setprecision(4);
   if (avg == -1000) {
       cout << "Average = " << avg << "\nVariance = " << var << "\nStandard Deviation = " << sd << endl;
   }
   else {
       cout << "Average = " << avg << "\nVariance = " << var << "\nStandard Deviation = " << sd << endl;
       cout << "Initial balance = " << p << "\nInterest rate = " << i << "\nCompounded times = " << c << endl;
       cout << "Number of years = " << y << "\nFuture Value = " << fv << endl;
   }
}

------------------------------------------------------------------------------------------------

Output

Enter file name: C:/Users/deept/Desktop/studentdata.txt
Enter principal amount: 1000
Enter interest rate: 5.5
Enter compount times: 4
Enter years: 20
Average = 7.6250
Variance = 2009.9900
Standard Deviation = 44.8329
Initial balance = 1000.0000
Interest rate = 5.5000
Compounded times = 4
Number of years = 20
Future Value = 2981.7373

Add a comment
Know the answer?
Add Answer to:
Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

    Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as the input a provided string for the sample name (sname) and number for strength (strgth) - open a text file named mytensiledata.txt, with permission to read or append the file. - Make sure to capture any error in opening the file - Write sname and strgth to the file - Close the file...

  • Needs some help [User-Defined Function] C++ Write a function, readWrite, that reads a set of test...

    Needs some help [User-Defined Function] C++ Write a function, readWrite, that reads a set of test scores stored in a file and calculates and returns the average of the numbers. The function returns a double and has the name of the file as its parameter. If the data is as follows, the average will be 5.66. 3 6 8

  • Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are...

    Modify the C++ program you created in assignment 1 by using 'user defined' functions. You are to create 3 functions: 1. A function to return the perimeter of a rectangle. This function shall be passed 2 parameters as doubles; the width and the length of the rectangle. Name this function and all parameters appropriately. This function shall return a value of type double, which is the perimeter. 2. A function to return the area of a rectangle. This function shall...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Lab 1 Q1. Write a Python program with the following function building block. •All input values...

    Lab 1 Q1. Write a Python program with the following function building block. •All input values are to be taken fro m the user. •Ensure to check for possible error cases like mismatch of data type and input out of range, for every function.       Function 1: countVowels(sentence) – Function that returns the count of vowels in a sentence. Function 2: Sum of all even numbers between two numbers, identify number of parameters   Q2. Write a Python program that reads a...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • MUST BE WRITTEN IN C++ All user input values will be entered by the user from...

    MUST BE WRITTEN IN C++ All user input values will be entered by the user from prompts in the main program. YOU MAY NOT USE GLOBAL VARIABLES in place of sending and returning data to and from the functions. Create a main program to call these 3 functions (5) first function will be a void function it will called from main and will display your name and the assignment, declare constants and use them inside the function; no variables will...

  • c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

    c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did the program Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions (names and purposes given below). Fot o tentoutefill datere nedefremfite You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or...

  • Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The...

    Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The functions are described below on page 2. (Just write the prototypes) When necessary, use the variables declared below in maino. mm 1.) showMenu m2.) getChoice 3.) calcResult m.) showResult 5.) getInfo mm.) showName 7.) calcSquare 8.) ispositive int main { USE THESE VARIABLES, when needed, to write function prototypes (#1 - #8) double num1 = 1.5; double num2 = 2.5; char choice; double result;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT