473,396 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.             /* TRIANGLE PATTERN*/
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #define SIZEOF 49
  7.  
  8. int main(void)
  9. {
  10.     /* DECLARATIONS */
  11.     char space = ' ';
  12.     char line[50] = "&";
  13.     char add[2] = "&";
  14.     char adds[2] = " ";
  15.     int counter;
  16.     int loopCount = 0;
  17.  
  18.  
  19.     /* DISPLAY FIRST LINE */
  20.     printf("%s", line);
  21.  
  22.  
  23.     /* DESIGN & PRINT REST OF THE LINES */
  24.  
  25.     while(loopCount < 35)
  26.     {
  27.         for(counter = 1; counter < SIZEOF; counter ++)
  28.         {
  29.             if(line[counter] != line[counter - 1])
  30.             {
  31.                 strcat(line, add);
  32.             }
  33.         }
  34.         loopCount = loopCount + 1;
  35.         printf("\n%s", line);
  36.     }
  37.  
  38.  
  39.  
  40.  
  41.  
  42.      return 0;
  43. }
Any help or advice will be greatly appreciated. Thanks.
Oct 16 '11 #1
5 2445
weaknessforcats
9,208 Expert Mod 8TB
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.
Oct 16 '11 #2
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.
Oct 16 '11 #3
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:
Expand|Select|Wrap|Line Numbers
  1. while(loopCount < 35)
  2.     {
  3.         for(counter = 1; counter < SIZEOF; counter ++)
  4.         {
  5.             if(line[counter] != line[counter - 1])
  6.             {
  7.                 temp=line[counter];
  8.                 line[counter]=add[0];
  9.             }
  10.         }
  11.         loopCount = loopCount + 1;
  12.         printf("\n%s", line);
  13.     }
  14.  
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.
Oct 16 '11 #4
donbock
2,426 Expert 2GB
"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.
Oct 17 '11 #5
donbock
2,426 Expert 2GB
It might help if you showed us the expected output for the first 4 lines.
Oct 17 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Rookie | last post by:
I have done a lot of programming some time ago using Fortran and various varieties of the Basic programming languages, Hewlett Packard Technical Basic, QBasic, Quick Basic. With each of these...
2
by: Tim Smith | last post by:
Dear All, Silly question, but I am having trouble understanding the diagram on the inside back cover entitled "Design Pattern Relationships." It shows the relationships between all of the...
2
by: sachin | last post by:
Hello Everybody I need some help regarding Natural Language Processing. I am designing a MT system from a SOV language to a SOV language. I need a parser which can find the root word...
4
by: cpptutor2000 | last post by:
Could some C guru please help me? Suppose I have a code snippet as: int* doit(){ int intArray; int *ip; ip = intArray; return ip; } Since the array intArray is allocated on the stack and so...
0
by: marcus | last post by:
I am developing a web site at work that needs to be both in French and English. I have been reading up on using resource files (.resx files) in combination with setting the CultureInfo to either...
9
by: vinod.bhavnani | last post by:
Hello all, I have 2 qs about statements and their meanings in the C language. first If i have an array named int a and if i use a staement like
5
by: singhm | last post by:
Hi guys so I have a trianlge program having hard time finishing this though, I have to develop a program which is the following: Write a program that will allow the user to enter the 3 lengths...
1
by: Dave128 | last post by:
I am trying to write a program for an assignment that uses a For loop to produce an output that looks like this: * ********** ********** * ** ...
7
by: harishram007 | last post by:
please can u help me out in the following patterns A ABA ABCBA ABCDCBA ABCDEDCBA ABCDEFEDCBA PATTERN 2: 4 4 4 4
4
by: triton96 | last post by:
I want to get in to programming but would like your input on a couple of things: I would like to write some basic applications that would for example create a payroll check or create a billing for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.