[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey guys I need help with an programming assignment in c. I've
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /wsr/ - Worksafe Requests

Thread replies: 27
Thread images: 9
File: hw1.png (17 KB, 1096x696) Image search: [Google]
hw1.png
17 KB, 1096x696
Hey guys I need help with an programming assignment in c. I've written the whole thing however when I compile it (after fixing all the errors) I'm still stuck with expression syntax error.

The basic premise is the program is suppose to sort out change and dollars and give the correct change back with the least amount of denominations. Can post more if someone responds.


Is there anyone here that can help with this sort of thing?
>>
>>121840
Can you post the whole thing?You might have an extra parenthesis or something and which compiler are you using?
>>
An integer array is not an integer. So your syntax error is the return type doesn't match the function type.

Since an array can be very big, the program never makes a copy of one just for the function. So any changes to one in a function will change it in the main program.

Fix your variable names too. Why does dollar6 match with dollar[5]? Why don't you start at dollarDenomination0? Why is dollar[1] and dollar[2] calculated differently from the rest of them?
>>
File: hw2.png (32 KB, 1265x898) Image search: [Google]
hw2.png
32 KB, 1265x898
Thanks for the responses guys, been editing it a lot since I've found more problems. In order to pass an array google says put * before the function name, but I feel like this is not correct for what I want to do.
>>
File: hw3.png (31 KB, 1271x899) Image search: [Google]
hw3.png
31 KB, 1271x899
Posting it once function at a time.
>>
File: hw4.png (49 KB, 1295x915) Image search: [Google]
hw4.png
49 KB, 1295x915
>>
File: hw6.png (19 KB, 1221x418) Image search: [Google]
hw6.png
19 KB, 1221x418
>>
File: hw5.png (37 KB, 1213x898) Image search: [Google]
hw5.png
37 KB, 1213x898
>>
File: hwmain.png (39 KB, 1214x896) Image search: [Google]
hwmain.png
39 KB, 1214x896
I'm pretty much an amateur here, but I managed to resolve the issue, but now when I compile it (using bcc32), the output is all garbage, so I've completely screwed up somewhere.
>>
>>121917
You don't need to return the array. Usually, a program copies a value for a variable to use only in the function, and forgets it after the function ends. But with an array, it just uses the original. The reason you can use an * is because that designates a pointer. If you don't know how to use pointers, don't use it. You don't need to return the array anyway, because the original (in the main program) has been changed already.

In the function getChangeAmount, you do not return the double. This means the program forgets it. I don't think you want this.

In the function splitChange, you try to return two integers. This won't work.
The solution involves the reference operator. Change the parameters to (double change, int &dollar, int &cent) and the function changes the originals just like it does with arrays.
>>
>>121928
You really need to initialize your arrays, When you declare an array, you don't know what the values are.
They could be the maximum value for integers as far as you know.
You must set all the values to zero with a for loop, or with int dollars[7] = { 0 };
>>
>>121932

so the function getChangeAmount, just leave it as void with no parameters? The splitChange code piece was given to us in the assignment to specifically change the double into integers (only the statement block, not the return or parameter values).
>>
>>121946
I thought elements in an array in c all have the value 0 unless given other inputs?
>>
>>121951
>so the function getChangeAmount, just leave it as void with no parameters?
As it stands, the function reads and forgets a value. You do need to return this value to main.

>The splitChange code piece was given to us in the assignment to specifically change the double into integers (only the statement block, not the return or parameter values).

The statement
return dollar, cent:
should not compile. If it does, it doesn't do what you think it should.

The way you work around the limitation of only returning one thing is to use references.
If you don't want to do that, use an integer array with two values.

I just noticed you don't have
change = getChangeAmount (change) ;
for any of your functions, so none of your returned values matter.

Either change that, or only use references for things you want to change.

>>121953
It may depend on the compiler and machine.

If you tested it, then that's fine.
>>
>>121953
arrays have garbage in them until they are initialized
>>
>>121962
The statement does compile but I'm pretty sure its returning garbage at this point since all my outputs are a mess. Would the solution to be remove the function entirely and put the code into the main?
>>
>>121977
I would split the function in to two, one that returns dollars, and one that returns cents.

If you want to debug, just add lines that print out values at different parts of your code.
>>
>>121983
Well now doing something unexpected it repeats Enter amount due twice before finishing.
>>
>>121988
What does main look like?
>>
File: hw7.png (27 KB, 915x513) Image search: [Google]
hw7.png
27 KB, 915x513
>>121988
Here is after I split the code. It's well past my bedtime so its probably wrong.
>>
File: hw8.png (31 KB, 1823x641) Image search: [Google]
hw8.png
31 KB, 1823x641
>>121989
>>
>>121991
>change = getChangeAmount (change) ;
This should be in main, not a function.
>>
>>121995
So it asks for the amount twice, but not three times? That's strange.
>>
>>122001
Overall three times, once correcty, but when I entered in a umber it printed it twice then Incorrect amount end a number between 5 and 95 ( for the cents which I need to change now)
>>
>>122010
When you have a function that returns a value, you should have in main
variable0 = function (parameter0, parameter1);

I would recommend you immediately print out variable0 (or whatever you call it) to make sure it is correct. When everything works, you can go back and delete the extra print lines.
Your getChangeAmount function should only be called once.

Although you can use references, your code is more readable if you don't. It's more of a workaround.

As for the cents, why is it, if I ask for 5.57 back, you give me just a 5 dollar note? Shouldn't I get a 5, a 50 cent piece, and a 5 cent piece?
So should you divide the number of cents by 5, then multiply it by 5?
>>
>>122018
The assignment states it only wants cents to be in factors of 5 so if I do type in 5.57 it should say invalid input, needs to be a multiple of 5 (so basically no rounding). At the moment no notes or cents are being printed back at me so I need to further develop my code further.

Appreciating all the help you've given me though
>>
>>122021
>The assignment states it only wants cents to be in factors of 5
That's fine.

Make sure to make the minimum 0 cents though, because you don't want to print an error when asked for 5 dollars flat.

Are you adding the extra print lines? Unless you have something like Visual Studio, it's the only way to make sure everything is correct, or find out what's wrong.
Thread replies: 27
Thread images: 9

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.