Refer to the following standard C function prototypes (you may not need all of t
ID: 3718102 • Letter: R
Question
Refer to the following standard C function prototypes (you may not need all of them):
FILE *fopen(char *path, char *mode); int fclose(FILE *fp); int fscanf(FILE *stream, char *format, ...); char *fgets(char *s, int size, FILE *stream); int fgetc(FILE *stream); int ferror(FILE *stream); size_t strlen(const char *s);
Also refer to the following declaration: typedef struct { double x; double y; } Point;
a) Write a function called readPoints to: • Import a filename as a char pointer — an input file, structured as follows: – The first line contains a single integer, representing the number of subsequent lines. – Each subsequent line contains two real numbers separated by a comma. Each pair of numbers represent x and y co-ordinates; i.e. a point in two dimensions.
For example: 4
2.35,6.14
5.5,7.0
0.0,-55.9
14084.1,39864.6
• Take a second parameter — the number of points/records, passed by reference. The readPoints function should set this value itself. • Read the contents of the file into a dynamically-allocated array of Point structs. • Return the new array, or NULL if the file could not be opened
(b) Write a function called calc() to: • Import an array of the type returned by readPoints, along with the number of points. • Perform the following calculation. For each point, the x and y values are multiplied. These individual products are then summed. Given the previous example file, the calculation would be as follows: (2.35 × 6.14) + (5.5 × 7.0) + (0.0 × ?55.9) + (14084.1 × 39864.6) • Return the result (a real number).
(c) Write a main() function to: • Take any number of command-line parameters, representing input files. • For each filename: – Call readPoints(), then when appropriate call calc(); – Print out the final result with 4 decimal places and a field width of 12; – De-allocate the array. (No action needs to be performed if no parameters are given.)
Explanation / Answer
PLEASE REFER BELOW CODE
#include<stdlib.h>
#include<stdio.h>
typedef struct point
{
float x,y;
}point;
point *readPoints(char filename[100], int *number_pts)
{
FILE *fp;
point *arr;
int count_points,i;
fp = fopen(filename, "r");
if(fp)
{
fscanf(fp,"%d ", &count_points);
*number_pts = count_points;
arr = (point*)malloc(sizeof(point));
for(i = 0; i < count_points; i++)
{
if(fscanf(fp, "%f,%f", &arr[i].x, &arr[i].y));
}
return arr;
}
else
{
return NULL;
}
}
float calc(point *arr,int number_pts)
{
int i;
float avg=0.0;
for(i = 0; i < number_pts; i++)
{
avg += (arr[i].x * arr[i].y);
}
return avg/number_pts;
}
int main(int argc, char *argv[])
{
int count_param,number_pts;
count_param = 1;
point *arr;
float avg;
while(count_param < argc)
{
arr = readPoints(argv[count_param], &number_pts);
count_param++;
if(arr == NULL)
printf(" File Not present ");
}
avg = calc(arr,number_pts);
printf(" Final result = %.4f ", avg);
free(arr);
return 0;
}
PLEASE REFER BELOW OUTPUT
khushal@khushal-ubuntu:~/Desktop/Chegg$ gcc -g Command_line.c -o Command_line
khushal@khushal-ubuntu:~/Desktop/Chegg$ ./Command_line file.txt
Final result = 140364272.0000
khushal@khushal-ubuntu:~/Desktop/Chegg$
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.