Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C problem.

Options
  • 23-03-2006 10:33pm
    #1
    Registered Users Posts: 6,833 ✭✭✭


    Well basically this is what I submitted for my final practical last term and I can't get it to work at all. I can't remember the error message it gave as I can't seem to get Cygwin to compile at home...
    Anyway the problem was to find out the least amount of coins needed to make up change for an amount less than €5.
    The attatched file is what I did but I can't find my mistakes.
    Any help greatly appreciated, I'd rather someone to explain what I'm doing wrong rather than solve the problem themselves as I'm supposed to learn this.
    Thanks


Comments

  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    It's been a while since I did anything with C, but I'll give it a look:

    Well, right off the bat, I see you've misspelt one of your functions.
    Also, you have a 'float variable declaration' section, but no float declarations! This bites you in the ass in a later value assignment (check the type of your pointers too).

    Is there a particular reason you're using Cygwin? If you're having trouble with your compiler, try another: This IDE is nice.


  • Registered Users Posts: 6,833 ✭✭✭Alkers


    Only using cygwin as it's what we use in college.
    I'll have another go tommorrow, too tired now, thanks though.


  • Closed Accounts Posts: 888 ✭✭✭themole


    well,

    in the function prototype for oid display_result you left out some of the "int" before the variable names.

    this line should be :
    int no_2euros, no_1euros, no_50cents, no_20cents, no_10cents, no_5cents, no_2, no_1cents;


    on this line:
    calc_change(amount, &no_2euros, &no_1euros, &no_50cents, &no_20cents, &no_10cents, &no_5cents, &no_2, &no_1cents);

    you have forgotten to pass the "int remain200, remain100, remain50, remain20, remain10, remain5, remain2, remain1);" values. which are themselves not declared correctly.

    this should be changed from:
    void calc_change(int amount, int *no_2euros, int *no_1euros, int *no_50cents, int *no_20cents, int *no_10cents, int *no_5cents, int *no_2, int

    *no_1cents, int remain200, remain100, remain50, remain20, remain10, remain5, remain2, remain1);

    to:
    void calc_change(int amount, int *no_2euros, int *no_1euros, int *no_50cents, int *no_20cents, int *no_10cents, int *no_5cents, int *no_2, int

    *no_1cents, int remain200,int remain100,int remain50,int remain20,int remain10,int remain5,int remain2,int remain1);

    as well as the values being pointers.

    but, tbh you should be using some sort of struct to hold all these valeus.

    its way too messy with all those variables.


  • Closed Accounts Posts: 71 ✭✭Mach


    Simona1986 wrote:
    Only using cygwin as it's what we use in college.

    Dude it dosen't matter weather you use DEV C++ or cygwin they to the same , as far as I know they doth are based on gcc.Personal if you want to program in c ditch windows and use Linux.


  • Registered Users Posts: 7,314 ✭✭✭Nietzschean


    for something like this it won't matter what compiler you use. And no requirement to ditch windows to program in C plenty of dev enviroments that are pretty good in windows .Net studio isn't too bad...

    Firstly your scanf was set for reading in a float where i assume you wanted %d for an integer....

    Anyway as mention'd before you should be going with something like a struct or an array to hold these values :
    struct money {
    int amount;
    int no_2euros;
    int no_1euros;
    int no_50cents;
    int no_20cents;
    int no_10cents;
    int no_5cents;
    int no_2cents;
    int no_1cent;
    } ;
    

    Then your making/using your functions is much simpler(you had incorrectly called your calc_change function) :
    void calc_change(struct money * change);
    
    ...
    calc_change(&change);
    

    Also in your calc_change function you created alot of variables remain200 ... remain1 , none of these are needed, and certainly don't need to be defined within the function's name(as they are local variables if you did want to use them, they don't need to be accessable from your main).

    Working through the change calcuator would easily be done using just one variable, the remainder at this point. i.e. once you calculate the number of 2 euro's in the change you take no_2euros * 200 and subtract that from ammount and use it as your running remainder.

    e.g.
            int remainder;
            //2 Euros :
            change->no_2euros=change->amount/200;
            remainder = change->amount - (change->no_2euros * 200);
    
    (worth noting i suppose the fact that change-> ammount /200 could return a rational/non integer number is fine, when assigning it to an int this info will just be chopped off)
    Now remainder will hold the remaining change to be divided up amongst smaller coins.
    so for 1euros one could do :
            //1 Euros :
            change->no_1euros=remainder/100;
            remainder -= change->no_1euros * 100;
    

    and so on...

    Code this is taken from is an example i made(well from your broken one) , compiles fine under
    gcc : gcc version 2.95.4 20020320 [FreeBSD]

    Giving output :
    Enter the amount of change needed in cent: 388
    Change is :
    1 2 Euro's
    1 1 Euro's
    1 50 Cents
    1 20 Cents
    1 10 Cents
    1 5 Cents
    1 2 Cent
    1 1Cent
    


  • Advertisement
  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Mach wrote:
    Personal if you want to program in c ditch windows and use Linux.

    Explain why?

    Good reasons only, please.


  • Closed Accounts Posts: 71 ✭✭Mach


    Sico wrote:
    Explain why?

    Good reasons only, please.

    1. Linux dosen't crash as much ans is virus free( as hardly no one writes virus for linux).

    2.All *inx come with gcc, which is open source, and has more more libiaries.

    3. The linux kernal, as long with most of the other program on it are written in C/C++.

    It's my personal choice to use linux, if the dude wants to use windows go a head.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Mach wrote:
    1. Linux dosen't crash as much ans is virus free( as hardly no one writes virus for linux).
    Its a lot harder to use though. A LOT harder. Dependancies can be hell unless the app you're trying to install is available from within Synaptic or some other package manager. Building apps from source can also be incredibly difficult at times with mis-matched versions all over the place.
    Mach wrote:
    2.All *inx come with gcc, which is open source, and has more more libiaries.
    I use GCC on windows...
    Mach wrote:
    3. The linux kernal, as long with most of the other program on it are written in C/C++.
    I'd be surprised if windows wasn't written in C or C++, but i can't see that making one bit of difference to anyone writing their own software. Who cares what your kernal is written in so long as it runs a C compiler.
    Mach wrote:
    It's my personal choice to use linux, if the dude wants to use windows go a head.
    Aye. But i still don't see any good reason why Linux is so much better than windows when it comes to coding in C/C++ that would make the switch to Linux worth it.


  • Registered Users Posts: 6,833 ✭✭✭Alkers


    Ok, thanks for the replies.
    I've only just started using C.
    This excercise was meant to be done using functions.
    I have never heard of a struct or an array.
    Have I gone about it in the right way for using functions or am I competely of the mark?
    I'm using programmers file editor and cygwin, not as a personal choice but because that's what we're thought on in college.
    Thanks


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Mach wrote:
    1. Linux dosen't crash as much ans is virus free( as hardly no one writes virus for linux).

    2.All *inx come with gcc, which is open source, and has more more libiaries.

    3. The linux kernal, as long with most of the other program on it are written in C/C++.

    I don't think any of these reasons are good enough to switch to Linux for programming.


  • Advertisement
  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    I'd be surprised if windows wasn't written in C or C++, but i can't see that making one bit of difference to anyone writing their own software. Who cares what your kernal is written in so long as it runs a C compiler.

    Windows is, of course, written almost entirely in C and C++, like every other modern operating system.

    Personally, I can't abide it, largely due to the interface, but there's no special reason it shouldn't be used for programming.


Advertisement