Hi, my name is Stephan. I've been working on this program off and on over the past few days. I have yet to perfect it. I would really appreciate any help! For some reason the output begins to deteriorate at around row 13...I can't figure out why.
Thank you in advance,
Stephan - import java.io.*;
-
import java.util.*;
-
import gpdraw.*;
-
import java.lang.*;
-
public class pascal{
-
int[] array= new int[1000];
-
public void generate(int input){
-
int i=0;
-
for(int n=0;n<=input;n++){
-
for(int r=0 ; r<=n;r++){
-
array[i] = fact(n)/(fact(n-r)*fact(r));
-
i++;
-
}}
-
int d=0;
-
for (int row = 0; row <=input; row++){
-
for(int pos=0; pos<=row; pos++){
-
System.out.print(" " + array[d]);
-
d++;
-
}
-
System.out.println();
-
-
}}
-
public static int fact(int x){
-
if( x <= 1 ) // base case
-
return 1;
-
else
-
return x * fact( x - 1 );
-
}
-
}
DRIVER - import java.util.Scanner;
-
public class pascaldriver{
-
public static void main(String[] args){
-
int input;
-
Scanner nub= new Scanner(System.in);
-
System.out.print("Enter the row you want the pascal triangle to go to: ");
-
input = nub.nextInt();
-
-
pascal tri = new pascal();
-
tri.generate(input);}
OUTPUT - Enter the row you want the pascal triangle to go to: 13
-
1
-
1 1
-
1 2 1
-
1 3 3 1
-
1 4 6 4 1
-
1 5 10 10 5 1
-
1 6 15 20 15 6 1
-
1 7 21 35 35 21 7 1
-
1 8 28 56 70 56 28 8 1
-
1 9 36 84 126 126 84 36 9 1
-
1 10 45 120 210 252 210 120 45 10 1
-
1 11 55 165 330 462 462 330 165 55 11 1
-
1 12 66 220 495 792 924 792 495 220 66 12 1
-
1 4 24 88 221 399 532 532 399 221 88 24 4 1
Starts at row 0 as well as position 0 on each row.
The more rows I enter the more senseless the output becomes. - 1
-
1 1
-
1 2 1
-
1 3 3 1
-
1 4 6 4 1
-
1 5 10 10 5 1
-
1 6 15 20 15 6 1
-
1 7 21 35 35 21 7 1
-
1 8 28 56 70 56 28 8 1
-
1 9 36 84 126 126 84 36 9 1
-
1 10 45 120 210 252 210 120 45 10 1
-
1 11 55 165 330 462 462 330 165 55 11 1
-
1 12 66 220 495 792 924 792 495 220 66 12 1
-
1 4 24 88 221 399 532 532 399 221 88 24 4 1
-
1 0 1 5 14 29 44 50 44 29 14 5 1 0 1
-
1 1 -4 -1 2 4 7 9 9 7 4 2 -1 -4 1 1
-
1 0 -1 -1 -1 4 -1 1 1 1 -1 4 -1 -1 -1 0 1
-
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1
I wouldn't post unless I had spent a few hours on this...
I WOULD REALLY APPRECIATE ANY HELP AT ALL!
3 2334
It probably has to do with the factorial being used. 13! is likely too big to hold in an int. This isn't your fault, so don't blame your code or yourself - that's just how Java was made.
If you have a desperate need to go to 13 rows and beyond, you need to look into alternate data types to use.
Yep, 13! is 6,227,020,800, which is too big for the 4billion or so a 32bit integer (which is all Java has) can handle. Use a long instead, that's 64 bits and will thus get you to 2^63-1, which is 9.22*10^18.
I'd say don't use factorials at all. A number at line i, column j is the sum of the
two numbers diagonally above it, i.e (i-1, j-1) and (i-1, j). If you calculate those
numbers from right to left you can use the same array over and over again.
Here's a spoiler so no comments nor an explanation: -
public class Pascal {
-
-
public static void main(String[] args) {
-
-
int[] n= new int[20];
-
-
for (int i= 0; i < n.length; i++) {
-
n[i]= 1;
-
for (int j= i; --j > 0; n[j]+= n[j-1]);
-
for(int j= 0; j <= i; j++)
-
System.out.print(n[j]+" ");
-
System.out.println();
-
}
-
}
-
}
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: abcd |
last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual
vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a
ljetvedehg, hard slog," mariyu vede legai pressed...
|
by: Christian Christmann |
last post by:
Hi,
in the past I always appreciated your help and hope that you also can help
me this time. I've spent many many hours but still can't solve the
problem by myself and you are my last hope.
...
|
by: Novice Computer User |
last post by:
Hi. Can somebody PLEASE help. I have spent hours on this.. but I am a
total novice and can't seem to figure it out. Here is a .php script.
Right now, the
minimum amount of time (i.e. duration)...
|
by: GUPTAJI |
last post by:
hi all,
can u give me the code to create a Pascal's Triangle........... and
yes, it should work
thankx
|
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...
|
by: asif929 |
last post by:
I have another program to write, i will appreciate if somebody can
help......prompts the user to enter positive integer, and then prints
out four triangles
For Example: If we enter 4 it should...
|
by: Aaron |
last post by:
Hi,
I have written a pascals triangle program. However, my program gets a
floating point exception somewhere between 60 and 70 and any subsequent
larger value. This is no doubt due to dividing...
|
by: Jim Madsen |
last post by:
Hi all--
Two years ago I bought a copy of vbdotnet2003 and the book "Learn MS
VBDotNet 2003 in 21 Days", published by Sams. I started working through
the book, but lost interest. This...
|
by: AlmasKhan01 |
last post by:
Hi I have been for sometime now been working on a MineSweeper program and I'm 90% complete on it it involves a window interface and is quite simple but the issue is not. The program is long(to me)...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |