hi everybody
firstly, my english is not good,so I'm sorry!now I try to explain my question. I wrote a small program and I didn't find a sollution my question,so I need your help
question:
a. Using an editor, create a file containing the following car numbers, number of miles driven, and number of gallons of gas used by each car:
Car No. Miles Driven Gallons Used
54---------- 250 ----------- 19
62---------- 525---------- 38
71---------- 123---------- 6
85 ---------- 1322---------- 86
97---------- 235---------- 14
b. Write a C program that reads the data in the file created in part a and displays the car number, miles driven, gallons used, and the miles per gallon for each car as in the table above. The output should also display the total miles driven, total gallons used, and average miles per gallon for all the cars. These totals should be displayed at the end of the table.
SYNTAX:
Code:
#include<stdio.h>
int main(void)
{
int car,mile,gallon,
sum_mile=0,sum_gallon=0;
FILE *inp;//pointer for input file
inp=fopen("cars.txt","r");
//read to file and write the screen
printf("Car No. Miles Driven Gallons Used\n");
printf("------------------------------------------------------\n");
fscanf(inp,"%d%d%d",&car,&mile,&gallon);
printf("%d \t\t %d \t\t\t %d\n ",car,mile,gallon);
fscanf(inp,"%d%d%d",&car,&mile,&gallon);
printf("%d \t\t %d \t\t\t %d\n ",car,mile,gallon);
fscanf(inp,"%d%d%d",&car,&mile,&gallon);
printf("%d \t\t %d \t\t\t %d\n ",car,mile,gallon);
fscanf(inp,"%d%d%d",&car,&mile,&gallon);
printf("%d \t\t %d \t\t\t %d\n ",car,mile,gallon);
fscanf(inp,"%d%d%d",&car,&mile,&gallon);
printf("%d \t\t %d \t\t\t %d\n ",car,mile,gallon);
printf("-----------------------------------------------------\n");
fclose(inp);
return(0);
}
I did't write in this part: The output should also display the total miles driven, total gallons used, and average miles per gallon for all the cars. These totals should be displayed at the end of the table.
Good night!