--- In [email protected], "RICHARD" <[EMAIL PROTECTED]> wrote: > > I need to produce the following using the source code below: > > 1. Pascal Triangle of a certain degree n (x+y)^n > Should be limited to 11 rows, and shall return and ask > for another round? What shall I do? > 1 > 1 1 > 1 2 1 > 1 3 3 1 > 1 4 6 4 1 > 1 5 10 10 5 1 > > 2. Specific line of Pascal Triangle of a certain n (x+y)^n > Should show the specific line if for example I chose 3 > number of rows... the screen shall display > > 1 2 1 only > > The same for other numbers... what shall edit > with my source code? > > 3. Rth term coefficient of Pascal triangle at (ax+by)^n > ___________________________________________________ > #include<stdio.h> > > int main() > { > int row,c,n,x; > void pasc(int); > > printf("\n\nEnter the no. of rows: "); > scanf("%d",&row); > > printf("\n\n\n"); > printf("\nPascal's triangle :\n\n\n"); > > for(n=0;n<row;n++) > { > for(c=row-n;c>=0;c--) > printf(" "); > pasc(n); > printf("\n\n"); > } > } > > void pasc(int n) > { > int r; > long fact(int); > > for(r=0;r<=n;r++) > printf("%3ld ",fact(n)/(fact(n-r)*fact(r))); > } > > long fact(int v) > { > if(v==1||v==0) > return(1); > else > return(v*fact(v-1)); > }
Hi Chad, I have to tell you that you have _not at all_ chosen an easy subject to start with. You run into a good lot of issues which can keep beginning programmers heavily frustrated for a long time, if not strongly enough to keep you from programming at all. But let's look at these things one after the other. First you have declared all your variables as "int". While this suffices on machines which use 32-bit integers, it is not really a good idea. You should have chosen "long" for everything. And even this will not ensure that your machine uses 32-bit (or even 64-bit) integers. But that's a different story. Second you have not written any comment into your code. This is bad style. While I myself don't use many comments in programs of this length if I write them for myself, it is generally a bad idea to keep your code free of comments. Just consider you won't touch this code for half a year. Do you really think after half a year you will immediately understand what you've done here? Trust me, if I wouldn't comment my code really extensively (15-30% of my shell scripts and C codes are comments), I surely would not be able to maintain that stuff after one or two years. And indeed one work of mine from February 2002 still keeps me busy every now and then; without proper commenting I would not be able to understand what I've done here and there. Third you have chosen a somewhat weird style of structuring your code. You should try to write your code such that the simple and/or small auxiliary functions are not only declared but also defined outside any function, in particular before main(). What I mean is that in my personal opinion you should structure your code like this: long factorial( int n) ... void Print_Pascal_Triangle_Line( int n) ... int main(...) ... Fourth (as you can see by these few lines above) you should immediately start to choose meaningful names for each and every variable, constant, function, and so on. Modern editors will help you in using longer variable names without the need to type them every time you need them; for example, you can define keyboard macros in VIM, vi, Emacs, Notepad++, TextPad, and so on, such that you can get this name "Print_Pascal_Triangle_Line" by just hitting Ctrl-Q (as an example). This saves you much typing and many typos. Fifth you encountered a nasty challenge that you will often encounter in your programmer's life: you have written some code which works well, but then you want to add some functionality (such as printing solely line n instead of all lines of the triangle), and suddenly you're in a mess. Where to start with? How to start? Here is an advice which might look like complete overkill here but will help you in similar situations always and ever. This advice comes from my personal experience, it has no "academic" (theory-bound instead of practical) background: Structure your code such that functionality and user interface are completely distinct tasks. In your example, this means: Re-structure and write your code such that every piece of code _either_ has something to do with functionality (such as calculating some value or setting up all values of line #n of the triangle) _or_ deals with the user interface (abbr. UI). What does this mean? Look at your code; here are just rough summaries: 1) the "main" function asks for the number of rows to print (UI) and then executes a loop (functionality); within this loop it calls the function which prints every line of the triangle (functionality, namely retrieving the contents of line #n, plus UI). 2) the "pasc" function prints out line #n of the triangle (UI). 3) the "fact" function calculates the factorial of its parameter (functionality). This means that "pasc" is a UI function; "fact" is a functionality function. "main" contains aspects of both, and this makes it harder to maintain than necessary. What I propose to you is the following structure: At the beginning of the "main" function, ask the user whether to generate a complete Pascal triangle or whether to print out one single line of it. According to this choice, have the user input either the number of rows of the triangle or the number of the row to print; then act accordingly: for a complete triangle, execute a loop which prints out every line of the triangle; for one line, print out only this line. Now make sure that the above steps are executed in a loop which the user may leave at any time; this can for example be done by extending the choice which the user has to make anyway, for example by offering the user to choose between a complete triangle, one line of the triangle, or quitting the program. I know, this is only a very rough sketch. I hope you understand what I'm writing about, and I hope even more that you will post here all the questions you will encounter; I hope this because if you do ask questions you will learn a whole lot more from this small program than you can even imagine! Please, don't hesitate to ask everything you want to know, we will answer as thoroughly and honestly as we can. [Yes, I know, once more I sound like a know-it-all; it's all my personal experience from many years of working with computers.] Regards, Nico
