YOU MUST USE CodeBlocks TO WRITE A PROGRAM, THIS IS C++ PROGRAMM! 2. Write a pro
ID: 3737297 • Letter: Y
Question
YOU MUST USE CodeBlocks TO WRITE A PROGRAM, THIS IS C++ PROGRAMM!
2. Write a program to illustrate the Ohm' s Law, I-V/R. The program will first request to input an initial voltage (in volt), a fixed resistance (in ohm) and an incremental interval for voltage. A function should be written which will return the value of current (in amp) given a voltage and a resistance. Then the program will call the function and print out a table that shows the value of current under each increment of voltage. The maximum voltage is set to 50 volts. a) Main should handle all input and output. (This requires that variables in main are updated by the function.) b) Secondary function should handle all calculations. c) Set precision to 2 Output Example: Enter the initial voltage (in volt) 40 Enter the resistance (in ohm): 10 Enter the voltage incremental interval (in volt): 2 Voltage (volt) Resistance (ohm)Current (amp) 40 10 10 10 10 10 10 4.00 4.20 4.40 4.60 4.80 5.00 42 46 48 50Explanation / Answer
Dear Student,
below i written the complete C++ code as per the requirement.
Please note the below program has been tested on ubuntu 16.04 system and compiled using g++ compiler. This code will also work on code blocks.
-----------------------------------------------------------------------------------------------------------------------------------
Program:
------------------------------------------------------------------------------------------------------------------------------------
//headers
#include<iostream>
#include<iomanip>
//namespace
using namespace std;
//function which calculates the current
double cal_current(double v, double r)
{
//calculate current
double I = v/r;
//return I to calling function
return I;
}
//start of main function
int main()
{
//variables
double V, R;
int inc;
double I;
//accept data from the user
cout<<"Enter the initial voltage (in volts): ";
cin>>V;
cout<<endl<<"Enter the resistance (in ohm): ";
cin>>R;
cout<<endl<<"Enter the voltage incremental interval (in volt): ";
cin>>inc;
cout<<endl;
//displa the heading
cout<<left<<setw(20)<<"Voltage (volt)"<<left<<setw(20)<<"Resistance(ohm)"<<left<<setw(20)<<"Current (amp)"<<endl;
cout<<left<<setw(20)<<"--------------"<<left<<setw(20)<<"---------------"<<left<<setw(20)<<"-------------"<<endl;
cout<<endl;
//for loop which call the function and display the output
for(int i = V; i<=50; i = i+inc)
{
I = cal_current(i, R);
cout<<left<<setw(20)<<i<<left<<setw(20)<<R<<left<<setw(20)<<fixed<<setprecision(2)<<I<<endl;
}
return 0;
}
================================================================
Sample Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.