Some 'C'

Soldato
Joined
27 Jun 2006
Posts
6,333
Hello there. :)

Just learning some C programming at Uni and am wondering how to tackle this question.

Now it's extremely simple but I didn't manage to get the answer and the solutions weren't posted up on the net so I'm just wondering. (If this isn't okay by the mods then feel free to close, but it's actually some honest revision :D as opposed to 'do my homework' please)

Code:
Question 1
Complete the following ‘C’ program that calculates the total cost of 10 pencils.

#include <stdio.h>

void main (void) {
[COLOR="Red"]   int numpencils;
   float unitcost;[/COLOR]

[COLOR="Lime"]   numpencils = 10;
   unitcost = 0.25;[/COLOR]

   printf("Total cost of %d pencils at £%.2f each is £%.2f\n",
                numpencils, unitcost, [COLOR="Yellow"]/* Insert code here */[/COLOR]);
}

How do I go about completing that?

I'm sort of complicating it for myself by thinking the following but I suppose I'll have a go and see what comes of it:

  • float total; (added onto)
  • total = numpencils*unitcost; (added onto)
  • total (instead of)

Is that anywhere near right or completely retarded?

I'm really not sure but would appreciate some help, even if it is simple in terms of the overall reach of the language. :)

Thanks.
 
i'd write the printf like this.

Code:
printf("Total cost of %d pencils at £%.2f each is £%.2f\n",
                numpencils, unitcost, (numpencils*unitcost));
 
i'd write the printf like this.

Code:
printf("Total cost of %d pencils at £%.2f each is £%.2f\n",
                numpencils, unitcost, (numpencils*unitcost));

Ah, that's most likely the right answer. :)

Could the answer be written in the way I have it though (which probably isn't correct in terms of how it is formed)? We're learning calculations for small programs at the moment, so the calculation is done once and that is it.

Our lecturer says that when it comes to bigger programs and we need a calculation to appear more often, we'll store it in a variable.
 
Ah, that's most likely the right answer. :)

Could the answer be written in the way I have it though (which probably isn't correct in terms of how it is formed)? We're learning calculations for small programs at the moment, so the calculation is done once and that is it.

Our lecturer says that when it comes to bigger programs and we need a calculation to appear more often, we'll store it in a variable.

You could do it your way, but it requires more lines of code than is really necessary.
 
You could do it your way, but it requires more lines of code than is really necessary.

Yeah, we're trying to make the code as lean as possible so I can understand why the first reply code would make more sense.

Thanks for your help - it's sort of give me a better understanding of it.

No doubt I'll be back sometime in the future with another problem - hopefully one a lot more complicated. :D
 
The other way to do it is how you described. Declaring a new variable, calculating the total seperately then displaying it is a much more logical and readable way to do it for a beginner.
 
Back
Top Bottom