Hello,

I have writted a prog that generates the values & triangleOf Pascale.


But the thing i cannot seem to find, is how to get rid of the Console.Writlines in the middle of my code (where the values get calculated).
I am using a 1 dimensional array for this insteed (i am aware a 2 dim/jagged array would make more sence for this, but thats not the point).


I have putted the Display And Calculate Method in a for that runs trough the Array but i cannot seem to get further then that.
The Console Write line code should be in the DisplayArray1Dim method.

Regards.

using System;

namespace Triangle
{
    class ConsCode
    {
        public void ExecuteProgram()
        {
             Console.Title = "Triangle";

            const int AantalRijen = 10;
            int[] Rij = new int[AantalRijen];

            for (int i = 0; i < Rij.Length; i++) 
            { 
                BerekenArray1Dim(Rij, i);
                DisplayArray1Dim(Rij, i);
            }

        }/*ExecuteProgram*/

        private void BerekenArray1Dim(int[] Rij, int i) 
        {
            for (i = 0; i < Rij.Length; i++) 
            {
                int F = 1;
                int teller = i;
                string formatting = "  ";

                for (int g = Rij.Length; g > i; g--)
                {
                    Console.Write(formatting);
                }/*formatting triangle*/

                Console.Write("1"); // --> should not be here
                for (int j = 1; teller >= 1; j++)
                {
                    int noemer = j;
                    F = F * teller / noemer; //denominator en nominator
                    teller--;
                    noemer++;
                    Console.Write("{0,4}", F);  // --> should not be here
                }
                Console.WriteLine(); // --> should not be here
            }
            Console.WriteLine("\n\n");  // --> should not be here

        }/*BerekenArray1Dim*/

        private void DisplayArray1Dim(int[] Rij, int i)
        {
            //Console WriteLines should be here in this method
        }/*DisplayArray1Dim*/

    }/*ConsCode*/
}

> Console.Write("1"); // --> should not be here
Into

// Console.Write("1"); // --> isn't here any more!
commented: Eaten something and it is not becoming you? -1

For a starter, leave out the for loop on line 14.
Change the i in lines 16 and 17 into AantalRijen.
This should perhaps already help you out a bit.
Keep you informed but have not much time right now. Succes gewenst!

The problem with your code is you are trying to do too many things in the same time.
If you have a problem, try to split it into small manageable chunks.
And, forget my previous post and look at this:
Here I made a class who gave me a list of the numbers of a line of the pascal triangle. Now do with this list what you like.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PascalTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            PascalTriangle P = new PascalTriangle();
            List<int> L;
            // I was a bit premature, make your for loop here again
            // example of getting line nr 5, line nrs start at 0
            L = P.GetPascalTriangleLine(5);
            //Write the line in any format you want here
            for (int i = 0; i < L.Count; i++)
            {
                Console.WriteLine(L[i]);
            }
            Console.ReadKey();
        }
    }

    class PascalTriangle
    {
        public PascalTriangle(){}

        public List<int> GetPascalTriangleLine(int LineNbr)
        {
            List<int> PL = new List<int>();
            int F = 1;
            int teller = LineNbr;
            PL.Add(F); //Add first element to the list
            for (int j = 1; teller >= 1; j++)
            {
                int noemer = j;
                F = F * teller / noemer; //denominator en nominator
                teller--;
                noemer++;
                PL.Add(F); //Add F to list
                //Console.Write("{0,4}", F);  // --> should not be here
            }
            return PL;
        }
    }
}
commented: Well written interesting posts like usual! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.