Question: C++ Overview In the first class we discussed how to read and write objects using IOStreams (formatted operations) and we made use of operator overloading

C++

Overview

In the first class we discussed how to read and write objects using IOStreams (formatted operations) and we made use of operator overloading and that code was uploaded as Week 01 Files. (Such is also covered in Week 01 Slides and in the assigned reading.) Instead of merely reading in integers or doubles, etc. we discussed how to handle some simple formatting around such numbers in order to read in two-dimensional point values such as (1.1, 2.3). Also discussed was how to properly determine whether or not something was read in in order to "fail" the stream which prevents any further I/O from occurring on the stream. Performing I/O like this is very useful in C++. This assignment's task is different but similar to what was presented in the lecture.

Task

Given an input file of zero or more three-dimensional vectors formatted as '<' x-coordinate ',' y-coordinate ',' z-coordinate '>' where:

  • < is the less than character
  • , is the comma character
  • > is the greater than character, and,
  • x-coordinate, y-coordinate, and z-coordinate are all double floating-point values

and all of these occur possibly with whitespace before and after each item. (NOTE: The single quotes around '<', ','. and '>' are not part of the input.) Thus, an example input file could be:

<1,-1,10> < 2 , -2,20 ><3,-3.3,30><-5,5,-50.5> 

You are to write a program that reads in from standard input (i.e., std::cin) all three dimensional vector values until an error (e.g., failed input or some other stream error) or EOF occurs. As the program reads in each vector, the program must do the following:

  • keep a running count of the number of vectors successfully read in, and,
  • add each subsequent vector to the previous vector sum.

The vector sum obviously will start with an all-zero vector value before any vector is read in. (Ensure you hard-code this in your program. Do not assume variables will get set to zero!)

Your program must produce its output as follows:

  • the first line must output "count: " (i.e., count: followed by a space, without the quotes) followed by the number of vectors read in, and,
  • the second line must output "sum: " (i.e., sum: followed by a space, without the quotes) followed by the final vector sum. (If there are no valid vectors in the input, then the all-zero vector needs to be output.)

Your program must produce the correct output regardless of the input provided to it --so ensure you do proper input checking.

Sample Program Run

Given an input.dat file such as:

<1,-1,10> < 2 , -2,20 ><3,-3.3,30><-5,5,-50.5> 

Then compiling a program (say it is called a01.cpp) and running it with input.dat redirected to its standard input, would look like this at a Linux/Unix shell prompt:

$ g++ -Wall -Wextra -o a01.exe a01.cpp $ ./a01.exe $ 

since there are four 4 vectors in input.dat.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!