Question

In: Computer Science

If the file circuit.txt contains the following data

Exercise 2:

  1. If the file circuit.txt contains the following data

3.0             2.1

1.5             1.1

2.6             4.1

The first column is voltage and the second column is the electric current.

Write program that reads the voltages and currents then calculates the electric power (P) based on the equation:

Voltage     Current        Power

3.0             2.1              (result)

1.5             1.1              (result)

2.6             4.1              (result)

                        P = v * i

Write your output to the file results.txt with voltage in the first, current in the second and power on the third column.

Your output file should look like

// part a

#include

#include

using namespace std;

int main ()

{

ifstream aaa;

aaa.open ("circuit.txt");

ofstream ppp;

ppp.open("result.txt");

int i;

double a,b,p;

ppp<< "Voltage"<<"   Current"<<"   Power"<<endl;

for (i=1; i<=3; i++)

    {

        aaa>>a>>b;

        p=b*a;

        ppp << a <<"      "<"      "<< p<<endl;

    }

return 0;

}

If you do not know the number of lines in the circuit.dat file in part a, modify your program to read the input file by using eof function.

Solutions

Expert Solution

#include

#include

#include

using namespace std;

int main()

{

ifstream aaa;

aaa.open ("circuit.txt");

ofstream ppp;

ppp.open("result.txt");

// checking if input file is good to open

if(!aaa.is_open())

{

cout << "Error in opening file circuit.txt! Exiting..\n";

exit(EXIT_FAILURE);

}

// checking if output file is good to open

if(!ppp.is_open())

{

cout << "Error in opening file result.txt! Exiting..\n";

exit(EXIT_FAILURE);

}

ppp << "Voltage" << " Current" << " Power" << endl;

double a,b,p;

while(!aaa.eof())

{

aaa >> a >> b;

p = a * b;

ppp << a << " " << b << " " << p << endl;

}

return 0;

}

 

 

OUTPUT FILE (result.txt)


Related Solutions

Use the data in the Mod8-2Data file to answer the following questions. The data contains information...
Use the data in the Mod8-2Data file to answer the following questions. The data contains information from a car seat manufacturer on the age of machine (in months) and the cost of repairs (in 10s of $). Run the regression in Minitab and show the regression line on a scatter plot. Assume a level of significance of 5%. Age Repairs10 110 32.767 113 37.668 114 39.252 134 44.314 93 34.262 141 47.616 115 32.474 115 33.898 115 43.345 142 52.637...
Assume there is a file called "mydata". each line of the file contains two data items
how do you read in a file in JAVA Assume there is a file called "mydata". each line of the file contains two data items: hours and rate. hours is the represented by the number of hours the worker worked and rate is represented as hourly rate of pay. The first item of data is count indicating how many lines of data are to follow.Methodspay- accepts the number of hours worked and the rate of pay. returns the dollor and cents...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was...
The data file contains displacement (in mm)-load (in N) data for a mechanical test that was conducted on an unknown metal. The initial length and diameter of the specimen are also given. a. (5 pts.) Using the data and a computer program (such as Excel), create an engineering stress-engineering strain graph with proper labels. The stress axis should be in the units of MPa. You do not need to show your spreadsheet or software code used to make the graph....
In the Excel data file, the tab labeled Question 1 contains data on the number of...
In the Excel data file, the tab labeled Question 1 contains data on the number of times boys and girls raise their hands in class. Conduct the t-test: Two-Sample Assuming Equal Variances. Males 9,8,4,9,3,8,10,8,9,10,7,6,12 Females 3,5,1,2,6,4,3,6,7,9,7,3,7,6,8,8 a. What is the null hypothesis? b. What is the research hypothesis? c. Why run a Two-Sample Assuming Equal Variances t-test? d. Interpret the findings. What are the results of the hypothesis test? Can you reject the null hypothesis?
The accompanying data file contains 20 observations for t and yt.
The accompanying data file contains 20 observations for t and yt. t 1 2 3 4 5 6 7 8 9 10 yt 12.1 12.2 9.3 12.8 8.6 9.3 14.3 12.1 11.5 15.1 t 11 12 13 14 15 16 17 18 19 20 yt 13.5 9.8 9.6 14.5 14.9 12.4 11.2 8.8 11.8 10 a. Discuss the presence of random variations. The smoother appearance of the graph suggests the presence of random variations. The smoother appearance of the graph...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
The accompanying data file contains 20 observations for t and yt. The data are plotted below....
The accompanying data file contains 20 observations for t and yt. The data are plotted below. t 1 2 3 4 5 6 7 8 9 10 yt 10.8 14.1 10.3 10.9 11.3 13.5 10.7 9.2 8.8 12 t 11 12 13 14 15 16 17 18 19 20 yt 9.8 11 15.1 12.5 12.9 12.3 9 14.9 10.1 11.9 b-1. Use the exponential smoothing method to make forecasts with α = 0.2. (Round intermediate calculations to at least 4...
Master file contains the most up-to-date and accurate data in the payroll system. This data is...
Master file contains the most up-to-date and accurate data in the payroll system. This data is input to the payroll system when payroll is processed. Discuss the issues/problems resulting when an unauthorized change is made to the data in the payroll master file and explain its consequences.
The data file "supermarket.csv" contains data on the total amount of eggs sold, relevant prices and...
The data file "supermarket.csv" contains data on the total amount of eggs sold, relevant prices and advertising information at a supermarket. 1) Treating the data as linear, run a multivariate regression of total egg sales on the three other variables. 2) Perform a log-log transformation of the data (Achieved by taking the natural logarithm of all variables except ad type). Run a multivariate regression of log total egg sales on log egg prices and log cookie prices (discard the dummy...
The data file contains the Body Mass Index (BMI) for a sample of men and a...
The data file contains the Body Mass Index (BMI) for a sample of men and a sample of women. Two of the columns, OW_male and OW_female code the BMI values as: 0 - if BMI ≤ 25.4 (these are considered “not overweight”); 1 - if BMI >= 25.5 (these are considered “overweight”). (a) Test whether there is sufficient evidence to show that the proportion of overweight males (proportion of males who are overweight) is different than the proportion of overweight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT