c programmers help

Discussion in 'Mac Programming' started by andrewface, Feb 7, 2007.

  1. macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #1
    i have an assignment due for school and i cant figure out one question (has to be in c)

    can someone help me write a program that takes two numbers that are input by the user and raises one to the power of the other...this cant be done with the exponent function it must be done by using looping

    thanks :apple:
     
  2. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #2
    Are they both integers? If so, it's relatively easy and you should figure it out on your own (sorry). If not, I have to figure you're in a pretty advanced programming class...
     
  3. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #3
    ya both integers and its a beginner class...which is why i have no idea what to do.. i have no programming experience what so ever
     
  4. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #4
    Let's assume these are positive integers. I don't want to ruin the whole thing for you, but I suggest using a for loop. You will need four variables, the two integers the user inputs, one two count in the for loop, and one for the final result. Have fun!

    EDIT: Now that I see your previous message, do you know how to make for loops?
     
  5. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #5
    awwwww!!! i got that far...i just cant get it to work uhg
     
  6. macrumors regular

    Joined:
    Jan 17, 2007
    #6
    Well you could have three...
     
  7. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #7
    yes i know the theory but this is my first time trying...i get very confused with the variables tho

    i want the program to go

    enter a number: x
    enter exponent: y

    x to the power of y: answer

    for (initial statement ; condition ; iterating statement) {
    body
    } right?
     
  8. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #8
    Okay. Assuming you already know how to get the user to input the values, the for loop should repeat until it goes through as many times as the exponent's value, and each time, it should multiply an additional integer (which should be initialized to 1) by the base number that the user inputted. This additional integer would be the answer.

    This would total four integers including the one that's being used to count in the for loop. CaptainZap, how would you do it with three? (I am no expert)
     
  9. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #9
    yes i know how to input values...can you show me what you mean for the for loop?...thats where i get confused the initializing
     
  10. macrumors regular

    Joined:
    Jan 17, 2007
    #10
    One for the Number/Result, one for the Exponent, and then another variable. I'll message you it since I don't want to spoil it for Andrew.
     
  11. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
  12. macrumors regular

    Joined:
    Jan 17, 2007
    #12
    Andrew, you aren't going to learn by having other people do the work, how about you post your code, and we can see what's wrong with it ^_^
     
  13. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #13
    ok should i try it with 4 or 3 variables?
     
  14. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #14
    I think I realize what CaptainZap is saying, but for now, I think you should stick with the four variable method.

    Time for some of the code, mixed with some pseudo code.

    int base = input from user;
    int exponent = input from user;
    int i =0;
    int result = 1;

    //For loop goes here, which will modify the result variable

    output result;

    See if you can figure it out from here (not trying to be mean; just trying to teach a bit)
     
  15. macrumors regular

    Joined:
    Jan 17, 2007
    #15
    Yah, do 4 variables like XnavxeMiyyep said, it will be easier to manage.
     
  16. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #16
    k i dont understand loops

    #include <stdio.h>
    main()
    {
    int n, b, e, i=0;

    printf("Please enter a number:");
    scanf("%d\n", &b);
    printf("Please enter an exponent:");
    scanf("%d", &e);

    for (i=1
     
  17. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #17
    First of all, you will find that, with the exception of counting variables, like i, you want to use descriptive words to name your variables instead of letters, or confusion will ensue!

    Also, the result (which I assume is n), needs to be initialized to 1, not 0. Otherwise, when you multiply it by the base over and over again (HINT HINT) , it would just stay at 0.

    for(i=0;i<e;i++) //Although, e should really be named exponent
    {
    //You're really close now. You can do this with just one line of code.
    }
     
  18. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #18
    #include <stdio.h>
    main()
    {
    int answer=1, base, exponent, i=0;

    printf("Please enter a number:");
    scanf("%d\n", &base);
    printf("Please enter an exponent:");
    scanf("%d", &exponent);

    for (i=1;i<exponent;i++) {

    printf("%d", base*i);
    }

    return 0;
    }


    i know thats wrong...why am i so bad at this?...im having alot of trouble understanding loops
    do i put i=0 at the beginning like that?
     
  19. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #19
    Okay, a couple things. The printf does not actually save any results, so all this will do is print out the base times i over and over again. Your printf should come after the loop, and you should just print out the answer. (So I guess I was sort of unclear about only needing one more line. You just needed one line IN THE LOOP)

    To calculate the answer, you want to keep multiplying it by the base over and over again (as this is how exponents are calculated). Good luck.
     
  20. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #20
    ok so

    answer=base*i

    }

    printf("%d", answer); ??
     
  21. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #21
    answer = base*answer;

    Otherwise, you've just made a randomly long multiplication function.:p
     
  22. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #22
    i dont understand that tho...answer = base*answer
     
  23. thread starter macrumors regular

    andrewface

    Joined:
    May 17, 2006
    #23
    #include <stdio.h>
    main()
    {
    int answer=1, base, exponent, i=0;

    printf("Please enter a number:");
    scanf("%d\n", &base);
    printf("Please enter an exponent:");
    scanf("%d", &exponent);

    for (i=0;i<exponent;i++) {
    answer=base*answer;

    }
    printf("%d", answer);
    return 0;
    }

    //hmm ok it works now theres just a problem with my inputs
    i get this
    Please enter a number:2
    2 //should be 2nd input after asks for exponent
    Please enter an exponent:4 //answer
    q2 has exited with status 0.
     
  24. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #24
    You're setting answer to be a value that's equal to what its current value times base is. The '=' is not the algebraic equal sign, it is a sign that means "get".
     
  25. macrumors 65816

    XnavxeMiyyep

    Joined:
    Mar 27, 2003
    Location:
    Washington
    #25
    This code should work correctly.
     

Share This Page