Thread: I need help!!!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    7

    I need help!!!

    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!
    Last edited by redox; 12-09-2010 at 05:28 PM. Reason: I editted because numeric expressions were confusing

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What problem are you having? You already have sum variables laid out; you just need to add the values to them after reading each car in. Once you're done reading the cars in, calculate the average and print out the required information.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    By "solution to your question" do you mean "somebody to write it for you"? This isn't a homework shop: C Board - Announcements in Forum : General Programming Boards.

    You don't actually ask a question here. What is it you don't understand? You told us in plain English what you didn't do, but you failed to give us the code showing where you tried to accomplish your goal.

    Try to do this yourself, and if you fail, come back for help and show us what you tried that didn't work.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    7
    Quote Originally Posted by itsme86 View Post
    What problem are you having? You already have sum variables laid out; you just need to add the values to them after reading each car in. Once you're done reading the cars in, calculate the average and print out the required information.
    thank you very much

Popular pages Recent additions subscribe to a feed