Program 9 (File I/O) Resistance input files * resfile1.bin ð? @ @ @ @ @ @ @ \"@
ID: 3720330 • Letter: P
Question
Program 9 (File I/O) Resistance input files
* resfile1.bin
ð? @ @ @ @ @ @ @ "@ $@ &@ (@ *@ ,@ .@ 0@ 1@ 2@ 3@ 4@
* resfile2.bin
À’@ È©@ Y@ àµ@ `}@ ?º@
* resfile3.bin
€K@fffff:?@ @?@ D@š™™™™™ñ? 0‘@ 1@?Âõ(A@ö(?ÂÕM@ À? @S@ À¢@ 0@ Ðs@ °y@
Voltage input files
* voltfile1.bin
ð? @ @ @ @ @ @ @ "@ $@ &@ (@ *@ ,@ .@ 0@ 1@ 2@ 3@ 4@
* voltfile2.bin
$À @ À 4@ @ @
* voltfile3.bin
À @ 4@ *@…ëQ¸Å1@ `WÀáz®GqT@Ház®Ç3@ €@À WÀ ÀV@ 2@ 1@ .@ €@@
Corresponding output files (Note: File names are based on input file names. For example, "r1v1_out.txt" is the output of using binary input files "resfile1.bin" and "voltfile1.bin".)
* r1v1_out.txt
R (ohms) V (volts) RV pair 0: 1.00 1.00 RV pair 1: 2.00 2.00 RV pair 2: 3.00 3.00 RV pair 3: 4.00 4.00 RV pair 4: 5.00 5.00 RV pair 5: 6.00 6.00 RV pair 6: 7.00 7.00 RV pair 7: 8.00 8.00 RV pair 8: 9.00 9.00 RV pair 9: 10.00 10.00 RV pair 10: 11.00 11.00 RV pair 11: 12.00 12.00 RV pair 12: 13.00 13.00 RV pair 13: 14.00 14.00 RV pair 14: 15.00 15.00 RV pair 15: 16.00 16.00 RV pair 16: 17.00 17.00 RV pair 17: 18.00 18.00 RV pair 18: 19.00 19.00 RV pair 19: 20.00 20.00
Voltage Current Power MIN 1.00 1.0000 1.0000 MAX 20.00 1.0000 20.0000 AVG 10.50 1.0000 10.5000
* r2v2_out.txt
R (ohms) V (volts) RV pair 0: 1200.00 -10.00 RV pair 1: 3300.00 5.50 RV pair 2: 100.00 -3.00 RV pair 3: 5600.00 20.00 RV pair 4: 470.00 4.00 RV pair 5: 6800.00 2.50
Voltage Current Power MIN -10.00 -0.0300 0.0009 MAX 20.00 0.0085 0.0900 AVG 3.17 -0.0040 0.0481
* r3v3_out.txt
R (ohms) V (volts) RV pair 0: 55.00 -5.00 RV pair 1: 999.30 2.00 RV pair 2: 1000.00 20.00 RV pair 3: 500.25 13.00 RV pair 4: 1.10 17.77 RV pair 5: 1100.00 -93.50 RV pair 6: 17.00 81.77 RV pair 7: 34.12 19.78 RV pair 8: 59.67 -33.00 RV pair 9: 0.13 -92.00 RV pair 10: 77.00 91.00 RV pair 11: 2400.00 18.00 RV pair 12: 16.00 17.00 RV pair 13: 317.00 15.00 RV pair 14: 411.00 33.00
Voltage Current Power MIN -93.50 -736.0000 0.0040 MAX 91.00 16.1545 67712.0000 AVG 6.99 -47.5172 4570.6896 Program 9 (File I/O) Resistance input files
* resfile1.bin
ð? @ @ @ @ @ @ @ "@ $@ &@ (@ *@ ,@ .@ 0@ 1@ 2@ 3@ 4@
* resfile2.bin
À’@ È©@ Y@ àµ@ `}@ ?º@
* resfile3.bin
€K@fffff:?@ @?@ D@š™™™™™ñ? 0‘@ 1@?Âõ(A@ö(?ÂÕM@ À? @S@ À¢@ 0@ Ðs@ °y@
Voltage input files
* voltfile1.bin
ð? @ @ @ @ @ @ @ "@ $@ &@ (@ *@ ,@ .@ 0@ 1@ 2@ 3@ 4@
* voltfile2.bin
$À @ À 4@ @ @
* voltfile3.bin
À @ 4@ *@…ëQ¸Å1@ `WÀáz®GqT@Ház®Ç3@ €@À WÀ ÀV@ 2@ 1@ .@ €@@
Corresponding output files (Note: File names are based on input file names. For example, "r1v1_out.txt" is the output of using binary input files "resfile1.bin" and "voltfile1.bin".)
* r1v1_out.txt
R (ohms) V (volts) RV pair 0: 1.00 1.00 RV pair 1: 2.00 2.00 RV pair 2: 3.00 3.00 RV pair 3: 4.00 4.00 RV pair 4: 5.00 5.00 RV pair 5: 6.00 6.00 RV pair 6: 7.00 7.00 RV pair 7: 8.00 8.00 RV pair 8: 9.00 9.00 RV pair 9: 10.00 10.00 RV pair 10: 11.00 11.00 RV pair 11: 12.00 12.00 RV pair 12: 13.00 13.00 RV pair 13: 14.00 14.00 RV pair 14: 15.00 15.00 RV pair 15: 16.00 16.00 RV pair 16: 17.00 17.00 RV pair 17: 18.00 18.00 RV pair 18: 19.00 19.00 RV pair 19: 20.00 20.00
Voltage Current Power MIN 1.00 1.0000 1.0000 MAX 20.00 1.0000 20.0000 AVG 10.50 1.0000 10.5000
* r2v2_out.txt
R (ohms) V (volts) RV pair 0: 1200.00 -10.00 RV pair 1: 3300.00 5.50 RV pair 2: 100.00 -3.00 RV pair 3: 5600.00 20.00 RV pair 4: 470.00 4.00 RV pair 5: 6800.00 2.50
Voltage Current Power MIN -10.00 -0.0300 0.0009 MAX 20.00 0.0085 0.0900 AVG 3.17 -0.0040 0.0481
* r3v3_out.txt
R (ohms) V (volts) RV pair 0: 55.00 -5.00 RV pair 1: 999.30 2.00 RV pair 2: 1000.00 20.00 RV pair 3: 500.25 13.00 RV pair 4: 1.10 17.77 RV pair 5: 1100.00 -93.50 RV pair 6: 17.00 81.77 RV pair 7: 34.12 19.78 RV pair 8: 59.67 -33.00 RV pair 9: 0.13 -92.00 RV pair 10: 77.00 91.00 RV pair 11: 2400.00 18.00 RV pair 12: 16.00 17.00 RV pair 13: 317.00 15.00 RV pair 14: 411.00 33.00
Voltage Current Power MIN -93.50 -736.0000 0.0040 MAX 91.00 16.1545 67712.0000 AVG 6.99 -47.5172 4570.6896
Explanation / Answer
PROGRAM:-
Point.h
#ifndef Point_h
#define Point_h
typedef struct {
double x; // X coordinate
double y; // Y coordinate
} Point;
// Print coordinates as (x.xx, y.yy)
void printPoint(Point *p);
// Read input coordinates
void readPoint(Point *p);
// Return distance between two points
double dist(Point p1, Point p2);
#endif / Point_h /
Rectangle.h
#ifndef Rectangle_h
#define Rectangle_h
#include "Point.h"
typedef struct {
Point vert[4]; // List of 4 vertices
// Functions all assume that vert[0] = lower
// left corner, vert[1] = upper left corner
// vert[2] = upper right corner,
// vert[3] = lower right corner
} Rectangle;
// Print contents of rectangle
void printRectangle(Rectangle *r);
// Print list of n Rectangles
void printList(Rectangle list[], int n);
// Returns area of rectangle
double area(Rectangle *r);
// Returns perimeter of rectangle
double perimeter(Rectangle *r);
// Returns 1 if two rectangles overlap; 0 otherwise
int overlap(Rectangle r1, Rectangle r2);
#endif / Rectangle_h /
Point.c
#include "Point.h"
#include <stdio.h>
#include <math.h>
// Print coordinates as (x.xx, y.yy)
void printPoint(Point *p)
{
// printf("** YOU MUST WRITE YOUR OWN VERSION OF printPoint() **");
printf("(%.2lf %.2lf)", p->x, p->y);
}
// Read input coordinates
void readPoint(Point *p)
{
// printf("** YOU MUST WRITE YOUR OWN VERSION OF readPoint() **");
scanf("%lf %lf", &p->x, &p->y);
}
// Return distance between two points
double dist(Point p1, Point p2)
{
// printf("** YOU MUST WRITE YOUR OWN VERSION OF dist() **");
return sqrt((p2->x - p1->x) (p2->x - p1->x) + (p2->y - p1->y) (p2->y - p1->y));
return 0;
}
Rectangle.c
#include "Rectangle.h" // Implicitly includes Point.h
#include <stdio.h>
// Print contents of rectangle
// Prints vertices in appropriate relative positions:
// vert[1] vert[2]
// vert[0] vert[3]
void printRectangle(Rectangle *r)
{
printPoint(&r->vert[1]);
printPoint(&r->vert[2]);
printf(" ");
printPoint(&r->vert[0]);
printPoint(&r->vert[3]);
printf(" ");
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printRectangle() ");
// printf("(%lf %lf) (%lf %lf) ", r->vert[1].x, r->vert[1].y, r->vert[2].x, r->vert[2].y);
// printf("(%lf %lf) (%lf %lf) ", r->vert[0].x, r->vert[0].y, r->vert[3].x, r->vert[3].y);
}
// Print list of n Rectangles
void printList(Rectangle list[], int n)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF printList() ");
int i, j;
for (i = 0; i < n; i++){
printRectangle(&list[i]);
printf(" ");
}
}
// Returns area of rectangle
double area(Rectangle *r)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF area() ");
double l = r->vert[3].x - r->vert[0].x;
double w = r->vert[1].y - r->vert[0].y;
return l*w;
}
// Returns perimeter of rectangle
double perimeter(Rectangle *r)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF perimeter() ");
double l = r->vert[3].x - r->vert[0].x;
double w = r->vert[1].y - r->vert[0].y;
return 2*(l+w);
}
// Returns 1 if two rectangles overlap; 0 otherwise
int overlap(Rectangle r1, Rectangle r2)
{
// printf("*** YOU MUST WRITE YOUR OWN VERSION OF overlap() ");
if(r1->vert[1].x > r2->vert[3].x || r2->vert[1].x > r1->vert[3].x)
return 0;
if(r1->vert[1].y > r2->vert[3].y || r2->vert[1].y > r1->vert[3].y)
return 0;
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.