Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
FloydTriangle
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 Floyd's triangle in the square matrix passed to it //credit for Floyd's triangle code goes to codescracker.com; Floyd function and test driver codes are created by Rezaul Hoque on July 04,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; void floyd(int [][5], int); void floyd(int b[5][5], int n){ int num=1; //when i=0 and j= 0 , first row contains all zero because it doesn't fulfill the j<i condition; when i=1 and j=0, a[1][0] becomes 1(initial value of num) and rest of the elements of second row are zero because we have to respect j<i condition; when i=2 and j=1, num is incremented to 2 and a[2][0] contains this value, num is incremented to 3 and a[2][1] contains it and the inside loop continuing condition (j<i) allows us this far, so rest of the elements of third row are filled with zero; the process is repeated for the rest of the rows for(int i=0;i<n;i++){ for(int j=0;j<i;j++){ b[i][j]=num; num++; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) cout<<" "<<b[i][j]; cout<<endl; } } 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}}; floyd(b,n); return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
edit mode
|
history
|
discussion