Thread: Printing a graph with data read in from a file

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Printing a graph with data read in from a file

    So I actually did this assignment awhile back, and now I have to combine the code into a new program, but when I went to test it out I realized it wasn't working. When I had submitted it I know it was working but now I can't get the bars themselves to print out, I'm thinking maybe I missed something and didn't save it? Here's the data that it should read in:

    Code:
    10 13
    1.131232 0.000183
    2.872323 0.000312
    4.233291 0.000422
    5.823823 0.000367
    6.464139 0.000316
    6.554324 0.000275
    6.001023 0.000245
    5.293232 0.0
    7.012323 0.000612
    7.013323 0.000583
    Here's what it should look like:
    Code:
    What file stores the car data?
    sample.txt
    
    95                                      
    90                                      
    85                                      
    80                                      
    75                                      
    70                                      
    65                                      
    60                                      
    55                                      
    50                                      
    45                                      
    40                                      
    35                                      
    30        *****                         
    25        ***** *****                   
    20        ***** *****                   
    15        ***** *****                   
    10  ***** ***** *****                   
    5   ***** ***** *****                   
    0   ***** ***** *****                   
        -----------------------------------
        00-05 05-10 10-15 15-20 20-25 25-30
    and this is what i've got:

    Code:
    #include<stdio.h>
    #define PI 3.14159
    
    void printGraph(int min, double milespg[]){ 
    	
    	int x=100, a, b;
    	while(x != 0){x -= 5; printf("%-4d", x);
    		for(a = 0; a <= (min-1); a++){
                for(b = 0; b < 5; b++){
    				
    				if(milespg[a]>=x)
    					putchar('*');
    				else
    					putchar(' ');}
    			putchar(' ');}
    		putchar('\n');}
    	
    	printf("    -----------------------------------\n");
    	printf("    00-05 05-10 10-15 15-20 20-25 25-30\n");
    }
    
    int main(){
        
        int min, in, a = 0, y;
        double totalrev = 0, totalgas = 0, rev, gas;
        char file[20];
        FILE *fp;
        double mpg[300];
    	
    	printf("What file stores the car data?\n");
        scanf("%s", &file);
    	
        fp = fopen(file, "r");
    	
        fscanf(fp, "%d", &min);
        fscanf(fp, "%d", &in);
        
        min = min / 5;
        while(a <= (min-1)){
    		for(y = 1; y != 301; y++){
    			fscanf(fp, "%lf %lf", &rev, &gas);
    			totalrev = totalrev + rev;
    			totalgas = totalgas + gas;
    		}
    		mpg[a] = (PI * in * 2 * totalrev / 63360) / totalgas;
    		a++;
    		totalrev = 0, totalgas = 0;
    	}
        
    	printGraph(min, mpg);
    	fclose(fp);
    	system("PAUSE");
    	return 0;
    }
    Can anyone see why it wouldn't be printing out the *'s?

  2. #2
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    Nevermind I figured it out, Xcode is weird and I had the file in the wrong place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  4. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM