Thread: i am a bit lost with this one

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    32

    i am a bit lost with this one

    [chapter three]
    Write single C or C++ statements that do the following:

    a. Input integer variable x with scanf.
    b. Input integer variable y with scanf.
    c. Initialize integer variable i to 1.
    d. Initialize integer variable power to 1.
    e. Multiply the variable power by x and assign the result to
    power.
    f. Increment the variable i by 1.
    g. Test i to see if it is less than or equal to y in the
    condition of a while statement.
    h. Output the integer variable power with printf.


    any hints on where to start?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sjcc View Post
    any hints on where to start?
    a. Input integer variable x with scanf.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    I have gotten that far I believe.

    this is what i have so far

    #include <stdio.h>
    int main ()
    {
    int x;
    int y;
    int i =1;

    scanf("%d", &x);
    scanf("%d", &y);

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    from step D is where i am starting to get lost.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sjcc View Post
    from step D is where i am starting to get lost.
    d looks incredibly similar to c, if you consider "power" and "i" to be the name of two int variables.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    #include <stdio.h>
    int main ()
    {
    int x;
    int y;
    int i =1;
    power =1;
    scanf("%d", &x);
    scanf("%d", &y);

    is that correct?
    Last edited by sjcc; 03-15-2009 at 06:19 PM. Reason: it was wrong

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before the variable name, put in the name of the data type for it:

    not

    Code:
    power = 1;
    but

    Code:
    int power = 1;
    And use code tags (highlight your code and press the # icon near the smilies menu).

    Edit:
    To avoid a problem with scanf() (which leaves newlines behind when used), add one space to your scanf() lines of code:

    not
    Code:
    scanf("%d", &x);
    but
    Code:
    scanf(" %d", &x);
    Last edited by Adak; 03-15-2009 at 06:32 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    int i =1;
    int power =1;
    If you put code tags around your code in the post, like this
    [code\]
    int main();
    [/code\]

    except without the inner \'s, people will be much happier and it will look better too.

    I am sure it is a lot to absord, learning C and the use of programming forums at the same time. You might want to read one of the STICKY threads at the top, such as:

    << !! Posting Code? Read this First !! >>
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    [code]#include <stdio.h>
    int main ()
    {
    int x;
    int y;
    int i =1;
    int power=1;

    scanf( "%d", &x);
    scanf( "%d", &y);

    power= power * x;

    [\code]

    i was wondering about the power = power * x did i do that right or should it have an int power= power * x?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Lower code tag has the backslash, and needs the forward slash to work: /

    Once you declare a variable with a datatype, the compiler will always remember what it is. Only declare it's type, once.

    Edit:

    You don't have to put in a reason for editing, unless you want to.
    Last edited by Adak; 03-15-2009 at 06:47 PM.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    Code:
    #include <stdio.h>
    int main ()
    {
    int x;
    int y;
    int i =1;
    int power=1;
    
    scanf( "%d", &x);
    scanf( "%d", &y);
    
    power= power * x;
    i was wondering about the power = power * x did i do that right or should it have an int power= power * x?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just one int power (some how, at the top of the function), is fine. Don't repeat it.

    Your assignment states power should be assigned the value of 1, so your int goes with that first assignment (which declares the variable, power).

    Lookin' good!

    Add the space I mentioned in scanf(), though. Don't get lazy.

    Not
    scanf( "%d", &x);

    but
    scanf(" %d", &x);

    see the difference?

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    thanks.

    Am i any closer to finish it now?

    Code:
    #include <stdio.h>
    int main ()
    {
        int x;
        int y;
        int i =1;
       
        
        scanf( "%d", &x);
        scanf( "%d", &y);
        
        int power= int power * x;
        
      while (1) {
           
           scanf("%d", &y);
           
    
           if(i <=y)
           break;

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sjcc View Post

    i was wondering about the power = power * x did i do that right or should it have an int power= power * x?
    You did it right. You only need to include the datatype when you declare the variable; after that it is that datatype (in this case int)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have the start of your while loop, but no increment for i yet.

    Add that.

    Also, the test for i being <= y has to be in the condition of the while loop,
    so

    while(1), with a break inside it, is not right.
    Last edited by Adak; 03-15-2009 at 06:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bitwrap
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 06-11-2005, 12:12 PM
  4. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM