Thread: im printing 3 lines of the same on all my lines, please help

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    6

    im printing 3 lines of the same on all my lines, please help

    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <math.h>
    #define input_file "c:\\engr 200\\train.txt"
    #define output_file "c:\\engr 200\\train_report.txt"
    
    
    /* Define the structure to represent a tsunami */
    struct trains
    {
       double time, displace, velocity;
    };
    /* Main Function */
    int main(void)
    {
       /* Declare and initialize variables */
       struct trains tr[51];
       int nrows,ncols, i, j;
       double displace, velocity, time;
       FILE *train, *train_report;
    
    
       /* Open input file */
       train = fopen(input_file,"r");
       train_report = fopen(output_file,"w");
       
       /* Verify input file, read control number, and hurricane data */
       if(train == NULL)
       {
          printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
          printf("\n\n   PROGRAM TERMINATED.\n\n\n");
          return 0;
       }
       else
       {
          /* Read control number */
          fscanf(train,"%i %i",&nrows,&ncols);
          
          /* Read train data into array */
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
                fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
                 &tr[i].velocity);
                }
          }
       }
    
    
       /* calculations */
          for(i=0; i<=nrows-1; i++)
          {
             for(j=0; j<=ncols-1; j++)
                {
                   tr[i].displace = 4.219*(exp(-1.58*time)-exp(-6.32*time));
                   tr[i].velocity = 26.67*exp(-6.32*time)-6.67*exp(-1.58*time);
                }
           }
      
          /* Print headings */
       printf("************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
              "\n\n\n  Time      Displacement     Velocity"
              "\n   (s)           (m)           (m/s)");
       fprintf(train_report,"************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
              "\n\n\n  Time      Displacement     Velocity"
              "\n   (s)           (m)           (m/s)");
          
       /* Print array */
       for(i=0; i<=nrows-1; i++)
          for(j=0; j<=ncols-1; j++)
          {
          printf("\n%6.3f         %6.3f         %6.3f",tr[i].time,
                 tr[i].displace,tr[i].velocity);
          fprintf(train_report,"\n%6.3f         %6.3f         %6.3f",tr[i].time,
                 tr[i].displace,tr[i].velocity);         
          }
       
       /* Print end */
       printf("\n\n************ END REPORT *************\n\n\n");
       fprintf(train_report,"\n\n************ END REPORT *************\n\n\n");
       
       /* Close the input file */
       fclose(train);
       fclose(train_report);
    
    
       /* Exit the program */
       return 0;
    }
    /******************************************************************************/
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be getting a couple of warnings about this code that need to be fixed:

    ||=== Build: Debug in c_homework (compiler: GNU GCC Compiler) ===|
    main.c||In function ‘main’:|
    main.c|19|warning: unused variable ‘velocity’ [-Wunused-variable]|
    main.c|19|warning: unused variable ‘displace’ [-Wunused-variable]|
    main.c|56|warning: ‘time’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
    ||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2016
    Posts
    6
    I think i fixed the warnings, but i am still printing 3 lines of the same of every line. I am using the dev c compiler if that helps. BTW thanks for your help.
    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <math.h>
    #define input_file "c:\\engr 200\\train.txt"
    #define output_file "c:\\engr 200\\train_report.txt"
    
    
    /* Define the structure to represent a tsunami */
    struct trains
    {
       double time, displace, velocity;
    };
    /* Main Function */
    int main(void)
    {
       /* Declare and initialize variables */
       struct trains tr[51];
       int nrows,ncols, i, j;
       FILE *train, *train_report;
    
    
       /* Open input file */
       train = fopen(input_file,"r");
       train_report = fopen(output_file,"w");
       
       /* Verify input file, read control number, and hurricane data */
       if(train == NULL)
       {
          printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
          printf("\n\n   PROGRAM TERMINATED.\n\n\n");
          return 0;
       }
       else
       {
          /* Read control number */
          fscanf(train,"%i %i",&nrows,&ncols);
          
          /* Read train data into array */
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
    			fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
    			 &tr[i].velocity);
    		    }
          }
       }
    
    
       /* calculations */
          for(i=0; i<=nrows-1; i++)
          {
             for(j=0; j<=ncols-1; j++)
                {
    			   tr[i].displace = 4.219*(exp(-1.58*tr[i].time)-
    			        exp(-6.32*tr[i].time));
    			   
                   tr[i].velocity = 26.67*exp(-6.32*tr[i].time)-
    			        6.67*exp(-1.58*tr[i].time);
                }
           }
      
          /* Print headings */
       printf("************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
       fprintf(train_report,"************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
    	  
       /* Print array */
       for(i=0; i<=nrows-1; i++)
          for(j=0; j<=ncols-1; j++)
          {
          printf("\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);
    	  fprintf(train_report,"\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);		 
          }
       
       /* Print end */
       printf("\n\n************ END REPORT *************\n\n\n");
       fprintf(train_report,"\n\n************ END REPORT *************\n\n\n");
       
       /* Close the input file */
       fclose(train);
       fclose(train_report);
    
    
       /* Exit the program */
       return 0;
    }
    /******************************************************************************/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
       for(i=0; i<=nrows-1; i++)
          for(j=0; j<=ncols-1; j++)
    So why do you have a j loop when you print, when j is never used in any of your print statements?

    In fact, all your nested loops have one subscript unused.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2016
    Posts
    6
    i don't know, im just following examples that my instructor gave me, if i knew what i was doing i would't be asking for help. but i did try to remove the j and place an i in its place, that made things worse.

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    6
    Quote Originally Posted by Salem View Post
    Code:
       for(i=0; i<=nrows-1; i++)
          for(j=0; j<=ncols-1; j++)
    So why do you have a j loop when you print, when j is never used in any of your print statements?

    In fact, all your nested loops have one subscript unused.
    i just removed the second for statement and it cleaned it up, thanks for the hint.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps you should study the instructor examples a bit more, to make sure you fully understand them.

    Simply copy/pasting things doesn't work.

    Ask yourself why you need to have a loop of columns, when you're reading 3 values at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2016
    Posts
    6
    ok i have added the last part to my code, and that is to print "The train car stopped moving forward at x.xxx seconds and x.xxx meters."
    how do I get this line to just print one time and to stop printing the rest?
    Code:
    /* Preprocessor directives */
    #include <stdio.h>
    #include <math.h>
    #define input_file "c:\\engr 200\\train.txt"
    #define output_file "c:\\engr 200\\train_report.txt"
    
    
    /* Define the structure to represent a tsunami */
    struct trains
    {
       double time, displace, velocity;
    };
    /* Main Function */
    int main(void)
    {
       /* Declare and initialize variables */
       struct trains tr[51];
       int nrows,ncols, i, j;
       FILE *train, *train_report;
    
    
       /* Open input file */
       train = fopen(input_file,"r");
       train_report = fopen(output_file,"w");
       
       /* Verify input file, read control number, and hurricane data */
       if(train == NULL)
       {
          printf("\n\n\n\n   ERROR OPENING INPUT FILE.");
          printf("\n\n   PROGRAM TERMINATED.\n\n\n");
          return 0;
       }
       else
       {
          /* Read control number */
          fscanf(train,"%i %i",&nrows,&ncols);
          
          /* Read train data into array */
          for(j=0; j<=ncols-1; j++)
          {
            for(i=0; i<=nrows-1; i++)
                {
    			fscanf(train,"%lf %lf %lf",&tr[i].time, &tr[i].displace,
    			 &tr[i].velocity);
    		    }
          }
       }
    
    
       /* calculations */
          for(i=0; i<=nrows-1; i++)
          {
             for(j=0; j<=ncols-1; j++)
                {
    			   tr[i].displace = 4.219*(exp(-1.58*tr[i].time)-
    			        exp(-6.32*tr[i].time));
    			   
                   tr[i].velocity = 26.67*exp(-6.32*tr[i].time)-
    			        6.67*exp(-1.58*tr[i].time);
                }
           }
      
          /* Print headings */
       printf("************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
       fprintf(train_report,"************ START REPORT ***********"
              "\n\n     TRAIN CAR STOPPING ANALYSIS"
    		  "\n\n\n  Time      Displacement     Velocity"
    		  "\n   (s)           (m)           (m/s)");
    	  
       /* Print array */
       for(i=0; i<=nrows-1; i++)
          {
          if(tr[i].velocity<0)
    	    {printf("\nThe train car stopped moving forward at %6.3f seconds and"
    		       " %6.3f meters.",tr[i].time,tr[i].displace);	
    	    }
    	else
    	{  
          printf("\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);
    	  fprintf(train_report,"\n%6.3f         %6.3f         %6.3f",tr[i].time,
    	         tr[i].displace,tr[i].velocity);
        }
          }
       
       /* Print end */
       printf("\n\n************ END REPORT *************\n\n\n");
       fprintf(train_report,"\n\n************ END REPORT *************\n\n\n");
       
       /* Close the input file */
       fclose(train);
       fclose(train_report);
    
    
       /* Exit the program */
       return 0;
    }
    /******************************************************************************/

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Translate a string matrix[lines][4] into one[lines][2]
    By muacsom in forum C Programming
    Replies: 3
    Last Post: 06-16-2012, 04:45 PM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. Printing an array in lines of 10.
    By furiousferret in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2004, 07:30 PM
  4. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM
  5. Printing multi-lines?
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2001, 04:52 PM