Thread: Quick array fix

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    31

    Quick array fix

    This code doesn't seem to be looping properly because it doesn't store the correct array values (in fact, the values are all set to 0 in the end). And I can't figure out why for the life of me..

    Code:
    for(i=0; i<min_in_trip/5; i++) {
        for(j=0; j<300; j++)
          fscanf(ifp, "%f%f", &temp, &temp2);
          numrevs += temp;
          gas += temp2;
        if(j == 300)
          array[i] = findfueleff(radius, numrevs, gas);
      }
    I really need this to work so that I can finish working on another program for homework that requires that the program that this code was derived from to work.

    Thanks!

    P.S. I have attached the .txt file that this program uses.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We like indentation here, but this is not Python, and indentation is no substitute for getting the braces right:
    Code:
    for(i=0; i<min_in_trip/5; i++) {
        for(j=0; j<300; j++) {
          fscanf(ifp, "%f%f", &temp, &temp2);
          numrevs += temp;
          gas += temp2;
        }
        if(j == 300)
          array[i] = findfueleff(radius, numrevs, gas);
      }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    (1)
    Code:
    >    for(j=0; j<300; j++)
    >      fscanf(ifp, "&#37;f%f", &temp, &temp2);
    >      numrevs += temp;
    >      gas += temp2;
    Assuming you want to sum all 300 items, you're missing some braces:
    Code:
        for(j=0; j<300; j++)
        {
          fscanf(ifp, "%f%f", &temp, &temp2);
          numrevs += temp;
          gas += temp2;
        }
    (2) Make sure temp and temp2 are declared as floats versus doubles, as %f is for reading floats. Use %lf to read doubles instead.

    (3) You should probably check the return of fscanf() to be sure it actually read two floats:
    Code:
          if (fscanf(ifp, "%f%f", &temp, &temp2) == 2)
          {
            /* Sum data */
          }
          else
          {
            /* Data not read */
          }

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Code:
    /* Definition libraries. */
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Defines constants. */
    #define PI 3.14159
    #define INCHES_IN_FEET 12
    #define FEET_IN_MILE 5280
    
    /* Initializes functions. */
    double findfueleff(double radius, double numrevs, double gas);
    void printGraph(double array[], int length);
    
    int main() {
      
      /* Defines variables. */
      FILE *ifp;
      int i, j;
      int min_in_trip, radius;
      double array[6], temp, temp2, numrevs, gas;
      char filename[20];
      
      /* Checks and opens input file. */
      do {
        printf("Enter the name of the file storing tire gas and tire rotation data.\n");
        scanf("%s", &filename);
        ifp = fopen(filename, "r");
      
        if(ifp == NULL)
          printf("File not found.\n");
      }
      while(ifp == NULL);
      printf("\n");
      
      /* Scans in length of trip and tire radius and determines array length. */
      fscanf(ifp, "%d%d", &min_in_trip, &radius);
      
      /* Scans in values from file. */
      for(i=0; i<min_in_trip/5; i++) {
        for(j=0; j<300; j++) {
          fscanf(ifp, "%lf%lf", &temp, &temp2);
          numrevs += temp;
          gas += temp2;
        }
        if(j == 300)
          array[i] = findfueleff(radius, numrevs, gas);
      }
        
      /* Prints out bar graph to display data. */
      printGraph(array, min_in_trip/5);
      
      /* Closes input file. */
      close(ifp);
      
      system("PAUSE");
      return (0);
    
    }
    
    /* Checks values that calculate fuel efficiency and returns the respective value. */
    double findfueleff(double radius, double numrevs, double gas) {
      if(numrevs == 0 && gas > 0)
        return 100;
      else if(numrevs == 0 && gas == 0) {
        return 0;
      }
      else
        return ((2*PI*radius/INCHES_IN_FEET/FEET_IN_MILE)*numrevs)/gas;
    }
    
    /* Creates bar graph. */
    void printGraph(double array[], int length) {
    
      int i, j;
      
      for(i=95; i>=0; i=i-5) {
        printf("%-4d", i);
        for(j=0; j<length; j++) {
          if(array[j]>=i)
            printf("***** ");
          else
            printf("      ");
        }
        printf("\n");
      }
    
      printf("    ");
      for(i=0; i<=34; i++) {
        printf("-");
      }
      printf("\n");
      printf("    ");
      printf("00-05 05-10 10-15 15-20 20-25 25-30\n");
      printf("\n");
    }
    Still not working with the above attached text file, so I decide to post the entire program instead.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It would probably be a good idea to initialize numrevs and gas.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    double array[6], temp, temp2, numrevs, gas; ..?

    ..unless there is something else I need to do.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I never said you hadn't declared them, I said you hadn't initialized them. Print the value of numrevs before the for-loop and see what you see.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    I just understood what you meant, and I set both variables equal to 0.

    But I'm not sure if it is printing the correct information still..

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In your code, when would this ever not be true:
    Code:
        if(j == 300)
    So what is the point in it?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM