Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PascalTriangle
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 7.4.0 //write and test a function that creates Pascal's Triangle in the square matrix passed to it; //credit for factorial function and combination function codes goes to John R. Hubbard, University of Richmond //Pascal Triangle function and test driver codes are created by Rezaul Hoque on June 30,2021; contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> using namespace std; int fact(int ); int comb(int, int); void pascal(int [][5], int); void pascal(int b[5][5], int n){ for(int i=0;i<n;i++){ for(int j=0;j<i+1;j++) b[i][j]=comb(i,j); } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cout<<" "<<b[i][j]; cout<<endl; } } int fact(int n){ if (n<0) return 0; int f= 1; while(n>1) f *= n--; return f; } int comb(int n, int k){ return(fact(n)/(fact(k)*fact(n-k))); } int main(){ int n=5; int b[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}; pascal(b,n); return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.54 sec, absolute running time: 0.96 sec, cpu time: 0.02 sec, memory peak: 5 Mb, absolute service time: 1,59 sec
edit mode
|
history
|
discussion
1 0 0 0 0 1 1 0 0 0 1 2 1 0 0 1 3 3 1 0 1 4 6 4 1