Open Side Menu Go to the Top
Register
C Program - Check my line plz! :) C Program - Check my line plz! :)

09-02-2009 , 08:26 PM
I am brand new to C and am in a class that requires me to write a program that calculates the costs of tickets bought. This is what I came up with after a couple hours . It compiles and runs fine but I feel like I am missing something...or it could have been easier...

Quote:
#include <stdio.h>

#define LOWER_BOWL_TIX_PRICE 50.00
#define UPPER_BOWL_TIX_PRICE 25.00

int main()
{

int lower_bowl_tickets, upper_bowl_tickets, number_of_tickets;
float sales_tax;

printf("How many lower bowl seats do you want?\n");
scanf("%d", &lower_bowl_tickets);

printf("How many upper bowl seats do you want?\n");
scanf("%d", &upper_bowl_tickets);

printf("For many games do you want to buy these tickets?\n");
scanf("%d", &number_of_tickets);

printf("What is the sales tax percentage in your locale?\n");
scanf("%f", &sales_tax);

printf("Your total price is $%.2f\n", (((sales_tax / 100) *
(lower_bowl_tickets*LOWER_BOWL_TIX_PRICE + upper_bowl_tickets*UPPER_BOWL_TIX_PRICE)*number_of _tickets) +
(lower_bowl_tickets*LOWER_BOWL_TIX_PRICE + upper_bowl_tickets*UPPER_BOWL_TIX_PRICE)*number_of _tickets));

system("PAUSE");
return 0;
}
Learn me the secrets plz!

Also, I was trying to multiply floats x ints for the longest time using a printf("yaddayadda %d\n", (int+int) * float...is that even possible?
C Program - Check my line plz! :) Quote
09-02-2009 , 08:48 PM
Look into casting and auto-casting for what happens when you multiply things that don't have the same size.

What dates are the tickets for?

Double check that you are satisfying all the requirements.

Double check your math, looks like you are charging them for twice the amount of tickets they want but only charging tax on the first group.

You aren't initializing your variables, does C do that for you?
C Program - Check my line plz! :) Quote
09-02-2009 , 09:10 PM
Quote:
Originally Posted by kerowo
Look into casting and auto-casting for what happens when you multiply things that don't have the same size.

What dates are the tickets for?

Double check that you are satisfying all the requirements.

Double check your math, looks like you are charging them for twice the amount of tickets they want but only charging tax on the first group.

You aren't initializing your variables, does C do that for you?
Not sure what casting/auto-casting is?

Quote:
Objective
1. To give students practice at typing in, compiling and running a simple program.
2. To learn how to read in input from the user.
3. To learn how to use assignment statements and arithmetic expressions to make calculations.

Problem A: Buying Football Tickets
You are excited to be in your first semester at college but are disappointed that football tickets are allocated through a lottery. Since you had a successful business in high school, you have plenty of money to buy your own football tickets so that you can guarantee your seats. In order to simplify the process, you decide to use your newly acquired C programming skills to aid you (and anyone else) in calculating the total cost of purchasing some football tickets.

Here is the information your program will ask the user to enter:

1. The number of lower bowl tickets he/she wants to buy.
2. The number of upper bowl tickets he/she wants to buy.
3. The number of games worth of tickets he/she wants to buy.
4. The sales tax in the locale in which the tickets are being bought, as a percentage.

Prompt the user to input these four values. You will output a single statement with the total cost of the football tickets.

Please define the following constants in your code:

#define LOWER_BOWL_TIX_PRICE 50.00
#define UPPER_BOWL_TIX_PRICE 25.00

Input Specification
1. The number of lower and upper bowl tickets will be integers in between 0 and 100, inclusive.
2. The number of games the user will be buying tickets for will be in between 0 and 7, inclusive.
3. The sales tax will be a real number percentage in between 0% and 20%.

Output Specification
Output the cost for all of the football tickets with tax rounded to two decimal places. Your output should follow the format below, where XX.XX is the cost in dollars for all of the football tickets.

The total cost of your football tickets is $XX.XX.



Output Sample
Here is one sample output of running the program. Note that this test is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

Sample Run
How many lower bowl seats do you want?
2
How many upper bowl seats do you want?
4
For many games do you want to buy these tickets?
5
What is the sales tax percentage in your locale?
6.5
The total cost of your football tickets is $1065.00.
That was the complete guidelines for the project.

I think the math is correct.

And I think that when the user enters the variables called for from the scanf functions that initializes them?
C Program - Check my line plz! :) Quote
09-02-2009 , 09:33 PM
I'm not sure why this line is in there twice:
(lower_bowl_tickets*LOWER_BOWL_TIX_PRICE + upper_bowl_tickets*UPPER_BOWL_TIX_PRICE)*number_of _tickets)

Look up casting in your text book or one of hundreds of on-line sites for learning C, it should be right by where it talks about data types and their sizes. It basically tells you what happens when you try to put too much data into variable.

Check and see what kind of input data validation the prof wants you to do. What is supposed to happen if you try and buy 101 tickets or use a tax rate greater than 20%.
C Program - Check my line plz! :) Quote
09-02-2009 , 10:49 PM
Quote:
Originally Posted by kerowo
Look up casting in your text book
The professor is probably smart enough not to give him an assignment that requires topics he has not yet taught.

Assigning an int into a float is generally safe, unless he's anticipating some huge numbers (float's got 24 bits dedicated to precision).

The big problem mixing floats and ints is comparisons, like with ==. Sometimes you'll do some math and end up testing a float that "should be" 74, against an int that is 74, but the float value came out as 73.9999999. Your code's logic expects it to be a match, but it won't. OP won't encounter that problem in this simple exercise, since he's just printing out the result and not doing anything with it

