Thread: Reversing a graph

  1. #1
    Registered User alpine's Avatar
    Join Date
    Oct 2010
    Posts
    6

    Reversing a graph

    So I've been tasked with printing out a graph that shows gas mileage for 5 minute intervals during a trip based on a file the user directs you to. Here's what mine currently prints out:

    Code:
    95                    
    90                    
    85                    
    80                    
    75                    
    70                    
    65                    
    60                    
    55                    
    50                    
    45                    
    40                    
    35                    
    30        *****       
    25        ***** ***** 
    20        ***** ***** 
    15        ***** ***** 
    10  ***** ***** ***** 
    5   ***** ***** ***** 
    0   ***** ***** ***** 
        -----------------------------------
        25-30 20-25 15-20 10-15 05-10 00-05
    I know there have been other threads about this program, but now I need to print it out backwards and a little more, which I haven't seen any cover yet. So the task is, I need to get the bars to come in from the right like they're moving from right off screen towards the left. The idea is that it's a prius and it updates your gas milage for the last thirty minutes. So it needs to look like this:

    Code:
    40						    *****
    35				              *****
    30                    *****       *****
    25                    ***** ***** *****
    20                    ***** ***** *****
    15                    ***** ***** *****
    10              ***** ***** ***** *****
    5               ***** ***** ***** *****
    0   		      ***** ***** ***** *****
        -----------------------------------
        25-30 20-25 15-20 10-15 05-10 00-05
    don't mind that it only goes up to 40 and that there's some stray stars, I just copied it from a word document but it's mostly intact. Here's what I've got so far:

    Code:
    #include<stdio.h>
    #define PI 3.14159
    #define SEC 300
    
    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("    25-30 20-25 15-20 10-15 05-10 00-05\n");
    }
    
    int main(){
        
        int min, in, a = 0, y;
        double totalrev = 0, totalgas = 0, rev, gas;
        char file[20];
        FILE *fp;
        double mpg[SEC];
    	
    	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;
    }
    I'm not really sure how to get the bars over there, and on top of it, if the file contains more than 30 minutes worth of data, for example 40, I need to make it skip the first 10 minutes and print only the latest 30. I know it's a lot to wrap your head around, but any help is appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have GOT to learn how to use that cloak of invisibility, when these kinds of tasks are being given out, young wizard.

    I'm thinking work the data in the MPG array, rather than the graphing array of characters. Fix that array at 30 minutes of data, and do the updates and shifting there. (the earliest row of data is replaced by the second to the earliest row of data, repeating for all rows. Newest data then goes in the latest row.)

    Then print the first 5 columns normally. The 6th and newest column of the graph, gets the slide in from the right hand side.

    Exactly how the code will go for the slide in, depends on your OS. Not horribly difficult, with Windows. Not sure about Linux (Ncurses is good to go, if you have that).

    That's how I'd do it - at least where I'd start. I'll try and pound out some sliding in code, a bit later.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a sliding version of my program. It also shifts the bars, from right to left, before bringing in the new data from the right (to make room for it).

    I used the original fuelmpg.txt data file, which only had three bars worth of data.

    For Windows user's, if you don't have conio.h you can include the windows.h file,
    then use SetConsoleCursorPosition(x,y), in your API, replacing gotoxy(x,y), and replace delay() with sleep(), or you'll REALLY have a slow slide!

    I have put up an executable of this file, at:
    swoopshare - Download FUELMPG.EXE

    I've also loaded up the data file as fuelmpg.txt, which you can d/l from
    swoopshare - Download fuelmpg.txt


    Code:
    /* earlier version of this is at fuelgraph1.c 
      This version has bar shifting, and the s-l-i-d-e. ;)
    */
    
    #include <stdio.h>
    #include <conio.h>
    #include <dos.h>
    
    #define PI 3.14159
    #define FEETMILE 63360
    #define MIN 300
    #define ROWS 10
    #define COLS 30
    
    void slide(char dat[ROWS][COLS]);
    
    void printGraph(double milesPG[], char dat[ROWS][COLS], int min) {
      int i,j,r,c,n; 
      double num=0.0;
    
      for(i=0;i<ROWS;i++) {   //set all dat elements to spaces
        for(j=0;j<COLS;j++)
          dat[i][j]=' ';
      }
      /* data[][] assignment block */
      printf("\n\n\n");
      for(i=0,c=0;i<min;i++) {  
        printf("\n %lf", milesPG[i]);
        num=milesPG[i]/5;        //to match vertical graduations
        for(j=0;j<num;j++) {
          dat[j][c]='*';
          dat[j][c+1]='*';
          dat[j][c+2]='*';
          dat[j][c+3]='*';
          dat[j][c+4]='*';
        }
        c+=6;
      }
    
      //print out block
      printf("\n\n");
      for(r=ROWS-1,n=45;r>-1;r--) {
        printf(" %2d| ", n);
        for(c=0;c<COLS;c++) {
          printf("%c",dat[r][c]);
        }
        putchar('\n');
        n-=5;
      }
      printf("     -----------------------------------\n");
      printf("     00-05 05-10 10-15 15-20 20-25 25-30\n");
    }
    void slide(char dat[ROWS][COLS]) {
      int c,i,j,k,n,x,y,r,maxcol=79,before,after;
      char aux[ROWS][5];
      for(i=0;i<ROWS;i++)
        for(j=0;j<5;j++)
          aux[i][j]=' ';
      //shift the columns
      for(i=0,j=0;i<ROWS;i++) {   //copy to aux
        for(j=0;j<COLS-12;j++) {
          if(i<ROWS && j<5) 
            aux[i][j]=dat[i][j];
          dat[i][j]=dat[i][j+6];
        }
      }
      //print out shifted graph
      printf("\n\n");
      for(r=ROWS-1,n=45;r>-1;r--) {
        printf(" %2d| ", n);
        for(c=0;c<COLS;c++) {
          printf("%c",dat[r][c]);
        }
        putchar('\n');
        n-=5;
      }
      printf("     -----------------------------------\n");
      printf("     00-05 05-10 10-15 15-20 20-25 25-30\n");
    
      //print out aux w/s-l-i-d-e ;)
      x=18; //18 = our target y
      y=ROWS+3;
      before = x; after=0; 
      for(k=0;k<maxcol-x;k++) {
        for(i=ROWS-1,j=0;i>-1;i--) {
          gotoxy(x,y);
          before=x;
          while(++before<maxcol-k) putchar(' ');
          if(aux[i][j]=='*') {
            putchar('*');
            ++j;
          }
          after=before+4;
          gotoxy(after, y++);
          while(after++<maxcol) 
              putchar(' ');
        }
        y=ROWS+3;
        delay(30);  
      }
    }
    int main() {
      int i, j, min, inches, length;
      double allRev=0, allGas=0, rev, gas;
      char file[20];
      char dat[ROWS][COLS];
      FILE *fp;
      double mpg[MIN];
    
      printf("\n\n\n");
      printf("What file stores the car data?\n");
      scanf("%s", &file); printf("\n");
      getchar();
    /*
          //for IDE debug only
      strcpy(file, "fuelmpg.txt");    //for debug in IDE only
      length = strlen(file);
      if(file[length-1]=='\n')       // ditto
        file[length-1]='\0';
    */  
      fp = fopen(file, "r");
      if(fp==NULL) {
        printf("\nError opening %s\n", file);
        return 1;
      }
      fscanf(fp, "%d", &min);
      fscanf(fp, "%d", &inches);
        
      min = min/5;
      i =allRev=allGas=0;
      while(i < min) {
        for(j=0;j<300;j++) {
          fscanf(fp, "%lf%lf", &rev, &gas);
          allRev+=rev, allGas+=gas;
        }
        mpg[i] = ( PI * inches * 2 * allRev / FEETMILE ) / allGas;
        allRev=0, allGas=0;
        ++i;
      }
      fclose(fp);  
      printGraph(mpg, dat, min);
      slide(dat);
      gotoxy(30,23); printf("press enter when ready");
      (void) getchar();
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ graph ploting
    By ivpavic in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2010, 12:06 PM
  2. error help making no sense
    By tunerfreak in forum C++ Programming
    Replies: 5
    Last Post: 04-17-2007, 07:55 PM
  3. Help w/ graph as adjacency matrix
    By ac251404 in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2006, 10:25 PM
  4. determining a path through the graph
    By Mist in forum C Programming
    Replies: 2
    Last Post: 02-27-2005, 12:21 PM
  5. Minimize crossing edges in undirected graph
    By Shiro in forum C Programming
    Replies: 0
    Last Post: 12-26-2001, 04:48 AM