"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
The function main should be defined to return an int and you should return something from this function.
If you don't provide a return value the compiler will return some value to the operating system, what is implementation defined.Code:int main(void) { return 0; }
Jim
hello
im still unsure about how I would keep track of the pennies and using modulo. could someone point me in the direction of a page that could explain this.
I was thinking that you assign a value to pennies and then use modulo to get answers. I cant figure out how this works. there is an example that shows the use of modulo but I cant figure out
on this practice program I was required to write an execution list, after writing the program out I found I had to adjust the (int) to (short int) now it works and gives the correct answer. I think the program I was trying to write should be based on this but there are some things I don't understand about the above program. the last printf has what looks like one argument, (int) (cost..etc);Code:#include <stdio.h> void main (void) { int bytes, code, dollars; float cost, per_byte = 1.42; code = 3; bytes = code * 17 * sizeof (int); dollars + bytes * per_byte + .5; printf("%i code segments cost %i dollars \n", code, dollars); code += 1 ; bytes = code * 17 * sizeof(int); cost = bytes * per_byte; printf("%I code segments cost %f dollars. \n", code, cost); printf(" this time we count the %i pennies. \n", (int) (cost * 100 + .5) % 100); }
what is the second part exactly?
also, the "sizeof (int)", is this referring to "byte" at the beginning of that line? there are two "sizeof (int)". I have read in the book but it doesn't explain anything like this. it explains size of but not the use here.
Earlier, you mentioned that repeatedly subtracting the value of a dollar, half dollar, quarter, etc and counting how many times you could do that for each quantity should yield the correct result. Recall that repeated subtraction is basically division, just like multiplication is repeated addition.
Also, you should understand that in C, when working with integers, you have two "division" operators. One is / which gives the whole number quotient: how many times the divisor can go completely into the dividend. For example, 123 / 50 gives a result of 2 since 50 goes into 123 two whole times. The other operator is % (modulo or remainder). It gives the remainder when dividing. So 123 % 50 gives 23 since, after 50 goes in 2 whole times, there is still 23 left over.
Now, on to your problem. You don't need to "keep track of the modulo" separately. Simply reduce the amount of pennies as you go along. Basically, get rid of any pennies you already used to make a dollar, half dollar, quarter, etc.
Knowing that you have two operators you need to work with, / and %, hopefully you can figure out how this will work. Taking your example of $7.73:
Fill in the ??? with the correct operator for each of those statements. You will use the exact same process for half dollars, quarters, etc.Code:// it's always preferable to use constants instead of "magic numbers" #define DOLLAR_TO_PENNIES 100 #define HALF_DOLLAR_TO_PENNIES 50 // similar for other values ... int dollars, half_dollars, quarters, dimes, nickels, pennies; // here pennies is 773, the starting amount dollars = pennies ??? DOLLAR_TO_PENNIES; pennies = pennies ??? DOLLAR_TO_PENNIES; // now pennies should be equal to 73, the number of pennies left over after you covert as much as possible to dollar bills // do this for all other coins and at the end you'll have the number of pennies (3)
Hope that helps without giving it all away.
Hi,
Use the exact value of pi i.e 3.14159 and instead of using 4/3 write 1.33333.You will get the correct answer .
Also if you want to learn easily more about C programming you can refer to <Link Removed>
pi = 3.14159;
volume = (1.33333 * pi) * (radius * radius * radius);
Best o Luck!!
Not really. If you're going to give advice like that, it would pay to give advise that is usable.
Firstly, pi is an irrational value, and cannot be exactly represented in a finite number of digits. 3.14159 is still an approximation.
Second, 1.33333 is also an approximation, since 1/3 gives a recurring digit (there are an infinite number of 3's). If you really want to multiply by 4/3 in floating point, it is better to multiply by 4.0/3.0 (unless you have context to simplify the total expression in other ways).