That said, OP might get some harmless compiler warnings on some platforms. Because these are doubles, not floats:

#define LOWER_BOWL_TIX_PRICE 50.00
#define UPPER_BOWL_TIX_PRICE 25.00

OP should use:

#define LOWER_BOWL_TIX_PRICE 50.00f
#define UPPER_BOWL_TIX_PRICE 25.00f

Or better yet, use variables because using #defines for constants is dumb and sloppy (gives up all type checking, etc):

float LOWER_BOWL_TIX_PRICE = 50.00f;
float UPPER_BOWL_TIX_PRICE = 25.00f;

Quote:
Originally Posted by kerowo
I'm not sure why this line is in there twice
OP is using one for base price (i.e. 100% * price) and one if for tax amount (i.e. 4% * price). It's correct, but ugly. It is a good idea to clean up long and obfuscated expressions.

printf("Your total price is $%.2f\n", (((sales_tax / 100) *
(lower_bowl_tickets*LOWER_BOWL_TIX_PRICE + upper_bowl_tickets*UPPER_BOWL_TIX_PRICE)*number_of _tickets) +
(lower_bowl_tickets*LOWER_BOWL_TIX_PRICE + upper_bowl_tickets*UPPER_BOWL_TIX_PRICE)*number_of _tickets));

could be simplified as

printf("Your total price is $%.2f\n", (1.0f + sales_tax / 100) * (lower_bowl_tickets * LOWER_BOWL_TIX_PRICE + upper_bowl_tickets * UPPER_BOWL_TIX_PRICE));

or even better as:

float base_price = lower_bowl_tickets * LOWER_BOWL_TIX_PRICE + upper_bowl_tickets * UPPER_BOWL_TIX_PRICE;
printf("Your total price is $%.2f\n", (1.0f + sales_tax / 100.0f) * base_ price);

or for supreme nittery:

float base_price = lower_bowl_tickets * LOWER_BOWL_TIX_PRICE + upper_bowl_tickets * UPPER_BOWL_TIX_PRICE;
float total_price = (1.0f + sales_tax / 100.0f) * base_ price;
printf("Your total price is $%.2f\n", total_price);

BTW, consistently using floats throughout an entire expression means never having to worry about accidental conversion to int truncating some intermediate value and screwing up your results.
C Program - Check my line plz! :) Quote
09-02-2009 , 11:29 PM
Tough to talk about datatypes in C and not about what happens when you mix and match them. However, it's been 10 years since I played with any of this and I think I tossed my C books when I moved last month so I'm not sure...

Any equation that takes 3 lines is too long though.
C Program - Check my line plz! :) Quote
09-03-2009 , 10:17 AM
probably my C++ learnings coming thru here, but we were taught to always prefer const variables over #define, as in:

Code:
#define LOWER_BOWL_TIX_PRICE 50.00
#define UPPER_BOWL_TIX_PRICE 25.00
becomes

Code:
const float LOWER_BOWL_TIX_PRICE = 50.0f;
const float UPPER_BOWL_TIX_PRICE = 25.0f;
(note the trailing 'f' which is sort of a short-hand cast, i guess)

the reason is that when the pre-compiler sees a #define, it blindly does a text substitution without doing any type checking, so you could end up with literally anything (eg. text) as an operand in an equation, which is probably bad.

also, whether or not scanf() initializes the variable, it's never a bad idea to define every int/long/float as 0 (or 0.0f). if you don't do this, the variable will have garbage data in it which could cause headaches when something's not working as expected.
C Program - Check my line plz! :) Quote
09-03-2009 , 11:47 AM
Is there a reason you're using the data type float instead of double? Get in the habit of always using double for your floating point variables, and you'll save yourself some confusion later on (ie, "Did I declare that a double or a float?" and "What happens when I add double x to float y?"). 64 bits for all your floating point variables (which is what you get with double as opposed to 32 bits for float) is perfectly reasonable, it's not like you'll be running out of memory. And having "too much" precision isn't going to hurt, but "too little" might. Just keep in mind that the format specifier on the scanf becomes %lf, not %f. You'll get used to that.
C Program - Check my line plz! :) Quote
09-03-2009 , 04:15 PM
Quote:
Originally Posted by ardubz
it's never a bad idea to define every int/long/float as 0 (or 0.0f). if you don't do this, the variable will have garbage data in it which could cause headaches when something's not working as expected.
There's a couple of counter-arguments to this.

1) Automatically putting a "= 0" at the end of every variable declaration can impair the compiler's ability to give you warnings about a variable being unused. The most common manifestation of this is when you do a "int tmp = 0;", and later on remove usage of tmp. The compiler can't warn you about unusued variable, because it is used - it was assigned to 0.

2) It's a programmer's responsibility to ensure every variable goes through a proper life-cycle, including instantiation before being used. Automatically putting "= 0" everywhere, in a sense, cheats. It absolves the programmer of this responsibility, is sort of a lazy way out, and can hide bugs from being discovered much longer than they would otherwise be (since zero often appears valid and will pass muster in common test cases).

One might argue that if you were going to automatically assign variables at declaration time as a standard operating procedure, it would probably be better to use a special value to represent uninitialized, like -131313 or 0x0DEF, and explicitly initialize it later in a more variable-appropriate and context-sensitive manner, rather than 0. Then, for instance, if OP commented out one of his scanf lines, he'd realize immediately that something was dreadfully wrong with his output ("negative price, wtf?!"), rather than reasonable looking output based on 0 tickets purchased.

This is a bit of a dogma/style thing, and you'll find lots of people who support your "always =0" philosophy.
C Program - Check my line plz! :) Quote

      
m