exponents are LAAAME

This is a discussion on exponents are LAAAME within the C Programming forums, part of the General Programming Boards category; I'm having trouble writing exponents. I know initially how you're supposed to define a variable and use the pow() func. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    exponents are LAAAME

    I'm having trouble writing exponents. I know initially how you're supposed to define a variable and use the pow() func. but my scenario is a bit different and I need to compile and email this by tonight


    x = pow(4,6);
    something like this, where it's showing 4 to the 6th power. as i said before, my case is a little worse.
    when I (try to) compile it, it says "invalid operands to binary ^" then there's a whole LIST of all the variables I put in. "invalid suffix "z" on integer constant
    "invalid suffix "x" onasaifasfjaklsjgkasjg.. and so on.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    int p1;
    int p2;
    int p3;
    int p4;
    int p5;
    	printf("Directions:\n");
    	printf("Simplify the monomials.\n");
    	printf("Write out exponents using the "^" key.\n");
    	printf("12p^5 = 12 times p to the 5th power\n"); /* this is a thing I did for my math teacher for extra credit basically*/
    
    
    /*--------------------------------------------------------*/
    	printf("54x^3z, 36z^5x\n");
    	scanf("&#37;d", &p1);
    if (p1 != 18z)
    { printf("Incorrect\n"); }
    else
    { printf("Correct\n"); }
    system("pause");
    /*-----------------------------------------------------*/
    	printf("74ac, 116b^3a\n");
    	scanf("%d", &p2);
    if (p2 != 2a)
    { printf("Incorrect\n\a"); }
    else 
    { printf("Correct\n"); }
    system("pause");
    /*------------------------------------------------------*/
    
    	printf("2xyz^4t^3, 14pz^2y^5\n");
    	scanf("%d", &p3);
    if (p3 != 2z^2y)
    { printf("Incorrect\n"); }
    else
    { printf("Correct\n");}
    system("pause");
    /*---------------------------------------------------------*/
    	printf("6y^4, 12y^2\n"); 
    	scanf("%d", &p4);
    if (p4 != 6y^2)
    { printf("incorrect\n\a"); }
    else
    { printf("correct\n"); }
    system("pause");
    /*-------------------------------------------------*/
    	printf("8mp^3, 20x^7p^5\n");
    	scanf("%d", &p5);
    if (p5 != 4p^3)
    { printf("Incorrect\a\n"); }
    else
    { printf("Correct\n"); }
    
    	printf("Thank you for playing!\n");
    
    system("pause");
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And you believe that 18z is an integer, as opposed to oh, say, a string, because why?

    (Also the common factor -- if that's what you're doing -- of the first pair is 18xz.)

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    you are forgettign that you have to explicitly code the multiplication operator, 2z isnt 2 times z its an invlid variable, you have to code 2 * z instead, and remember the order fo operations so enclose (2*z)^(2*y). Also remember than in C/C++ the ^ operator is bitwise XOR not exponent. So what you really want is pow(2 * x , 2 * y);
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    so you're saying for every "variable" that is an invalid variable (2x) I have to write out as pow(2*x);? or something similar

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Between abachler and myself, one of us is completely horrendously wrong. I'm betting it's not me, but I would anyway. My guess is that you want the user to literally type "2z^2y" or "2yz^2" as the answer to the problem, which you would then need to compare (as a string, not an integer, naturally) to the right answer hardcoded in (which would also need to be a string, not an integer, naturally).

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    so I wouldn't define it as 'int', but as 'char'

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Sm33zy View Post
    so I wouldn't define it as 'int', but as 'char'
    Closer; you need a whole bunch of chars. A char array, perhaps.

    Of course, you will need to make this change throughout.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    it came down to this. This is a different copy from my original file so I screwed around with it a bit. I'm sure it's obviously still full of errors so I think I'll just call it a night
    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
    int p1= 18*2;
    int p2=2*4;
    int p3=2*z*z*y;
    int p4=6*y*y;
    int p5=4*p*p;
    	printf("Directions:\n");
    	printf("Simplify the monomials.\n");
    	printf("I believe the ^ key is invalid in C\n");
    	printf("plz write out your answer using parentheses.\n");
    	printf("ex. 2*x instead of 2x because 2x is an invalid variable here.\n");
    
    /*--------------------------------------------------------*/
    int x;
    int y;
    int z;
    	printf("54*x*y*2, 36*z*5*x\n");
    	scanf("%d/%d/%d",&x,&y,&z);
    if (p1 != 18*z)
    { printf("Incorrect\n"); }
    else
    { printf("Correct\n"); }
    system("pause");
    /*-----------------------------------------------------*/
    int a[2];
    int c;
    int b;
    
    	printf("74*a*c, 116*b*3*a\n");
    	scanf("%d/%d/%d",&a,&b,&c);
    if (p2 != 2*4)
    { printf("Incorrect\n\a"); }
    else 
    { printf("Correct\n"); }
    system("pause");
    /*------------------------------------------------------*/
    int x[2];
    int y[2];
    int z[2];
    int t;
    	printf("2*x*y*z*4*t*3, 14*x*z*2*y*5\n");
    	scanf("%d/%d/%d", &x,&z,&y);
    if (p3 != 2*z*z*y)
    { printf("Incorrect\n"); }
    else
    { printf("Correct\n");}
    system("pause");
    /*---------------------------------------------------------*/
    int y;
    	printf("6*y*2, 12*y*2\n"); 
    	scanf("%d", &y);
    if (p4 != 6*y*y)
    { printf("incorrect\n\a"); }
    else
    { printf("correct\n"); }
    system("pause");
    /*-------------------------------------------------*/
    int m;
    int p;
    int x;
    int p;
    	printf("8*m*p*3, 20*x*7*p*5\n");
    	scanf("%d/%d/%d", &m,&p,&x);
    if (p5 != 4*p*p)
    { printf("Incorrect\a\n"); }
    else
    { printf("Correct\n"); }
    
    	printf("Thank you for playing!\n");
    
    system("pause");
    
    return 0;
    }

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Do you even have a compiler? You don't have to guess that you have errors, a compiler will tell you that.

    Your first error is on line 4, main returns int.
    Next errors are on lines 8-10, you're pretending there is such thing as variables called z ,y, and p. I say 'pretending' because none of the lines before there declare any variable or constant by those names.

    I would liken you're attempt at programming to someone with a hammer who knows that it can be used to put nails into wood, but lays it down prodding it with his finger waiting for those nails to jump into holes. In other words, you don't have a friggin clue what you're doing!
    Last edited by iMalc; 11-21-2008 at 09:14 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fraction Exponents
    By ninjaturtle[k9] in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2004, 10:46 AM
  2. non-interger exponents
    By brane_sail in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2003, 12:41 AM
  3. God, i feel like a n00b again. How do i do exponents?
    By Inquirer in forum C++ Programming
    Replies: 13
    Last Post: 09-01-2003, 08:41 PM
  4. how to use exponents
    By guitargatler in forum C++ Programming
    Replies: 11
    Last Post: 02-01-2003, 08:09 PM
  5. For loop and exponents
    By TrazPFloyd in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2002, 05:19 AM

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