Question

Write a program in C that reads 20 float numbers from a file. The values are...

Write a program in C that reads 20 float numbers from a file. The values are stored in a vector (1-dimensional array). Find and display the smallest number, the second smallest number, and the third smallest number of the set. Use only these libraries: stdio.h, stdlib.h, math.h, and string.h. Below is an example of the input file (in1.txt) and the resulting output file (output.txt).

In1.txt:

3.14       4.05       -3.73      4.13       1.32

-2.21      0.46       4.57       4.64       -3.42

4.57       -0.14      3.00       -3.58      -0.78

2.92       4.594     1.55       -4.64      3.49

Output.txt:

The smallest number is: -4.64

The second smallest number is: -3.73

The third-smallest number is: -3.58

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Program:

#include <stdio.h>
#include <stdlib.h>

int main()
{
//File Pointer
FILE *fp, *ofp;

//Array declaration
float arr[20];
float firstMin, secondMin, thirdMin;
int i=0;

//Opening file
fp = fopen("in1.txt", "r");
ofp = fopen("output.txt", "w");

//Checking file opening status
if(fp == NULL)
{
printf("File Opening failed....");
return -1;
}

//Reading data from file
while(fscanf(fp, "%f", &arr[i]) == 1)
{
//Incrementing i
i++;
}

//Finding first second and third min elements
firstMin = arr[0];
secondMin = arr[0];
thirdMin = arr[0];

for(i=0; i<20; i++)
{
//First check
if(arr[i] < firstMin)
{
//Adjusting minimum values
thirdMin = secondMin;
secondMin = firstMin;
firstMin = arr[i];
}
//Second Check
else if(arr[i] < secondMin)
{
//Adjusting minimum values
thirdMin = secondMin;
secondMin = arr[i];
}
//Third Check
else if(arr[i] < thirdMin)
{
//Adjusting minimum values
thirdMin = arr[i];
}
}

//Writing results to file
fprintf(ofp, "The smallest number is: %.2f \n The second smallest number is: %.2f \n The third-smallest number is: %.2f", firstMin, secondMin, thirdMin);

//Closing files
fclose(fp);
fclose(ofp);

return 0;
}

__________________________________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Write a program in C that reads 20 float numbers from a file. The values are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Write a program using python that reads from values from a text file and plots them...

    Write a program using python that reads from values from a text file and plots them using matplotlib. The input data for each graph is on three lines of the input textfile. The first line contains the x-coordiates of the points of the graph, and the second line contains the y-coordinates of the points of the graph that correspond, in the same order, to the x-coordinates on the first line. The third line in each group of three lines may...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • Write a program in C language that reads scores and id numbers from a file, finds...

    Write a program in C language that reads scores and id numbers from a file, finds the average score, and assigns each student a grade based on the following rules: Each score > average + 20 gets an A Each score between average + 10 and average + 20 gets a B Each score between average - 10 and average + 10 gets a C Each score between average - 20 and average - 10 gets a D Each score...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Write the C program, to achieve as shown in the sample code execution, using the given...

    Write the C program, to achieve as shown in the sample code execution, using the given struct and using the comments in the given main program below: typedef struct{ char first[20]; char last[20]; float gpa; int score; } student; int main(void){ student *ptr; //first name //last name //student gpa //student score } //ask a user to enter the number of students, num //dynamically allocate memory for an array of students of the appropriate size (i.e. //num that the user just...

  • 1. Two manufacturing processes are being compared to try to reduce the number of defective products...

    1. Two manufacturing processes are being compared to try to reduce the number of defective products made. During 8 shifts for each process, the following results were observed: Line A Line B n 181 | 187 Based on a 5% significance level, did line B have a larger average than line A? *Use the tables I gave you in the handouts for the critical values *Use the appropriate test statistic value, NOT the p-value method *Use and show the 5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT