Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Input: The program should prompt the user to enter the name of two binary files.

ID: 3540527 • Letter: I

Question

Input: The program should prompt the user to enter the name of two binary files. Each

file contains a series of double-precision values; one represents a series of resistance

values, while the other contains the corresponding voltage drop across those resistors.

Note that:

-If the user enters the name of a file that cannot be opened, print an error

message and repeat the prompt for the name.

- You may assume that each input file holds the same number of values, and that

number will be no more than 20.

- After reading each file name, the program should read the contents of the file into

an array that stores these values. Look at the output test files on the course web

page (r1v1_out.txt, r2v2_out.txt, r3v3_out.txt) to see a list of the

values in each input file.

- If an error occurs when reading a file, print an error message and exit the

program.

- Note that reaching the end of the file without reading the maximum

amount of data is not an error. If you attempt to read 10 values and only

get 5, for example, the remainder of the program should work strictly with

those 5 values.


The program should also prompt for the name of a file to store your output, which is

described below.


Output: After reading the input files, your program should print the following

information:

- A table of the resistance/voltage pairs read from the input files.

- Each quantity should be shown using 2 decimal places.

- You may assume the maximum resistance is 99,999.99 ohms.

- You may assume the minimum and maximum voltages are -99.99 and

99.99 volts, respectively.

- A separate table showing the minimum, maximum, and average values for three

quantities: the voltage drops, current flow, and power consumption across all

resistors. Note that:

- Voltage should be printed using 2 decimal places; current and power

should be printed using 4.

-You should calculate the current and power for each pair of R/V values

and store each number in an appropriate array. For example, if the

resistance, voltage, and current values are stored in arrays res[],

voltage[], and current[], then current[0] is based on res[0]

and voltage[0].

-To calculate current, use the equation I (current) = V / R

- To calculate power, use the equation P = V * I

- After filling the current and power arrays, you can find the minimum,

maximum, and average value in each array before printing.


I don't understand how to work with a program that uses files such as open and closing files.


test files to use : http://tinyurl.com/kzkyc6r


Example of Output:

Explanation / Answer

#include <stdlib.h>

#include <stdio.h>


int main()

{

FILE *rfile,*vfile;

char Rfile[100];

char Vfile[100];

char output[100];

  

do{

printf("Enter name of file containing resistance values: ");

scanf("%s",Rfile);

  

rfile = fopen(Rfile,"rb");

  

if(!rfile)

printf("Wrong filename or file does not exist. Please enter again ");

else

break;

  

}while(1);

  

  

do{

printf("Enter name of file containing voltage drop values: ");

scanf("%s",Vfile);

  

vfile = fopen(Vfile,"rb");

  

if(!vfile)

printf("Wrong filename or file does not exist. Please enter again ");

else

break;

  

}while(1);

  

int size = 0;

double res[20];

double voltage[20];

double current[20];

double power[20];

  

double minres = 99999.99, maxres=0;

double minvol = 99.99, maxvol=-99.99;

double maxcur = maxvol/minres;

double mincur = -maxcur;

double maxpower = maxvol * maxcur;

double minpower = minvol * mincur;

  

double sumvol=0,sumcur=0,sumpow=0;

  

while( fread(&res[size],sizeof(double),1,rfile) && size<20)

{

if(!fread(&voltage[size],sizeof(double),1,vfile))

{

printf("Error occurred - VFile does not have corresponding value ");

return 0;

}

current[size] = voltage[size]/res[size];

power[size] = voltage[size] * current[size];

sumvol += voltage[size];

sumcur += current[size];

sumpow += power[size];

size++;

}

  

printf("Enter name of output file: ");

scanf("%s",output);

  

FILE *op = fopen(output,"w");

  

fprintf(op," R (ohms) V (volts) ");

int i;

  

for(i=0;i<size;i++)

{

fprintf(op,"RV pair %3d: %8.2lf %8.2lf ",i,res[i],voltage[i]);

if( voltage[i] > maxvol )

maxvol = voltage[i];

if( voltage[i] < minvol )

minvol = voltage[i];

if( current[i] > maxcur)

maxcur = current[i];

if( current[i] < mincur)

mincur = current[i];

if( power[i] > maxpower)

maxpower = power[i];

if( power[i] < minpower)

minpower = power[i];

}

fprintf(op," ");

  

fprintf(op," Voltage Current Power ");

fprintf(op,"MIN %8.2lf %8.4lf %8.4lf ",minvol,mincur,minpower);

fprintf(op,"MAX %8.2lf %8.4lf %8.4lf ",maxvol,maxcur,maxpower);

fprintf(op,"AVG %8.2lf %8.4lf %8.4lf ",sumvol/size,sumcur/size,sumpow/size);

  

return 0;

}