[C++] Pascal Triangle - Muhammad Faruq Nuruddinsyah
[C++] Pascal Triangle

Here's constructing a Pascal Triangle in C++

Sample input:
5

Output:
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1


Source Code:
/* 
 * File:   pascal-triangle.cpp
 * Author: Muhammad Faruq Nuruddinsyah
 *
 * Created on May 3, 2013, 7:29 AM
 */
 
#include <iostream>
 
using namespace std;
#define N 5
 
int main(){
    // Create the triangle
 
    const int row = N;
    const int col = N * 2 - 1;
    int triangle[row][col];
 
    for(int i = 0; i < row; i++)
        for(int j = 0; j < col; j++)
            triangle[i][j] = 0;
 
    triangle[0][N - 1] = 1;
    triangle[row - 1][0] = 1;
    triangle[row - 1][col - 1] = 1;
 
    for(int i = 1; i < row; i++){
        for(int j = 1; j < col - 1; j++){
            triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j + 1];
        }
    }
 
 
    // Print out the result
 
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            if(triangle[i][j] != 0){
                cout << triangle[i][j] << " ";
            }else{
                cout << "  ";
            }
        }
 
        cout << endl;
    }
 
    return 0;
}

Informasi:
Lihat semua daftar ACS - Download: pascal-triangle.cpp - Tanggal: 3 Mei 2013 - Kategori: C++