Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-12-2010
fyoung91
Task: Calculate a math problem with an independent variable entered by the user, in c++;

The problem I'm having is how to use the variable part, so if the equation was (2^K)/(20*K), what is the best way to set up :

So far Im on the lines of;

Code:

``` #include <stdio.h> #include <math.h> int main  {   double math, PI, x, K;   printf("Enter a vlaue for variable K: ");   PI = (2^K)/(20*k) ................```
Obviously the equation isn't correct, just a simplified example.***

Thanks.
• 08-12-2010
johnggold
I think you're looking for this.

Code:

`scanf("%d", &i);  /*  read an integer into i */`
This is just an example. If your variable is floating point, then use %f instead of %d.

The formatting options for scanf are about the same as for printf

Hope this helps.
• 08-12-2010
fyoung91
Had a go, and yeah forgot the line you mentioned, thanks.
Keep getting 'Illegal use of floating point' now though in line 13, the equation.

Code:

``` #include <stdio.h> #include <math.h> int main    {   double PI, K;   printf("Enter a vlaue for variable K: ");   scanf("%d", &K);   PI = (2^K)/(20*K);   printf("%d", PI);   return 0;   }```
Should the variable PI and K be floats perhaps?, does that matter?
• 08-12-2010
rags_to_riches
No, this: (2^K) is not a valid operator. Try pow() from the math library.
• 08-12-2010
johnggold
Try this :

Code:

``` #include <stdio.h> #include <math.h> void main    {   int a, b;   printf("Enter a value for variable a: ");   scanf("%d", &a);   b = (2^a)/(20*a);   printf("%d", b);     } or... for floating point void main    {   double a, b;   printf("Enter a value for variable a: ");   scanf("%f", &a);   b = ((double) 2^a)/((double)20*a);   printf("%f", b);     }```
Hope you can move on, now.
• 08-12-2010
darren78
You are using C instead of C++. If, as you say in your initial post, that you are required to write this in C++, then you need to disregard most of whats been said above and look up how to do it in C++,
• 08-12-2010
pianorain
Quote:

Originally Posted by johnggold
Hope you can move on, now.

Hope no reads your post and thinks it's actually a correct answer. void main? Really? And operator^ is not a power operator.
• 08-12-2010
johnggold
Sorry - missed that one whilst correcting the code.
He did say the formula was just an example.

Code:

```#include <stdio.h> #include <math.h> void main    {   int a, b;   printf("Enter a value for variable a: ");   scanf("%d", &a);   b = (int) pow( (double) 2, (double) a)/(20*a);   printf("%d", b);     } or... for floating point void main    {   double a, b;   printf("Enter a value for variable a: ");   scanf("%f", &a);   b = pow((double) 2,a)/((double)20*a);   printf("%f", b);     }```
OK?
• 08-12-2010
laserlight
Quote:

Originally Posted by johnggold
OK?

Well, it is still more of a C example than a C++ example, and then void main should really be int main() (i.e., you have a compile error right there). It is also rather strange that you cast integer literals to double when you can use double literals.
• 08-12-2010
johnggold
I've always done it that way - makes the constants stand out, and I write a lot of code with casting to double and back to int, so its easier to see whats what in complex equations conatining both.

The compiler doesn't care, so long as it knows what you want.
• 08-12-2010
laserlight
Quote:

Originally Posted by johnggold
I've always done it that way - makes the constants stand out, and I write a lot of code with casting to double and back to int, so its easier to see whats what in complex equations conatining both.

Interesting reasoning. I tend to side with the school of thought that casts should be avoided where feasible, and I do not quite buy your argument that this "makes the constants stand out", but I guess it is a matter of subjective style.
• 08-12-2010
johnggold
I've always believed that style is overrated, and gets in the way of the real objective of programming - delivering solutions.

We use beautifier software so that everyone gets to look at the same code in their own style. I prefer Polystyle software, which I have tuned exactly to my style.

I once interviewed a programmer who went on and on about his boss having a lousy programming style, using old compilers, saying that he could rewrite in much more up to date style.

My colleague asked him how old the code was. Years old was the reply.

Next question - how reliable is the code? - oh very reliable - hardly any support calls.

He never got the point, or the job.
• 08-12-2010
laserlight
Quote:

Originally Posted by johnggold
I've always believed that style is overrated, and gets in the way of the real objective of programming - delivering solutions.

If you truly believed that, then you would have written 2.0, not (double)2, because you would not have reasoned that your style, in your opinion, makes it "easier to see whats what in complex equations containing both". No, unless I am overestimating you, you do not believe that style is overrated: you believe that an excessive focus on style beyond what is necessary for readable code is overrated.
• 08-13-2010
johnggold
Because of the work we do, constants are always integer, unless you include PI, which is a predefined constant.

So casting with (double) is all that is required.

I think you are confusing your reasoning with that of others. Here's a typical code fragment.

Code:

```drawnormarc( (double) S_X  + ( (double) S_WIDTH  / (double) 2 ) + x1,                       (double) S_Y  + ( (double) S_HEIGHT / (double) 2 ) + y1,               (double) S_X  + ( (double) S_WIDTH  / (double) 2 ) + x2,               (double) S_Y  + ( (double) S_HEIGHT / (double) 2 ) + y2,               (double) S_X + ( (double) S_WIDTH  / (double) 2 ) + radius[RMA_WIDTH],               (double) S_Y + ( (double) S_HEIGHT  / (double) 2 ) + radius[RMA_HEIGHT],                 radius[RMA_RADIUS] );```
Should have changed font - it'll probably all go out of line when I post, but I hope you'll see the reasoning.

Most of our input values are integer, and for a lot of our outputs, we have to round back to integer.

Hope this helps
• 08-13-2010
laserlight
Quote:

Originally Posted by johnggold
I think you are confusing your reasoning with that of others.

I do not understand what you mean.

Quote:

Originally Posted by johnggold
I hope you'll see the reasoning.

I don't, but I don't find it unreadable either: that is what I mean by subjective style.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last