![]() |
|
#1
|
||||
|
||||
|
now I already did all the logic code I just need to create a function to make it go into to the shape of a triangle instead of: 1 11 111 ect... I have no idea how you would even go about doing this... |
|
#2
|
||||
|
||||
|
Just stick in spaces, if this is for the command prompt.
Ie: Code:
1 1 1 1 1 1 1 1 1 1 |
|
#3
|
||||
|
||||
|
Wouldnt it just be simpler todo in Pascal? LMAO sorry had to!
__________________
SOFTWARE: Rain's Simple MMS Maker | Rain's UltraLite MMS Makers TUTORIALS: PlayStation One on PSP Guide | UMD/ISO/CSO Guide | Recovery Flasher | FATMSMOD 3.71 on 4.01 | 4.05 Visualizer on 4.01
|
|
#4
|
||||
|
||||
|
Quote:
somehow I got a slanted triangle playing with setw I guess its better then nothing |
|
#5
|
|||
|
|||
|
I just spend a while on building a similar application in C (I'm learning C before C++)
And got this output Code:
0-1e-c2-a4-a5-1b:~ MacBook$ ./pascal
How Many rows do you want?
Input: 8
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
Adding an extra newline makes it a bit easier to see the characters, but the shape gets even more messed up. Code:
0-1e-c2-a4-a5-1b:~ MacBook$ ./pascal
How Many rows do you want?
Input: 8
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
First you would need to calculate the largest number for example, if you have 16 rows this is: 12870 ; if you only have 12 it's 924. then the amount of spaces is largest int + 1. So with 16 rows, the amount of spaces is 5+1 =6. Then, when printing / couting the numbers, you need to check the size of an int and do spaces - int size; so with the int 35: 6 - 2 = 4 And then print that amount of spaces. It prob takes a while to get it all figured out and working properly so I would only add it if you really, really need it. Anyways this is the code I wrote (yea it's prob fucked up and stuff but this is only my 1st C app (well 2nd because I once helped someone with his C app) Code:
/*
* Pascal's Triangle
*
*
* Created by Jasper on 21-09-08.
* Copyright 2008 Jasper van der Stoop. All rights reserved.
*
*/
#include <stdio.h>
#include <stdlib.h>
int getfactorial(int top) {
unsigned i;
int factorial = 1;
for (i = 2; i <= top; i++){
factorial = factorial * i;
}
return factorial;
}
int main(int argc, char *argv[])
{
// Initialize variables
char rows, row;
int i, r, v, height, spaces, fact, factorial, factt1, factb1, factb2;
// Get the Triangle Height
printf("How Many rows do you want?\nInput: ");
rows = getchar();
height = atoi(&rows);
for (r = 0; r < height; r++){
// Put the Spaces in
spaces = height - r;
for (i = 0; i <= spaces; i++){
printf(" ");
}
for (v = 0; v <= r; v++){
int b = r-v-1+1;
int c = v;
factt1 = getfactorial(r);
factb1 = getfactorial(b);
factb2 = getfactorial(c);
fact = factt1 / (factb1 * factb2);
printf("%d ", fact);
}
printf("\n\n");
}
}
|
![]() |
|
|
|||
|
|||
|
|
| Thread Tools | |
| Display Modes | |
|
|