Help needed for the following lab c Purpose This weeks lab w

Help needed for the following lab (c++).

Purpose

This week\'s lab will consist of you writing some C++ code and turning it in to Web-CAT to ensure that it is correct. So the purpose is to write some basic code, and then use the automated grading system to have it scored. Additionally, we will be using a basic selection statement to check for input errors.

Learning Objectives

This lab focuses on the following learning objectives:

Write complete C++ programs to solve engineering problems (G1)

Use high-quality programming standards to develop C++ programs to solve engineering problems (G2)

Apply successful debugging strategies in a software development process while developing programs of several hundreds lines in length (G3)

Define and use the concepts of data types, arrays, pointers, and stream and file I/O (G4)

Use dynamic memory management, library functions, user-defined functions, and simple classes to solve engineering problems in C++ (G5)

Part 1

To compute this, we will use the following formula:

where R1, R2 and R3 are the values for the 3 resistors.

You can see from this formula that the value for the resistors cannot be 0. You need to check for this. If any of the resistors has a value of 0, please write a message indicating this error and stop your program.

Using this formula, write a C++ program to request, calculate and display the combined resistance for the three resistors R1, R2 and R3 are connected in parallel.

The output from this formula will simply be the answer as a double.

Your job is going to be to read in a series of numbers each set of doubles will be on a single line. See the example input. You can use extraction (>>) to read each double into a variable.

Input

The input will consist of a series of numbers. There will always be three numbers and the numbers should be considered to be doubles. It will be in a file called \"input.txt\". Your function should open a file with that name.

1.0 1.0 1.0
.5 .5 .5
0 1.0 .5

Are three sample inputs.

Output

If you entered one of the inputs above then this is the output from your program. Your program should use a file called \"output.txt\".

0.333333
0.166667
--error--

The output is just the answer to the problem, if none of the resistor values are 0 or the word --error-- if any of them are 0.

In your computeResistance function, make sure you are using in for your input, not cin. Make sure you are using out for your output not cout. The only exception to using cout would be if you\'d like a prompt then use cout.

In order to get this to work, you need to put your functions in a file called:

and you must write a function with the following signature:

When you are done, zip up your resistors.h and any implementation files and turn them in for lab 3. Attached below is a main function you could use. I will also attach some input files you could use for testing. You are welcome to write your own main and your own input files.

This video attempts to explain why we need all these files:

To compile and link these files you can do it this way:

That assumes your code file is named resistors.cpp and you have a main.cpp. It will compile and link the two cpp files together and name it resistors.exe. Then you run your program this way:

Make sure the input file is in the same directory with the exe and it should work.

SKELETON FILE

#include \"resistors.h\"#include <fstream>
using std::endl;using std::ifstream;using std::ofstream;using std::string;
void computeResistance(){

ifstream in(\"input.txt\");ofstream out(\"output.txt\");double r1, r2, r3;
in >> r1 >> r2 >> r3;//do your selection here to check the resistor values////then do the output for the answer or --error-- is there\'s an error.

}

Main.cpp

#include \"resistors.h\"#include <fstream>#include <iostream>
using std::cout;using std::endl;using std::ifstream;
int main(){
computeResistance( );

return 0;

}

header.h file

#include <iostream>#include <string>
void computeResistance();

  

Solution

//resistor.h

#include <iostream>
#include <string>
void computeResistance();

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

//resistor.cpp

#include\"resistor.h\"
#include <fstream>
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
void computeResistance()
{
   ifstream in(\"input.txt\");
   ofstream out(\"output.txt\");
   double r1, r2, r3;
   //check if input file can be opend
   if (!in)
   {
       return ;
   }
   while (!in.eof())
   {
       in >> r1 >> r2 >> r3;
       //do your selection here to check the resistor values//
       if (r1 > 0 && r2 > 0 && r3 > 0)
       {
           double resistance = 1 / (1 / r1 + 1 / r2 + 1 / r3);
           out << resistance << endl;
       }
       //then do the output for the answer or --error-- is there\'s an error.
       if (r1 <= 0 || r2 <= 0|| r3 <= 0)
       {
           out << \"--error--\" << endl;
       }
   }
  
}

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

//main.cpp

#include\"resistor.h\"
#include <fstream>
#include <iostream>
using std::cout; using std::endl; using std::ifstream;
int main()
{
   computeResistance();
   return 0;
}

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

//output as mentioned

Help needed for the following lab (c++). Purpose This week\'s lab will consist of you writing some C++ code and turning it in to Web-CAT to ensure that it is co
Help needed for the following lab (c++). Purpose This week\'s lab will consist of you writing some C++ code and turning it in to Web-CAT to ensure that it is co
Help needed for the following lab (c++). Purpose This week\'s lab will consist of you writing some C++ code and turning it in to Web-CAT to ensure that it is co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site