Using the while loop to execute sequences

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2011
jamesallen4u
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.
• 10-01-2011
quzah
^ is XOR, not the power operator.

Quzah.
• 10-01-2011
megafiddle
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.
• 10-01-2011
CommonTater
Quote:

Originally Posted by megafiddle
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
• 10-02-2011
jamesallen4u
My teacher told me to only use while loops, I can't use long. Is there any other way?
• 10-02-2011
jamesallen4u
Quote:

^ is XOR, not the power operator.
Thanks for that explanation. That explains my weird output.
• 10-02-2011
laserlight
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...)
• 10-02-2011
CommonTater
Quote:

Originally Posted by jamesallen4u
My teacher told me to only use while loops, I can't use long. Is there any other way?

• 10-02-2011
TheBigH
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.
• 10-02-2011
jamesallen4u
Quote:

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.

Attachment 10959
• 10-02-2011
jamesallen4u
Quote:

I'm not sure what you mean by strings?
• 10-02-2011
laserlight
I am thinking more of printing character by character, actually.
• 10-02-2011
jamesallen4u
My teacher is trying to get us to use printf less often. It would create more lines of code than while.
• 10-02-2011
laserlight
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.
• 10-03-2011
vikasvijayan
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)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last