Thread: having problem showing the correct value and using integer arithmetic

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ninjaman View Post
    Exited: ExitFailure 12 //i don't know what this is, it keeps appearing. im using codepad.org
    I suggest adding the standard "return 0" at the end of the main function.
    I never used codepad.org so just a guess on the C code you posted.

    Tim S.
    "...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

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The function main should be defined to return an int and you should return something from this function.
    Code:
    int main(void)
    {
    
       return 0;
    }
    If you don't provide a return value the compiler will return some value to the operating system, what is implementation defined.

    Jim

  3. #18
    Registered User
    Join Date
    May 2014
    Posts
    23
    Quote Originally Posted by jimblumberg View Post
    The function main should be defined to return an int and you should return something from this function.
    Code:
    int main(void)
    {
    
       return 0;
    }
    If you don't provide a return value the compiler will return some value to the operating system, what is implementation defined.

    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

    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);
    
    }
    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);
    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.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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:
    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)
    Fill in the ??? with the correct operator for each of those statements. You will use the exact same process for half dollars, quarters, etc.

    Hope that helps without giving it all away.

  5. #20
    Registered User
    Join Date
    Jul 2014
    Posts
    2
    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!!

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Cusp2207 View Post
    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 .
    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).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this purely arithmetic hex char to integer work?
    By XenoReseller in forum C Programming
    Replies: 3
    Last Post: 05-31-2012, 01:13 AM
  2. 40 digit integer arithmetic (arrays)
    By freddyvorhees in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2009, 12:03 PM
  3. how do i ensure a correct integer input
    By csonx_p in forum C++ Programming
    Replies: 11
    Last Post: 09-18-2008, 12:19 PM
  4. Problem with some arithmetic!
    By bobthebullet990 in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 10:04 AM
  5. arithmetic problem
    By stalker in forum C Programming
    Replies: 1
    Last Post: 10-29-2003, 08:48 AM