Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, below is a pretty simple exercise I am doing, it is to help me since I am new get to know the basics of

Hello, below is a pretty simple exercise I am doing, it is to help me since I am new get to know the basics of coding, could someone please complete this and explain why in each step, it needs to follow the guidelines in the paper. I could go online but I would rather be able to learn how to do it. Thanks...a code needs to be written as to make a quadratic equation calculator to run when compiled

For this lab we are going to be writing a quadratic equation calculator. The basic idea is you need to compute the discriminate and determine the number and type of answers from here. Also, this is the first lab where you will be turning your code into Web-CAT for autograding. Web-CAT is extremely particular about the format of your output. So even if you did just take someones code from the web, youd be rewriting it to match the output format required by Web-CAT.

A quick word about Web-CAT, so in order for Web-CAT to be able to access your code, we need to use a header file that we write. The header file typically includes some #include statements, potentially some using statements, and for us the most important part is a function declaration. A function declaration, like a variable declaration, simply allows for a function name to be known and allows code to call a function. So my test code will include the header, and therefore it can call the function we are writing for our lab. I will also provide the framework for the function, similar to last lab, you will be adding the inside of the function. Finally, Web-CAT is extremely picky about output. If you have extra words or numbers, it wont find your answers. I will prescribe what the output needs to look like and so you need to follow that. Extra spaces on a line do not matter. As long as you have at least a space where I have space and dont have spaces where I dont you can have as many or as few spaces as you want.

Header file Call this file: lab4.h

#include //this allows us to use files for input and output

#include //this gives us access to sqrt

#include //this gives us access to string for our input and output variables

using std::string; //these let the compiler know what to

using std::ifstream; //do when it encounters these words

using std::ofstream;

using std::endl;

/*

* This is a function declaration. We haven't gotten there yet,

* so I provide this one so we can do more before we've

* learned all the ins and outs.

*/

void quadradic( string input, string output );

/*

* All you need to do is download this file and put it in your

* directory with your lab4.cpp file. When you turn in

* your code for testing you'll also zip up this file

* with your code. This file allows my testing file to

* link with your code

*/

main.cpp This can be used to link your code and test

#include "lab4.h" //this gives the main file access

//to the function we declared in the header

//so it can call it.

int main()

{

quadradic( "input.txt", "output.txt" );

//here's where we name the input and

//output files. Don't put file names

//anywhere but here. After this we'll

//use the variables to access the file

//we want to use.

}

lab4.cpp This is the start of lab 4.

#include "lab4.h" //this get's the header file I

//provided into your code

//The header will also "glue" or link your cpp and my

//test file together

/*

* So I'm going to put a lot of comments in here and

* you should read them. They are intended to explain

* what's going on and should help answer some of

* your questions.

*/

/*

* So this is the start of a function. I know

* we haven't covered them yet, so that's why

* I put this here. It's how I will connect

* with your code.

*/

void quadradic( string input, string output )

{

ifstream in(input);//this connects your code

//to the file named in the variable input.

//this is how we will do 99% of the code we

//write this term

//Now we can use in to do our input; think of

//it like cin

ofstream out(output);//this connects your code

//to the file named in the variable output.

//this is how we will do 99% of the code we

//write this term

//Now we can use in to do our output; think of

//it like cout

//declare the variables you'll need

//you'll need at least ones for a, b, and c

//as well as one for the answer. You may

//want to make one for the discriminate too.

double a, b, c, discrim, answer1, answer2;

//Now that you've read in the values from the file using

//the variable in and storing them in a, b and c

//check the discriminate to make sure it's safe

//or even needed to compute the square root.

//in here is like cin, but in is attached to the

//input file

in >> a >> b >> c;

//Now output the answers. See the sample outputs

//to make sure your output format matches what is

//expected. If it doesn't make the expected

//format, then it is wrong even if your answers

//are correct. Harsh I know. But make sure

//you figure that out now in lab and not on the

//projects.

//do some output here

//use out just like you would use cout and it will

//go into the output file.

//this is where you start doing some output

//and figuring out the roots and outputting them

in.close(); //don't forget to close up

out.close();//when you are done.

}

Input

So since there are 3 cases we are concerned with Ill give you a sample of all 3. In any case, the input will be a line of 3 numbers. The code above will read in the number correctly, regardless if they look like ints as doubles.

Sample 1

1 8 9

Sample 2

4 4 1

Sample 3

7 2 9

Below will be the corresponding sample outputs for the above inputs.

Output

Sample 1

a = 1

b = 8

c = 9

Two real solutions.

x1 = -1.35425

x2 = -6.64575

Sample 2

a = 4

b = 4

c = 1

One real solution.

x = -0.5

Sample 3

a = 7

b = 2

c = 9

Two imaginary solutions.

x1 = -0.142857 + 1.12486i

x2 = -0.142857 - 1.12486i

So some things to notice is that the output is prescriptive, meaning, if you have the right answer but its not in the correct format, then your answer is wrong. This is true for all Web-CAT assignments.

Second, I start typically by giving back the input, so the first 3 lines are just that. Youll need the spaces where I have them, for example:

a = 7

is not the same as

a=7

or

a= 7

or

a =7

You have to have spaces where I do, at least 1, and you cant have space where I dont.

After the first 3 lines, theres a line that tells the kind of solution. Note the punctuation marks.

Finally, theres 1 or 2 lines, depending on the number of solutions. When there are two note I have x1 and x2, but when theres only 1 solution I just have x. In the imaginary case, I have both the real and imaginary parts.

Requirements

You must name your header file lab4.h

In your lab4.h header file, you must declare the following function void quadradic( string input, string output );

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions