Help with a Triangle Pattern in C language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • davemc617
    New Member
    • Oct 2011
    • 3

    Help with a Triangle Pattern in C language

    Hi there; i'm a newbie when it comes to everything coding, and I have an assignment due tomorrow. The assignment is to code a triangle that begins with an '&' in the leftmost position of EVERY line, and in the case of line 1 the remaining 49 spots of the string will remain black. In the case of the remaining lines (2-32), I must print a '&' if the position above it on the previous line and the position above it and to the left of the previous line are different, otherwise it is again black. Oh, and I'm using Microsoft Visual to compile my code.

    My thought process beginning was to create a string: line[50]="&" and move from there. However, everything I try to get this to work comes out all screwy and resembles nothing close to what is required. Here's an example of what i've tried:

    Code:
    			/* TRIANGLE PATTERN*/
    
    
    #include <stdio.h>
    #include <string.h>
    #define SIZEOF 49
    
    int main(void)
    {
    	/* DECLARATIONS */
    	char space = ' ';
    	char line[50] = "&";
    	char add[2] = "&";
    	char adds[2] = " ";
    	int counter;
    	int loopCount = 0;
    
    
    	/* DISPLAY FIRST LINE */
    	printf("%s", line);
    
    
    	/* DESIGN & PRINT REST OF THE LINES */
    	
    	while(loopCount < 35)
    	{
    		for(counter = 1; counter < SIZEOF; counter ++)
    		{
    			if(line[counter] != line[counter - 1])
    			{
    				strcat(line, add);
    			}
    		}
    		loopCount = loopCount + 1;
    		printf("\n%s", line);
    	}
    	
    	
    	
    	
    
     	return 0;
    }
    Any help or advice will be greatly appreciated. Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This is an excellent time to earn how t use your Visual Studio debugger so you can step through the code.

    I did not do a complete review of your code but I notice you are using strcat. Are you aware that strcat appends by looking for a null terminator and then appeending by overlaying that terminator with the appended data which itself must have a null terminator?

    I don't see where you initialze your line on each cycle of the loop.

    And speaking of that your line array is just an array of 50 characters and not a array of 50 characters arrays.

    Comment

    • davemc617
      New Member
      • Oct 2011
      • 3

      #3
      Well, i'm not entirely sure what you mean by null termination. The way I went about strcat was with the if statement: if(line[counter] != line[counter - 1]). So in this case, if line[1] doesn't equal line[0] i'm trying to replace the blank spot in line[1] (the next line below) with an '&' by concatenating 'char add = "&"' into line [1]. But it appears that it is concatenating it into the rest of the entire string.

      I know that's kind of wordy and it may be hard to understand, if so i'm sorry. Is there a proper way to go about using strcat that i'm just not getting? I'm going to go try and work it out again in the mean time.

      Thanks.

      Comment

      • davemc617
        New Member
        • Oct 2011
        • 3

        #4
        Ok. So I scrapped the concatenation, and now i'm trying to reallocate the strings. I may not be using the right term so here's what it looks like:
        Code:
        while(loopCount < 35)
        	{
        		for(counter = 1; counter < SIZEOF; counter ++)
        		{
        			if(line[counter] != line[counter - 1])
        			{
        				temp=line[counter];
        				line[counter]=add[0];
        			}
        		}
        		loopCount = loopCount + 1;
        		printf("\n%s", line);
        	}
        But this is having the same outcome as my concatenation attempts. It looks a little something like this:
        &
        &&&&&&&&&&&&&&& &&&&&&
        &&&&&&&&&&&&&&& &&&&&&
        &&&&&&&&&&&&&&& &&&&&& etc. It just keeps going on like that.

        So I think my problem may lie with my declaration of the original string. Maybe I don't need to declare it as line[50] right away or something? I'll try to work it out.

        Any more help would be greatly appreciated.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          "I must print a '&' if the position above it on the previous line and the position above it and to the left of the previous line are different, otherwise it is again black."
          Does the output of the next line depend on only the previous line or the previous two lines? If the latter, then what is the rule for constructing the second line since there is only one previous line?

          This would be easiest if you had one array in which to construct the current line and one (or two) other arrays to hold the previous line(s) that control the construction of the current line.

          Once you print out a line, its array becomes that of the previous line. You could handle this role change by copying one array into the other (that is, shifting the lines); but that is not elegant or efficient. A faster way to change roles is to have a pointer for each role and shift the pointer values.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            It might help if you showed us the expected output for the first 4 lines.

            Comment

            Working...