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

C program TextEdit File Edit Format View Window Help 1.0000000e+01 2.9265380e+03

ID: 3817393 • Letter: C

Question

C program

TextEdit File Edit Format View Window Help 1.0000000e+01 2.9265380e+03 5.0821200e+02 4.3231640e+01 2.0000000e+01 1.0170240e+04 9.2798610e+02 4.0723180e+01 3.0000000e+01 2.14862 60e+04 1.1832420e+03 1.0328000e+01 4.0000000e+01 3.3835080e+04 1.1882285e+03 -9.3307000e+00 5.0000000e+01 4.5250830e+04 1.0899705e+03 -1.0320900e+01 6.0000000e+01 5.5634490e+04 9.8935650e+02 -9.8019000e+00 7.0000000e+01 6.5037960e+04 8.9134700e+02 9.8000000e+00 8.0000000e+01 7.3461430e+04 7.9334750e+02 9.7999000e+00 9.0000000e+01 8.0904910e+04 6.9534750e+02 9.8001000e+00 1.0000000e+02 8.7368380e+04 5.9734 700e+02 -9.8000000e+00 1. 1000000e+02 9.2851850e+04 4.9934 700e+02 9.8000000e+00 1.2000000e+02 9.7355320e+04 4.0134 750e+02 9.7999000e+00 1.3000000e+02 1.0087880e+05 3.0334400e+02 -9.8008000e+00 1.4000000e+02 1.0342220e+05 2.0534500e+02 -9.7990000e+00 1.5000000e+02 1.0498 570e+05 1.3402000e+02 4.4660000e +00 1. 6000000e+02 1.0610260e+05 2.6304 000e+02 3.0270000e +01. 1.7000000e+02 1.1024650e+05 6.7618500e+02 5.2359000e +01 1.8000000e+02 1.1962630e+05 1.2929950e+03 7.1003000e+01 1.9000000e+02 1.3610640e+05 2.1234 700e+03 9.5092000e+01 2.0000000e+02 1.6209570e+05 3.1700000e+03 1.1421400e+02 2. 1000000e+02 1.9950640e+05. 3.8340050e+03 1.8587000e+01 2.2000000e+02 2.3877580e+05 3.8779450e+03 -9. 7990000 +00 2.3000000e+02 2.7706530e+05. 3.7799500e+03 9.8000000e+00 2.4000000e+02 3.1437480e+05. 3.6819500e+03 9.8000000e+00

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
//Reads the file contents and returns number of records
int ReadFile(float arr[], FILE *in)
{
//To count length
int len = 0;
//Checks if file pointer contains null then show error and exit
if (in == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}//End of if
//Loops till end of file
while(!feof(in))
{
//Reads data from file and stores in array
fscanf(in, "%f", &arr[len]);
//Increase the counter
len++;
}//End of while
// Close input file
close(in);
//Returns the length
return len;
}//End of function

//Prints data
void PrintData(float arr[], int len)
{
printf(" Acceleration Data ");
int x;
//Loops till length
for(x = 0; x < len-1; x++)
//Displays data
printf(" %f", arr[x]);
}//End of function

//Finds and displays the maximum Acceleration
void FindMax(float arr[], int len)
{
int x;
//Initially Assumes maximum is zero
float max = 0;
//Loops till length
for(x = 0; x < len-1; x++)
//If current value of array is greater than the earlier maximum
//Then update the maximum by the current value of array
if(arr[x] > max)
max = arr[x];
//Displays the maximum Acceleration
printf(" Maximum Acceleration = %f", max);
}//End of function

//Writes the positive Acceleration to Report.txt file
void WritePositive(float arr[], int len)
{
int x;
//Opens Report.txt file in write mode
FILE *fp = fopen("Report.txt", "w");
//Loops till end
for(x = 0; x < len-1; x++)
{
//Checks for positive
if(arr[x] >= 0)
//Writes data to file
fprintf(fp, "%f ", arr[x]);
}//End of for loop
}//End of function

//Main function
int main()
{
//Creates a float type array to store data from file
float arr[200];
//To store number of records in file
int length;
//Opens Acceleration.txt in read mode
FILE *fp = fopen("Acceleration.txt", "r");
//Reads the file
length = ReadFile(arr, fp);
//Displays the data in array
PrintData(arr, length);
//Finds and displays the maximum Acceleration
FindMax(arr, length);
//Writes the positive Acceleration
WritePositive(arr, length);
}//End of main

Output:

Acceleration Data

10.000000
2926.538086
508.212006
43.231640
20.000000
10170.240234
927.986084
40.723179
30.000000
21486.259766
1183.241943
-9.330700
50.000000
45250.828125
1089.970459
-10.320900
60.000000
55634.488281
989.356506
-9.801900
Maximum Acceleration = 55634.488281

Report.txt File contents

10.000000
2926.538086
508.212006
43.231640
20.000000
10170.240234
927.986084
40.723179
30.000000
21486.259766
1183.241943
50.000000
45250.828125
1089.970459
60.000000
55634.488281
989.356506