Suppose that the file inData.txt contains the following data:
3 4 5
15.6
“Mark” “Taylor” 28
18500 3.5
B
The numbers in the first line represent the Side A, Side B and Side C of a Triangle. The number in the second line represents the radius of a circle (Assume that p = 3.1416). The third line contains the first name, last name, and the age of a person. The first number in the fourth line is the savings account balance at the beginning of the month and the second number is the interest rate per year. The fifth line contains an uppercase letter between B and Z (inclusive). Write statements so that after the program executes, the contents of the outData.txt are shown as below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results.
Triangle:
Side A= 3, Side B = 4, Side C = 5, area = 6, perimeter = 12
Circle:
Radius = 15.60, area = 764.54, circumference = 98.02
Name: Mark Taylor, age: 28
Beginning balance = $18500.00, interest rate = 3.50
Balance at the end of the month = $18553.96
The character that comes before B in the ASCII set is A
f. Write a complete C++ program that tests the statements in parts a through e.
Hints for Part 2:
• You should include the math header file and call appropriate routine
• You should calculate the area of Triangle using Heron’s formula, defined as follows:
s = (a+b+c)/2 where a, b and c are the side lengths of the triangle
area = √(?(?−?)(?−?)(?−?))
• Financial compound interest computed as follows: F = P(1 + i)n Where F is the future value of money P is the present value of money i is the interest rate in percentage (i.e., rate /100), represent as decimal number n is the time period
--------------------------------lab.cpp-----------------------
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
double sideA;
double sideB;
double sideC;
double radius;
string fName;
string lName;
int age;
double presentBalance;
double rate;
char ascii;
ifstream inFile;
ofstream outFile;
inFile.open("inData.txt");
outFile.open("outData.txt");
// inFile.open();
//outFile.open();
inFile >> sideA >> sideB >> sideC;
cout << sideA << " " << sideB << " " << sideC << endl;
outFile << sideA << " " << sideB << " " << sideC << endl;
inFile >> radius;
cout << "Circle " << radius << endl;
outFile << "Circle " << radius << endl;
inFile >> quoted(fName) >> quoted(lName) >> age;
cout << "First Name " << fName << endl;
outFile << "First Name " << fName << endl;
cout << "Last Name " << lName << endl;
outFile << "Last Name " << lName << endl;
cout << "Age: " << age << endl;
outFile << "Age: " << age << endl;
inFile >> presentBalance >> rate;
cout << "Balance and rate " << presentBalance << " " << rate << endl;
outFile << "Balance and rate " << presentBalance << " " << rate << endl;
inFile >> ascii;
cout << "The character " << ascii << endl;
outFile << "The character " << ascii << endl;
inFile.close();
outFile.close();
return 0;
}
------------------------inData.txt--------------
3 4 5
15.6
"Mark" "Taylor" 28
18500 3.5
B
If you have any doubts, please give me comment...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
string quoted(const string &str);
int main()
{
double sideA;
double sideB;
double sideC;
double radius;
double area;
double perimeter;
double circumference;
string fName;
string lName;
int age;
double presentBalance;
double rate;
double futureValue;
char ascii;
ifstream inFile;
ofstream outFile;
inFile.open("inData.txt");
outFile.open("outData.txt");
// inFile.open();
//outFile.open();
inFile >> sideA >> sideB >> sideC;
perimeter = (sideA+sideB+sideC)/2;
area = sqrt(perimeter*(perimeter-sideA)*(perimeter-sideB)*(perimeter-sideC));
cout <<"Side A= "<< sideA << ", Side B = " << sideB << ", Side C = " << sideC <<", area = "<<area<<", perimeter = "<<perimeter<<endl;
outFile <<"Side A= "<< sideA << ", Side B = " << sideB << ", Side C = " << sideC <<", area = "<<area<<", perimeter = "<<perimeter<<endl;
inFile >> radius;
area = M_PI * radius *radius;
circumference = 2*M_PI*radius;
cout<<setprecision(2)<<fixed;
outFile<<setprecision(2)<<fixed;
cout << "Circle:"<<endl;
cout<<"Radius = " << radius<<", area = "<<area<<", circumference = "<<circumference<<endl;
outFile << "Circle:"<<endl;
outFile<<"Radius = " << radius<<", area = "<<area<<", circumference = "<<circumference<<endl;
inFile >> quoted(fName) >> quoted(lName) >> age;
cout << "Name: " << fName<<" "<<lName <<", age = "<<age<< endl;
outFile << "Name: " << fName<<" "<<lName <<", age = "<<age<< endl;
inFile >> presentBalance >> rate;
futureValue = presentBalance*pow((1+(rate/100.0)), 1/12.0);
cout << "Beginning balance = $"<<presentBalance<<", interest rate = "<<rate<<endl;
cout<<"Balance at the end of the month = $"<<futureValue<< endl;
outFile << "Beginning balance = $"<<presentBalance<<", interest rate = "<<rate<<endl;
outFile<<"Balance at the end of the month = $"<<futureValue<< endl;
inFile >> ascii;
cout << "The character that comes before "<<ascii<<" in the ASCII set is "<<(char)(ascii-1)<< endl;
outFile << "The character that comes before "<<ascii<<" in the ASCII set is "<<(char)(ascii-1)<< endl;
inFile.close();
outFile.close();
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.