Thread: Increment/Decrement for loop?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question Increment/Decrement for loop?

    Code:
    /* What's wrong with the damn code?I just gives endless zeros,I want
    the output to be something like this - 
    			1
    		1	2	1
    	1	2	4	2	1	
    1	2	4	8	4	2	1
    
    Its driving me crazy!
    */
    
    #include<stdio.h>
    
    	void main()
    		{
    		int i,j;
    			for(i = 1;i<=8;i*=2)
    				{
    				for(j = 1;j<=i;j*=2)
    					{
    					printf("%d\t",j);
    						if(j == i&&j>1)
    							{
    							for(j = j/2;j>=1;j/=2)
    								{
    								printf("%d\t",j);
    								}
    							}
    					}
    				printf("\n");
    				}
    		}

  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    I found two problems, the infinite loop was caused by the inner most loop. It was using the same counter as the loop before it and one was multiplying while the other was dividing which kept the counter from ever reaching the condition.
    The second problem was with the tab spacing, I couldn't see a way to do is the way you were trying so I added another loop to handle the tabs in front of the numbers.
    By the way, don't use void main() it's nonstandard and has undefined behavior. Just because it works most of the time doesn't mean it will work all of the time
    Code:
    #include<stdio.h>  
    void main()
    	{
    		int i,j,k,tcnt,scnt=3;
    		for(i = 1;i<=8;i*=2)
    			{
    			tcnt=scnt;
    			while(tcnt > 0)
    				{
    					printf("\t");
    					tcnt--;
    				}
    			for(j = 1;j<=i;j*=2)
    				{
    				printf("%d\t",j);
    				if(j == i&&j>1)
    					{
    					for(k = j/2;k>=1;k/=2)
    						{
    							printf("%d\t",k);
    						}
    					}
    				}
    			printf("\n");
    			scnt--;
    			}
    	}
    pointer = NULL

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Wink

    I was not thinking about the tabs problem now,just wanted the loops to work right...thanks anyway.
    Merry Christmas to you...

    ~M~

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Red face

    Hey,just curious...what were you thinking....tcnt,scnt.....don't bite my head off...its 1:21AM...and I've nothing to do..sleeping is an option but.....

    ~M~ :-)
    tcnt,scnt

  5. #5
    Unregistered
    Guest
    tcnt and scnt have to do with the order and number of tabs. tcnt means tab count and is the variable actually used in the loop, scnt means static count and is used to set the decreasing amount of tabs starting at 3 and going down as the lines get longer.

    The infinite loop has been taken care of as well as the output been corrected, so it should work as needed.

    *pointer

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    I knew what tcnt and scnt were meant to do,just didn't know how you got those names

    ~M~

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Question

    When I run the code in M$VC++ the "1" in the first row is off by one tabspace,when I run the same in Turbo C++ its okay,would you mind checking if that happens with your computer as well?

    ~M~

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Sometimes different compilers will react differently to certain code. This usually isn't a problem if the code is in the ISO C standard and portable, which means not using any compiler specific functions and conforming to the standard (which void main does not).

    I ran the code through my compiler and gave it a more conforming look, but pointer's code worked originally in my MSVC++ 6. Who knows, my changes may help and may not. What version of MSVC++ do you use? The problem could be conflicting versions, if both versions are current then all will be well as I ran the code through the current Borland C++ also and it works fine.

    Concerning the code, be it yours or pointer's, I recommend judicious commenting as well as the use of #define to avoid using integer and character constants without describing what they are supposed to do. I'm an experienced programmer and this code looked like a jumble when I first read it. Also, find a spacing format and stick with it; consistency is very important to those who have the pleasure of reading your work.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    
    int main(void){
    	int i, j, k; /*counter variables*/
    	int currentTabs, maxTabs = 3;
    
    	for(i = 1; i <= 8; i *= 2){
    		currentTabs = maxTabs;
    		/*determine and print whitespace needed*/
    		while(currentTabs > 0){
    			printf("\t");
    			currentTabs--;
    		}
    		/*calculate and print numbers. ex:
    		**   1
    		**  121
    		** 12421
    		**1248421
    		**(spaces are tabs)
    		*/
    		for(j = 1; j <= i; j *= 2){
    			printf("%d\t", j);
    			if(j == i && j > 1){
    				for(k = j / 2; k >= 1; k /= 2)
    					printf("%d\t", k);
    			}
    		}
    		printf("\n");
    		maxTabs--;
    	}
    	return EXIT_SUCCESS;
    }
    Last edited by Prelude; 12-28-2001 at 09:36 AM.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    I use Turbo C++ 3.0 and M$ VC++ 6.0.I'm fairly new to proper programming[been much of a code-reader and understand things like that...]so it will take some time to get to the finer points,but thanks for pointing out...well,the off by one problem is still there..also,I tried some other code to do the same....here it is...

    Code:
    #include<stdio.h>
    
    	void main()
    		{
    		int i,j;
    			for(i = 1;i<=5;i++)
    				{
    				for(j = 1;j<=5-i;j++)//why do you need this loop?
    					{
    					for(j = 1;j<=i;j++)
    						{
    						printf("%d\t",j);
    						for(j = i - 1;j>=1;j--)//??
    							{
    							printf("%d\t\n",j);
    							}
    						}	
    					}
    				}
    		}
    I have a lot of trouble understanding what it really does..please do help me understand..

    ~M~

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Well, as it is you have an infinite loop because you use the variable j in three loops that depend on each other. Two increment and one decrements, so you'll most likely not get the output you wanted. My question is this, what exactly do you want this particular code to do? Because I got it to work and the output is as follows:
    Code:
    1	1
    1
    2	1
    1
    3	1
    1
    1	2
    2
    2	2
    2
    3	2
    2
    1	1
    1
    1
    2	1
    1
    1
    3	1
    1
    1
    4	1
    1
    1
    As you can see that's not very descritive of what the program does Give me an example of what you want it to do and I'll help you do it.

    As for the problem with the tabbing, it's difficult to help you when I can't reproduce the problem on my compiler. You said it works fine on Turbo C++ and has problems with MSVC++ 6 and it worked fine for me in MSVC++ 6. Try fiddling with the code and see if you can get rid of a tab in the beginning, something like this:
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void){
    	int i, j, k; /*counter variables*/
    	int currentTabs, maxTabs = 3;
    
    	printf("\t");      /*add this if there is one too FEW tabs on the first line*/
    	for(i = 1; i <= 8; i *= 2){
    		currentTabs = maxTabs;
    		/*determine and print whitespace needed*/
    		while(currentTabs > 0){
    			printf("\t");
    			currentTabs--;
    		}
    		if(maxTabs == 3) /*add this and the next line if there is */
    			printf("\t");  /*too MANY tabs on the first line*/
    		/*calculate and print numbers. ex:
    		**   1
    		**  121
    		** 12421
    		**1248421
    		**(spaces are tabs)
    		*/
    		for(j = 1; j <= i; j *= 2){
    			printf("%d\t", j);
    			if(j == i && j > 1){
    				for(k = j / 2; k >= 1; k /= 2)
    					printf("%d\t", k);
    			}
    		}
    		printf("\n");
    		maxTabs--;
    	}
    	return EXIT_SUCCESS;
    }
    I commented the areas you should and and if you should add them. Don't put them both or you'll have problems again. Let me know how that works.

    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    8

    Unhappy

    Oh well,I forgot to tell you how this code works on Turbo C++ 3.0.For the first run,it has the off by one error,from the second run onwards,its okay,now,after adding the extra tab,it runs perfectly on VC++ and also on Turbo C++,but only during the first run,from the second run onwards,its has an extra tab...
    About the code I posted prior to your post,it does exactly the same..print the pyramid,however it requires minor changes...
    for(j = 1;j<=5-i;j++) ==> I can't figure out why this is required at all,the loop before this for(i = 1;i<=5;i++) runs till _i = 5_ and the next loop for(j = 1;j<=i;j++) again depends on the first loop...my problem there is no condition check...how does it work...I'm working on it but just can't figure out how to make the damn thing work...
    In the code posted along with my first post[BEFORE POINTER CORRECTED IT],a series of 0s are shown as the output instead of 1s,if you comment out the if case,the value of i is 2 and upon division[after the condition check..leading to the forever loop]should be 1 and so should the output....

    Appreciate the help,
    Mandar

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    8
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    	void main()
    		{
    		int i,j,k,tcnt,scnt=3;
    		clrscr();
    			for(i = 1;i<=8;i*=2)
    				{
    				tcnt=scnt;			    
    			while(tcnt > 0)
    				{
    					printf("\t");
    					tcnt--;
    				}
    			//for(j = 1;j<=5-i;j++)
    					//{
    					for(j = 1;j<=i;j*=2)
    						{
    						printf("%d\t",j);
    						for(k = j/2;k>=1;k/=2)
    							{
    							printf("%d\t",j);
    							}
    					//}	
    					}
    					printf("\n");
    					scnt--;
    					getch();
    				}
    		}
    Try this code on TC++,it will gove you some idea as to what exactly happens..only on TC++ you can you getch()..

    ~M~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interlocked increment/decrement
    By Elysia in forum C++ Programming
    Replies: 17
    Last Post: 04-13-2009, 01:46 PM
  2. Increment/Decrement operator troubles
    By LineOFire in forum C Programming
    Replies: 6
    Last Post: 11-15-2006, 10:28 PM
  3. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM