Sign in to follow this  

C# Question

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im wanting to print Pascal’s Triangle from user input. So if I ask the user for a positive number and he types 4 I want to print. 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 and if 2 was typed then only print the first 3 rows. I would like any help or suggestions please. Thanks, Andy

Share this post


Link to post
Share on other sites
This is not a homework question. Its a challenge in my book im learning. I just need some starting tips. And yes I know loops and nested loops but its got something to do with arrays cause that was the chapter I just read.

Thanks,
Andy

Share this post


Link to post
Share on other sites
Hint: Use the variable that's being incremented by a for loop to read from one array and write to the next.

You also have to be able to make arrays based on which row # you're on.

To make an array however large you need it to be:


int[] rowArray;


// later... maybe in a loop...
rowArray = new int[numberOfSlots];

Share this post


Link to post
Share on other sites
In this code im messing with Arrays but how do I print the Arrays?


using System;

public class proj3
{
public static void Main()
{
Console.Write("Enter a positive integer: ");
int input = Convert.ToInt32(Console.ReadLine());
int[,]rowArray = new int[input,input];

for (int i = 0; i < input; i++)
{
for (int j = 0; j < 10; j++)
{
rowArray[i, j] = i * j;
}
}
}
}

Share this post


Link to post
Share on other sites
"ToString" works on *everything*.

So something like this:


Console.Write(array[index].ToString());


Use "Console.Write" instead of WriteLine if you want to be able to print more stuff on the same line.


EDIT: (oh wait, you already know what Write does...)

Share this post


Link to post
Share on other sites

0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1

There's obviously a pattern here.
- Every line i has i+1 numbers in it (line 0 has 1 number, line 1 has 2 numbers, line 2 has 3 numbers etc.)
- You can think of the nonexisting line -1 to have 0 numbers in it - it's a good starting point for the algorithm.
- line i can be built from line i-1 by creating a vector of size i+1 where the leftmost and rightmost number is 1, and the rest of the numbers is calculated as sums from the previous line

Here's some pseudocode to get you started:

create an empty vector of integers called "in"
loop i from 0 to n
create a new vector called "out" of length i+1
set the leftmost number of out to 1
set the rightmost number of out to 1
loop k from 1 to i-1
out[k] := in[k-1] + in[k]
write i followed by n-i+1 spaces to the console
write the contents of out to the console, seperated by spaces
write a new line to the console
this iteration's "out" must be next iteration's "in"


I implemented this in C++, and this is how the output looks with input 9:

0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1

The formatting obviously still needs improvement.

[Edited by - DevFred on June 24, 2008 2:25:20 PM]

Share this post


Link to post
Share on other sites
ok I used the ToString() command but Im not getting what I want. I want it to print something that looks like a grid.


using System;

public class proj3
{
public static void Main()
{
Console.Write("Enter a positive integer: ");
int input = Convert.ToInt32(Console.ReadLine());
int[,]rowArray = new int[input,input];

for (int i = 0; i < input; i++)
{
for (int j = 0; j < input; j++)
{
rowArray[i, j] = i * j;
Console.Write(rowArray[i, j].ToString());
}
}
}
}



I enter 3 for the user and it print 000012024

Share this post


Link to post
Share on other sites
I change it to look like the code at the bottom and I get a grid look but its messed up. It prints it like this.

0
1230
2460
369




The 0's should all move to the next spot down looking like this

0123
0246
0369




using System;

public class proj3
{
public static void Main()
{
Console.Write("Enter a positive integer: ");
int input = Convert.ToInt32(Console.ReadLine());
int[,]rowArray = new int[input,input];

for (int i = 0; i < input; i++)
{
for (int j = 0; j < input; j++)
{
rowArray[i, j] = i * j;
Console.Write(rowArray[i, j].ToString());
if (j % input == 0)
{
Console.WriteLine();
}
}
}
}
}


Share this post


Link to post
Share on other sites
Put the writeline below the inner loop, but still inside the outer loop. Remove the (j % input) == 0 since it's the same as "j == 0" in this case (since j never becomes equal to or greater than input). You don't want a newline AFTER you just printed a zero, but you don't want an extra newline before the first row, either.

Share this post


Link to post
Share on other sites

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this