Newbie help please.

This is a discussion on Newbie help please. within the C++ Programming forums, part of the General Programming Boards category; Task: Calculate a math problem with an independent variable entered by the user, in c++; The problem I'm having is ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    6

    Newbie help please.

    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.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    6
    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?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    No, this: (2^K) is not a valid operator. Try pow() from the math library.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86

    Smile

    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.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    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++,

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by johnggold View Post
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 11:27 AM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21