Thread: i am a bit lost with this one

  1. #31
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    Code:
    #include <stdio.h>
    int main ()
    {
        int x;
        int y;
        int i =1;
        int power;
       
      
        
        scanf(" %d", &x);
        scanf(" %d", &y);
        
        i= power * x;
       
    
        while (i<=y)
        { i++;
    
           }
           
           return 0;
           
           ++i;
           
           printf("int power is i");
           
           }
    is this right now

  2. #32
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Hi, the while loop needs to have:
    Code:
    power = power * x;
    statement in as well. Otherwise, you'll just find x^1.

    The specs seem a little out of order.

    a. input integer variable x with scanf
    b. input integer variable y with scanf
    c. initialise integer variable i to 1
    d. initialise integer variable power to 1
    e. while i <= y
    f. multiply power by x and assign the result to power
    g. increment i by 1
    h. output the integer variable power with printf.

    Code:
        while (i <= y) {
            power = power * x;
            i++;
        }
    I guess that's the point of the assignment though, to think about how to do the problem.

  3. #33
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Be aware that the program is finished when you reach a "return" statement in main().

    The printf documentation may seem confusing if you have never done any programming before. Anyway, you cannot include the variable in quotes (with C); it will just literally print "i".

    Code:
    #include <stdio.h>  /* needed for printf */
    
    int main() {
         char example[]="hello world";
         int x=5;
    
         printf("%s, x is %d\n", example, x);
         return 0;
    }
    The green part is the "template". The underlined bits are "conversion specifiers". For each conversion specifier in the template, you need a matching argument. In this example, there are two (in blue), one is a "string" (a char pointer) and the other one (important to you) is a "digit", which would be an integer.

    \n is a newline, try it without that to see the difference.
    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

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    int main ()
    {
        int x;
        int y;
        int i =1;                //#c
        int power;            //change to int power = 1; as per #d of assignment
       
      
        
        scanf(" %d", &x);   //#a
        scanf(" %d", &y);   //#b
        
        i= power * x;       //put this inside while loop, before the ++i;
       
    
        while (i<=y)      //#f
        { i++;                //#g
    
           }
           
           return 0;               //move this line to the very end of the program
           
           ++i;                       //remove this line
           
           printf("int power is i");  //#h I'm not sure if this should go inside the while loop or not
                                                // I think it should go inside, however.      
           }
    is this right now

    Let's see where we are:

    a. input integer variable x with scanf
    b. input integer variable y with scanf
    c. initialise integer variable i to 1
    d. initialise integer variable power to 1
    e. while i <= y
    f. multiply power by x and assign the result to power
    g. increment i by 1
    h. output the integer variable power with printf.

    So, a few things to be fixed, yet.

    Edit:

    The console window your program is running in will close immediately after running, in many compilers. It's the default action of a
    console window in Windows.

    To keep the wiindow open long enough to see it properly, you might need to add this line of code:

    getchar();

    Note that each getchar() call will only remove *one* char from the keyboard buffer. If you have more than one, it will need some further help:
    Code:
    while((c = getchar()) != '\n');   //removes all chars in the keyboard buffer including the first newline char.
    getchar();                                //now getchar() by itself will wait for the next "enter" key before proceeding
    
    return 0;
    Last edited by Adak; 03-15-2009 at 09:00 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