Thread: i am a bit lost with this one

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    how would I do that increment for i?
    i =??

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could use i = i + 1, but C programmers use the shortened ++i.

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

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    my compiler keeps telling me that int i= int power * x is wrong..

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    don't use the word "int", anymore. Compiler knows that power is an int variable, already.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    int main ()
    {
        int x;
        int y;
        int i =1;
       
        
        scanf(" %d", &x);
        scanf(" %d", &y);
        
        int i= int power * x;          //stop repeating the int word!!
        i = i +1;                     //goes inside your while loop, on last line, thereof
        
      while (1) {          //can't use this (1) - ditch it.
           
           scanf("%d", &y);
           
    
           if(i <=y)        //goes into your while(HERE) condition
           break;
           
           return 0;     //make this outside the while() loop - last line of code.
           ++i;       
      }
    A few corrections

  7. #22
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    hmm it is still not working.

    i've tried

    i = power * x;
    and int i = power * x;

    any suggestions?

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't declare int power, when it's on the right side of an equation. You can use power, but not before it's declared (created with the right data type).

    so

    int power;

    i = power * x;

  9. #24
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    so instead of a while (1), do i just leave the while there by it self or take it out also?

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Leave it, remove the "1" from inside the parenthesis. You need to add a condition for while, which your assignment specified.

    Remove the break statement, and the other stuff I mentioned, and I'll be back in 1.5 hours. Dinner time!

  11. #26
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    do i still need the if statement? i was thinking can't i just take the if statement and put it in the while statement?

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    g. Test i to see if it is less than or equal to y in the
    condition of a while statement.


    Since you only need to get the value of y once, you should do that before the while loop, not inside of it. Also (it is hard to judge by the specifications you gave, but...) I would assume that the purpose of the while loop is to trap the value of i until it is greater than y (so the condition is "while less than or equal to"):
    Code:
    while (i<=y) i++;
    By my reading, the "program" produced by this assignment does not really do anything coherent -- the purpose may be to just get you to write some functional statements, within the context of a single program.
    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

  13. #28
    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++;
    
           scanf("%d", &y);
           
           
           
    
      
           
           
           return 0;
           
           ++i;
           
           printf("int power is i");
           
           }
    would this be the final code?

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You only need to scanf for y the first time, right? There is no mention of repeatedly inputing y until it is less than i or something?

    Note that the while loop I just gave
    Code:
    while (i<=y) i++;
    does not have any brackets because there is only one statement in the loop. It means the same as this:
    Code:
    while (i<=y) {
           i++;
    }
    In case you misunderstood.

    A difficulty here is that because there is no real purpose to the program, it will be difficult for you to assess whether everything has been done "correctly".

    Are you translating the specifications from another language?
    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. #30
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    i do not believe so. it is just part of my assignments.

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