The program must use a repetition logic (while, loop), and selection logic (if.
ID: 3808606 • Letter: T
Question
The program must use a repetition logic (while, loop), and selection logic (if. else). The code should be very basic and for beginners. No couts or vectors are to be used. Printf statements are okay.
You are in charge of a high school NFL football competition. A datafile called input.txt contains the data on each student. Each line of data consists of a students’s ID number (an integer), followed by his best 3 throws. A line containing 0 only terminates the data. A student qualifies for the competition if the average of his 3 throws is atleast 45.5 metres.
Write a program to read the data from input.txt and prints, for each student, his student number, his average throw and whether or not he qualifies. In addition print:
The number of students who qualify and the number who don't qualify,
The student with the highest average throw (ignore the possibility of a tie).
Explanation / Answer
Here is the code for you:
#include <stdio.h>
int main()
{
//You are in charge of a high school NFL football competition
FILE *fp;
//A datafile called input.txt contains the data on each student.
fp = fopen("input.txt", "r");
int ID, qualifiedCount = 0, disqualifiedCount = 0;
float throw1, throw2, throw3, avgThrow, highestAvgThrow;
if(fp == NULL)
{
printf("Unable to open the input file. Quitting... ");
return 0;
}
//A line containing 0 only terminates the data.
fscanf(fp, "%d", &ID);
while(ID != 0)
{
//Each line of data consists of a students’s ID number (an integer), followed by his best 3 throws.
fscanf(fp, "%f%f%f", &throw1, &throw2, &throw3);
//A student qualifies for the competition if the average of his 3 throws is atleast 45.5 metres.
avgThrow = (throw1 + throw2 + throw3) / 3;
// Write a program to read the data from input.txt and prints, for each student,
// his student number, his average throw and whether or not he qualifies.
if(avgThrow >= 45.5)
{
qualifiedCount++;
printf("%d %2f Qualified ", ID, avgThrow);
}
else
{
disqualifiedCount++;
printf("%d %2f Disqualified ", ID, avgThrow);
}
if(avgThrow > highestAvgThrow)
highestAvgThrow = avgThrow;
}
// In addition print:
// The number of students who qualify and the number who don't qualify,
// The student with the highest average throw (ignore the possibility of a tie).
printf("The number of qualified players are: %d ", qualifiedCount);
printf("The number of dis-qualified players are: %d ", disqualifiedCount);
printf("The highest average throw is: %.2f ", highestAvgThrow);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.