Jump to content

Strange calculation errors? Anyone know why?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Atlas444

Atlas444

    Newbie

  • Members
  • Pip
  • 3 posts
I've recently been learning C in my free-time using a friends textbook. At the end of each chapter are exercises to practice programming. This particular exercise instructs you to: Write a program that takes a monetary amount (as an integer) and outputs the number of $20, $10, $5, and $1 bills to use.

I wrote the program and even compiled it using x-code (the included macintosh IDE), but it's outputting strange amounts. Here is the code:
#include <stdio.h>
int main()
{
	/*Computes how to pay using the smallest number of each bill*/
	int amount;
	int n20 = amount/20;   
	int n10 = ((amount-(n20*20))/10);
	int n5 = ((amount-((n20*20)+(n10*10)))/5);
	int n1 = ((amount-((n20*20)+(n10*10)+(n5*5)))/1);
	
	printf("Enter the amount to be payed: ");
	scanf("%d", &amount);
	
	printf("$20 Bills:%d\n",n20);
	printf("$10 Bills:%d\n",n10);
	printf("$5 Bills:%d\n",n5);
	printf("$1 Bills:%d\n",n1);
	
	return 0;
	
}
And here is an example of the output:

Enter the amount to be payed: Running…
93
$20 Bills:1638
$10 Bills:0
$5 Bills:1
$1 Bills:2

This exercise is from Ch.2 and even though I'm a few chapters ahead of that, I'm still very new. So please, help me out if you know what the heck is happening! And I have a thirst for knowledge so if you want to throw in some more complex stuff, feel free to but please attempt to explain it to me. Thanks

Atlas

Edited by WingedPanther, 10 February 2010 - 08:57 AM.
add code tags (the # button)


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Uh. Take input, then calculate output. Kinda like, "first get in the car, then drive to class". Reversing the order of that ain't gonna work.

#3
Atlas444

Atlas444

    Newbie

  • Members
  • Pip
  • 3 posts
I got it to function properly by moving the declarations that are initialized after the scanf call...but I don't understand why that would make a difference?
Can't you declare and initialize in the same line? And aren't declarations supposed to go before anything else?

Is it because I'm using the declaration (int amount) in the declarations after before it's been assigned a value?

#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Yes. In the code you originally posted, you are performing calculations on 'amount' before you have assigned 'amount' a value. It happens to contains some random bit of data and you perform calculations on that. Then you read in 'amount'. Then you print out the stuff you calculated from the random data and completely ignore the 'amount' that was input.

When you perform the calculations after you read the input, then all goes well.

And you can declare and initialize. But in C, all declarations must precede statements; this makes doing the initialization problematic if it's going to "initialize" based on later statements.