Sunday, 5 August 2018

DS Malik PROGRAMMING EXERCISES -- Chapter 3 -- Question 1 -- Solution

Leave a Comment
Question :


1. Consider the following incomplete C++ program:
#include <iostream>
int main()
{
...
}
a. Write a statement that includes the header files fstream, string, and
iomanip in this program.
b. Write statements that declare inFile to be an ifstream variable and
outFile to be an ofstream variable.
c. The program will read data from the file inData.txt and write output
to the file
outData.txt. Write statements to open both of these files,
associate
inFile with inData.txt, and associate outFile with
outData.txt.
d. Suppose that the file inData.txt contains the following data:
10.20 5.35
15.6
Randy Gill 31
18500 3.5
A
The numbers in the first line represent the length and width, respectively, of
a rectangle. The number in the second line represents the radius of a circle.
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. (Assume
that p ¼3.1416.) The fifth line contains an uppercase letter between
A and
Y (inclusive). Write statements so that after the program executes, the con-
tents of the file
outData.txt are as shown 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.
Rectangle:
Length = 10.20, width = 5.35, area = 54.57, parameter = 31.10
Circle:
Radius = 15.60, area = 764.54, circumfer
ence = 98.02
Name: Randy Gill, age: 31
Beginning balance = $18500.00, interest rate = 3.50
Balance at the end of the month =
$18553.96
The character that comes after A in the ASCII set is B
e. Write statements that close the input and output files.
f. Write a C++ program that tests the statements in parts a through e.

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
       ifstream inFile;
       ofstream outFile;

       inFile.open("inData.txt");
       outFile.open("outData.txt");
       double length;                       //variable declarations are correct
       double width;
       double radius;
       string name;
       int age;
       double savings;
       double interest;
       string letter;

       inFile >> length >> width;
       outFile << length << width; //just add an endl after these so it look similar to the file given

       inFile >> radius;
       outFile << radius;

       inFile >> name >> age;
       outFile << name << age;   //The file reads "Randy Gill", note that ifstream
                                                  //files take whitespaces as the end of reading, which means you're reading
                                                  //Gill into age

       inFile >> savings >> interest;
       outFile << savings << interest;

       inFile >> letter;
       outFile << letter << endl;
       //simple enough, use cout statements to display
       //contents of your vars
       inFile.close();
       outFile.close();

       system("PAUSE");

       return 0;

}

Output:


Related Articles:


DS Malik Fifth Edition Complete Solution Manual 

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution



Note :: All credit and all rights belong to DS Malik

and their respective partners. I do not own this material, nor do i claim to do so. This material is only for educational purpose.

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: