help with arrays and loops

This is a discussion on help with arrays and loops within the C Programming forums, part of the General Programming Boards category; OK, re-read my edited post up above yours, and let's see what else it needs....

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, re-read my edited post up above yours, and let's see what else it needs.

  2. #32
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok great! thats working now. so now my next problem is that i need the hours totaled and the charges totaled, which i have a function to calculate the charges, but as far as getting them all totaled and being able to put each value into the table, im kind of lost on how to write that loop

  3. #33
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    i know that in order to total them i need to do:
    totalHours= totalHours + hours
    charges[i]= calculateCharges(hours)
    totalCharges= totalCharges + charges[i]

    but how exactly do i get each value put into the table

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Check your output line of code - it may have been me getting ham-fingered, but it was lacking the f in the printf() line

    Code:
      printf("%d\t\t%.1f\t\t%.2\n", cars[i], hours, charges[i]);
    
    // should be:
      printf("%d\t\t%.1f\t\t%.2f\n", cars[i], hours, charges[i]);
    hmmm... takes in all eight cars hours at first, then calculates up all the charges later, is that right?

    Edit: add i+1 to the car number print line, so we're counting like "normal" people, instead of C programmers (who start from 0 when they count).
    Last edited by Adak; 11-26-2010 at 10:02 AM.

  5. #35
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    yeah it asks the user for the hours of each of the 8 cars and then after that it calculates the charges based on the amount of hours entered, and then it prints a table that lists each car, the hours for each car, the charge for each car, and then at the bottom it totals them all up

  6. #36
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    this is what my code is suppose to print out

    Enter the hours for car 1:
    Enter the hours for car 2:
    Enter the hours for car 3:
    Enter the hours for car 4:
    Enter the hours for car 5:
    Enter the hours for car 6:
    Enter the hours for car 7:
    Enter the hours for car 8:

    Cars Hours Charges
    1
    2
    3
    4
    5
    6
    7
    8
    TOTAL

    and then based on the inputs they will fill in the empty slots of the table

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, so we change the loop just a bit.

    Your for loop should be shortened to just:
    Code:
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:\n", i+1, cars[NUMBER_OF_CARS]);
      scanf("%f",&hours);
      totalHours = totalHours + hours;
      charges[i] = calculateCharges(hours);
      totalCharges = totalCharges + charges[i];
     }
    Moving the closing brace up about 3 lines.
    Last edited by Adak; 11-26-2010 at 10:06 AM.

  8. #38
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    yeah thats how i have mine, but as far as printing the table, is this how my loop is suppose to look?

    Code:
      for(i=0, i<NUMBER_OF_CARS; i++){
    printf("CARS\t\tHOURS\t\tCHARGES");
    printf("%d\t\t%.1f\t\t%.2f", cars[i], hours, charges[i]);
    printf("TOTAL\t\t%.1f\t\t%.2f", totalHours, totalCharges):
    }
    i feel like thats kind of close but not exactly
    Last edited by pmooney12; 11-26-2010 at 10:18 AM.

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After the for loop you have now, you need another for loop, to print out the stats for each car. The lines of code you had were fine, they just need to be put inside a for loop:

    Code:
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("\n");
      printf("Car\t\tHours\t\tCharge\n");
      printf("%d\t\t%.1f\t\t%.2f\n", cars[i], hours, charges[i]);
     }
    Check the math to be sure the charges and such are accurate, (I did not), and if you get stumped, post back.

  10. #40
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok so i made a few tweaks and my table is set up correctly but my calculations are wrong this is what i got

    Enter the hours for car 1:4

    Enter the hours for car 2:5

    Enter the hours for car 3:3

    Enter the hours for car 4:6

    Enter the hours for car 5:7

    Enter the hours for car 6:8

    Enter the hours for car 7:7

    Enter the hours for car 8:6
    Cars Hours Charges
    1 6.0 2.50
    2 6.0 3.00
    3 6.0 2.00
    4 6.0 3.50
    5 6.0 4.00
    6 6.0 4.50
    7 6.0 4.00
    8 6.0 3.50
    Total 46.0 27.00p

    apparently i need to make hours an array also, so i did that

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
    int main(void)
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     float hours[NUMBER_OF_CARS];
     float totalHours= 0.0;
     float totalCharges= 0.0;
     int i;
    
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("\nEnter the hours for car %d:",i+1, cars[i]);
      scanf("%f",&hours[i]);
      totalHours= totalHours + hours[i];
      charges[i]= calculateCharges(hours[i]);
      totalCharges= totalCharges + charges[i];
      }
      printf("Cars\t\tHours\t\tCharges");
      for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("\n");
      printf("%d\t\t%.1f\t\t%.2f", cars[i], hours[i], charges[i]);
     }
     printf("\nTotal\t\t%.1f\t\t%.2f", totalHours, totalCharges);
    
     return 0;
    }
     float calculateCharges(float hours[i])
    {
     float cHours=ceil(hours[i]);
     float charge=0.0;
     int i;
      if (cHours <= 3.0)
       charge=2.00;
      else if (cHours>3.0 && cHours <= 17.0)
        charge= ((cHours-3.0)*0.5)+2.0;
      else
        charge=10.0;
      
       return charge;
    }
    and this is my error message

    pgm39prog6.c:34: error: `i' was not declared in this scope
    pgm39prog6.c: In function `float calculateCharges()':
    pgm39prog6.c:36: error: `hours' undeclared (first use this function)
    pgm39prog6.c:36: error: (Each undeclared identifier is reported only once for
    each function it appears in.)
    pgm39prog6.c:36: error: `i' undeclared (first use this function)


    Ive tried fixing it a few times and nothing is really working

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't want a parameter named i, and a variable named i both, in the same function. Since you don't use the int i in calculateCharges(), just delete it, and keep the i that you have in the parameter hours[i].

    In your function prototype for float calculateCharges(float);, you need to change it, since you're bringing down an array now. (Arrays are degraded to just pointers when passed to other functions). So the new prototype should be:

    Code:
    float calculateCharges(float *);
    or
    float calculateCharges(float hours[i]);

  12. #42
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    sorry but i think im confused on which int i you are telling me to delete

  13. #43
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You are passing a simple float to calculateCharges(), right? So the fundtion should be
    Code:
    float calculateCharges(float hours)
    Inside that you must refer only to hours.

  14. #44
    Registered User
    Join Date
    Nov 2010
    Posts
    35
    ok here is my program

    Code:
    #include <stdio.h>
    #include <math.h>
    
    float calculateCharges(float);
    const int NUMBER_OF_CARS= 8;
    
     main()
    {
     int cars[NUMBER_OF_CARS]= {1,2,3,4,5,6,7,8};
     float charges[NUMBER_OF_CARS];
     float hours[NUMBER_OF_CARS];
     float totalHours= 0.0;
     float totalCharges= 0.0;
     int i;
    
     for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("Enter the hours for car %d:",i+1, cars[i]);
      scanf("%f",&hours[i]);
      totalHours= totalHours + hours[i];
      charges[i]= calculateCharges(hours[i]);
      totalCharges= totalCharges + charges[i];
     }
      printf("\nCars\t\tHours\t\tCharge");
      for(i=0; i<NUMBER_OF_CARS; i++)
     {
      printf("\n");
      printf("%d\t\t%.1f\t\t%.2f", cars[i], hours[i], charges[i]);
     }
      printf("\nTOTAL\t\t%.1f\t\t%.2f", totalHours, totalCharges);
    
     return 0;
    }
    float calculateCharges(float hours)
    {
     float cHours=ceil(hours);
     float charge=0.0;
      if (cHours <= 3.0)
       charge=2.00;
      else if (cHours>3.0 && cHours <= 17.0)
        charge= ((cHours-3.0)*0.5)+2.0; 
      else
        charge= 10.0;
      
      return charge;
    }

    any idea why when i submit it to this site called webcat, i get this error message

    Your program 'pgm39prog6.c' had compilation errors.
    Please fix the errors and submit when correct.
    Note: If it compiled for you, be aware that non-standard C++ extensions (e.g., MS Visual C++ additions, including headers) can cause problems.

    any ideas? I thought everything in my program was strictly C-programming. it works just like its suppose to

  15. #45
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two things:

    1) You still haven't added "int" to main(). I'm surprised you even got it to compile without a warning.

    2) A lot of compilers are dual C/C++ combo's. Be sure your programs file name has the extension .c and not .cpp *.cpp file names will be handled by the C++ compiler, if present, by default.

    You should have a setting under options, that adjusts this, btw. Some students have found that their program was set to compile with the C++, instead of the C compiler, even though it had a .c file name extension.

    For my old turbo C compiler, I had to remove the const int line of code, and replace it with
    #define NUMBER_OF_CARS 8

    note that the above has NO semi-colon at the end of the line, and no equal sign, either.

    I don't see anything else that could be construed as C++. One of the above, is very likely the problem.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and loops
    By pmooney12 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 09:38 PM
  2. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  3. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  4. Help using arrays and loops for math
    By LLINE in forum C++ Programming
    Replies: 3
    Last Post: 06-09-2008, 04:09 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21