OK, re-read my edited post up above yours, and let's see what else it needs.
OK, re-read my edited post up above yours, and let's see what else it needs.
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
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
Check your output line of code - it may have been me getting ham-fingered, but it was lacking the f in the printf() line
hmmm... takes in all eight cars hours at first, then calculates up all the charges later, is that right?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]);
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 11:02 AM.
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
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
OK, so we change the loop just a bit.
Your for loop should be shortened to just:
Moving the closing brace up about 3 lines.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]; }
Last edited by Adak; 11-26-2010 at 11:06 AM.
yeah thats how i have mine, but as far as printing the table, is this how my loop is suppose to look?
i feel like thats kind of close but not exactlyCode: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): }
Last edited by pmooney12; 11-26-2010 at 11:18 AM.
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:
Check the math to be sure the charges and such are accurate, (I did not), and if you get stumped, post back.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]); }
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
and this is my error messageCode:#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; }
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
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]);
sorry but i think im confused on which int i you are telling me to delete
You are passing a simple float to calculateCharges(), right? So the fundtion should be
Inside that you must refer only to hours.Code:float calculateCharges(float hours)
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
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.