Like Tree1Likes

Using the while loop to execute sequences

This is a discussion on Using the while loop to execute sequences within the C Programming forums, part of the General Programming Boards category; Hello, I had more of a conceptual question regarding a program I am trying to execute. The desired output I ...

  1. #1
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    Using the while loop to execute sequences

    Hello,

    I had more of a conceptual question regarding a program I am trying to execute. The desired output I am trying to achieve is:

    1,11,111,1111,11111,111111,1111111


    Here was what I tried:

    Code:
    #include <stdio.h>
    int main()
    {
        int k;
        
        k = 0;
    
        while ( k < 1111111 )
        {
            printf("The value of k = %3d\n\n", (10^k-1/9) );
            k = k +1;
        }
    }
    It seemed relatively logical since the way to obtain the sequence is by using 10^k-1/9 in math. But being new to C, I don't have much experience so this has baffled me. Any tips, hints, suggestions, or solutions to this problem would be greatly appreciated. Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ^ is XOR, not the power operator.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    327
    Assuming the output are numerical values, 1111111 is too large for an int.
    Limit for int is 32,767.

    With a large enough variable, like a long int, a simpler solution might be:
    Code:
    #include <stdio.h>
    int main()
    {
        long k;
        
        k = 1;
    
        while ( k <= 1111111 )
        {
            printf("The value of k = %d\n\n", k );
            k = k * 10 + 1;
        }
    }
    Unless the output was specified as numerical, you could solve it with strings also.
    Last edited by megafiddle; 10-01-2011 at 07:47 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    Assuming the output are numerical values, 1111111 is too large for an int.
    Limit for int is 32,767.
    Only on that grugy old TurboC ... most modern compilers have 32 bit ints which is -2,147,483,648 to +2,147,483,647

    Really if there was no other reason to update -- like Turbo C code not working on 64 bit systems, it being very non-standard, etc.-- this should convince you to make the leap.

    Of course there's also the 64 bit long long int ... -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

  5. #5
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    My teacher told me to only use while loops, I can't use long. Is there any other way?

  6. #6
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    ^ is XOR, not the power operator.
    Thanks for that explanation. That explains my weird output.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by jamesallen4u
    My teacher told me to only use while loops, I can't use long. Is there any other way?
    Allow me to change your assignment, temporarily. Your assignment is: given an input of integer k, produce the output consisting of 'z' repeated once, twice, ..., k times, with the help of while loops. For example, for k=7:
    Code:
    z,zz,zzz,zzzz,zzzzz,zzzzzz,zzzzzzz
    (Yes, it is time for me to zzz...)
    jamesallen4u likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jamesallen4u View Post
    My teacher told me to only use while loops, I can't use long. Is there any other way?
    Sure... take Lases advice and build your output as a string...

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    This thing
    Code:
    (10^k-1/9)
    has at least two other problems than the XOR operator. The first is that, if the ^ did what you originally expected, it would be equivalent to (10^k)-(1/9). The second is that (1/9) is integer division, which gives you a whole number and discards the remainder so that (1/9) = 0.
    Code:
    while(!asleep) {
       sheep++;
    }

  10. #10
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43

    Assuming the output are numerical values, 1111111 is too large for an int.
    Limit for int is 32,767.
    I must say, you were right with that statement. My Microsoft compiler sputtered exactly at 11111. When I compiled on an online IDE, it worked just fine so I guess my compiler is just out of date. Please see the screenshot of my output below. Thanks.

    Name:  output 1.png
Views: 377
Size:  11.2 KB

  11. #11
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    Sure... take Lases advice and build your output as a string...
    I'm not sure what you mean by strings?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    I am thinking more of printing character by character, actually.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Do you C what I C? jamesallen4u's Avatar
    Join Date
    Oct 2011
    Posts
    43
    My teacher is trying to get us to use printf less often. It would create more lines of code than while.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by jamesallen4u
    My teacher is trying to get us to use printf less often. It would create more lines of code than while.
    The use of printf and the use of a while loop are two separate issues. Your teacher is probably just trying to get you to avoid stuff like:
    Code:
    printf("z");
    printf("z");
    printf("z");
    printf("z");
    printf("z");
    printf("z");
    printf("z");
    when you can use a loop to abstract away the repetition, but that is the core of your assignment here so your objection is pointless.

    Incidentally, I would use putchar, not printf, but it amounts to the same output.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    i wil give you a simple solution for this problem
    Code:
     void main()
      {
        int i,k=1;
        while(k>0)
        {
          for(i=0;i<k;i++)
            printf("1");
          printf(","); 
          k++;
        }
     }
    this code will run infinitely....to see the output clearly,you can just make the while loop condition to a finite value...for eg: while(k<10)
    Last edited by vikasvijayan; 10-03-2011 at 04:37 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-18-2010, 01:34 PM
  2. Escape sequences in c
    By linuxlover in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 02:39 PM
  3. increasing sequences
    By jack_carver in forum C Programming
    Replies: 4
    Last Post: 03-09-2010, 10:31 PM
  4. Escape sequences in VC++
    By emilyh in forum Windows Programming
    Replies: 7
    Last Post: 09-26-2001, 07:02 AM
  5. Escape Sequences
    By Me-Again-Again in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 06:24 AM

Tags for this Thread


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