Ididn’t understand how to modify the program for the givensource code. Can someo
ID: 3616353 • Letter: I
Question
Ididn’t understand how to modify the program for the givensource code. Can someone please show me how to do this? Thanks inadvance
1) Modifythe program so that all the output is performed in a separatefunction.
What isthe type of this function? Explain.
2). Modifythe program so that all the input values are read from the keyboardusing
only one function. What type should be all input variables?Explain.
#include"stdafx.h"
#include<iostream>
usingnamespace std;
//Function prototype.
floatcapacitive_reactance(float, float);
// Thisfunction computes the capacitive reactance
// for agiven value of capacitor and frequency.
floatinductive_reactance(float, float);
// Thisfunction computes the inductive reactance
// for agiven value of inductor and frequency.
intmain()
{
floatfarads; // Value of the capacitor.
floathenrys; // Value of the inductor.
floathertz; // Value of the frequency.
floatreactanceC; // Value of the capacitive reactance.
floatreactanceI; // Value of the inductiive reactance.
cout<< "Input value of the capacitor in farads => ";
cin>> farads;
cin.ignore();
cout<< "Input value of the inductor in henrys => ";
cin>> henrys;
cin.ignore();
cout<< "Input value of the frequence in hertz => ";
cin>> hertz;
cin.ignore();
reactanceC= capacitive_reactance(farads, hertz);
cout<< " ";
cout<< "The reactance of a " << farads << " faradcapacitor ";
cout<< "at a frequency of " << hertz << " hertz is "<< reactanceC;
cout<< " ohms. ";
reactanceI= inductive_reactance(henrys, hertz);
cout<< " ";
cout<< "The reactance of a " << henrys << " henryinductor ";
cout<< "at a frequency of " << hertz << " hertz is "<< reactanceI;
cout<< " ohms. ";
cin.get();
return0;
}
floatcapacitive_reactance(float capacitance, float frequency)
{
floatreactance; // The capacitive reactance.
float pi =3.14159;
reactance= 1/(2 * pi * frequency * capacitance);
return(reactance);
}
floatinductive_reactance(float inductance, float frequency)
{
floatreactance; // The inductive reactance.
float pi =3.14159;
reactance= 2 * pi * frequency * inductance;
return(reactance);
}
Explanation / Answer
Actually in this question, you have to modify the program sothat:
1) all the output is performed in separate function (means youhave to display output using cout) and donot take any input
2) all the input is performed in separate one function (meansyou have to take input from user through keyboard using cinfunction only) and donot display any output
I hope its clear, however I have modified the code below foryour ease
Please Rate as I also have to do my assignment as well but Iwill do mine later :)
#include "stdafx.h" #include <iostream>
using namespace std;
// Function prototype. float capacitive_reactance(float, float); // This function computes the capacitive reactance // for a given value of capacitor and frequency.
float inductive_reactance(float, float); // This function computes the inductive reactance // for a given value of inductor and frequency.
void displayOutput(); //This function will display output only using cout and itstype is void (null)
float takeInput(); //This one function will only take input using cin and itstype is float since it will return float result //All input types farads, henrys, hertz are float and since weare using float values so no need to change types
int main() { float farads; // Value of the capacitor. float henrys; // Value of the inductor. float hertz; // Value of the frequency. float reactanceC; // Value of the capacitive reactance. float reactanceI; // Value of the inductiive reactance.
displayOutput();
cin.get(); return 0;
}
float capacitive_reactance(float capacitance, floatfrequency) { float reactance; // The capacitive reactance. float pi = 3.14159; reactance = 1/(2 * pi * frequency * capacitance); return(reactance); }
float inductive_reactance(float inductance, floatfrequency) { float reactance; // The inductive reactance. float pi = 3.14159; reactance = 2 * pi * frequency * inductance; return(reactance); }
void displayOutput() {
cout << "Input value of the capacitor in farads =>"; farads=takeInput(); //Take input for capacitor
cout << "Input value of the inductor in henrys =>"; henrys=takeInput(); //Take input for inductor
cout << "Input value of the frequence in hertz =>"; hertz=takeInput(); //Take input for frequence
reactanceC = capacitive_reactance(farads, hertz); cout << " "; cout << "The reactance of a " << farads << "farad capacitor "; cout << "at a frequency of " << hertz << "hertz is " << reactanceC; cout << " ohms. ";
reactanceI = inductive_reactance(henrys, hertz); cout << " "; cout << "The reactance of a " << henrys << "henry inductor "; cout << "at a frequency of " << hertz << "hertz is " << reactanceI; cout << " ohms. "; }
float takeInput() { // as asked in question that we have to create one functionfor input, thus this will serve as one function for all values andwill return float type. float result; cin >> result; cin.ignore(); return (result); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.