Join 136,560 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,909 people online right now. Registration is fast and FREE... Join Now!
int main(void) { int nb_of_lines, i, j, n; int triangle[i][j];
printf("Enter the number of lines (a strictly positive number): "); scanf("%>=1d", &nb_of_lines);
n = nb_of_lines;
/* Evaluate each component in a triangle based on the number of lines entered. * * Store the values in a two-dimensional array. */ for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { if (j == 0 || j == 1) triangle[i][j] = 1; else triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } }
/* Display the triangle with numbers stored in the array elements. */ for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { printf("%d ", triangle[i][j]); } } system("PAUSE"); return EXIT_SUCCESS; }
then it shows like the attached file Doc1.doc. kindly to advise what's the problem? as i have run a virus scan, scan disk, system check disk and defragmenting the whole hard drive. but still appear like the attached file.
besides that, if i can run the program successfully, let say i enter 5 for the number of lines, the output should appear like 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
how do i make the output become like the following triangle, 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 sorry after i space or tab it still look like the above, any way this one is actually look like a triangle tree.
This post has been edited by kckc314: 11 Apr, 2008 - 09:44 PM
Attached File(s) Doc1.doc ( 107.5k )
Number of downloads: 11
The error is a 'stack overflow'. I'll work on making it stop doing that.
edit: To avoid this error is the future decalre and intialize all variables that will eventually be used in an array. See here:
cpp
int nb_of_lines=0, i=0, j=0, n; int triangle[i][j];
However the program doesn't work still, but thats a start.
can anyone help me to check my program as i get unexpected results after i run it. as far as i concern, i don't see any problem in it. but i need expert advise.
int main(void) { int nb_of_lines = 0, i = 0, j = 0, k = 0, l = 0, n; int triangle[i][j];
printf("Enter the number of lines (a strictly positive number): "); scanf("%d", &nb_of_lines);
n = nb_of_lines;
/* Evaluate each component in a triangle based on the number of lines entered. * * Store the values in a two-dimensional array. */ for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { if (j == 0 || i == 1) triangle[i][j] = 1; else triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } }
/* Display the triangle with numbers stored in the array elements. */ for (i = 0; i < n; i++) { k = n - i - 1; for (l = 0; l <= k; l++) { /* insert spaces*/ printf(" "); } for (j = 0; j <= i; j++) { printf("%d ", triangle[i][j]); } } system("PAUSE"); return EXIT_SUCCESS; }
when you run it to enter 5, the expected result should be like this: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 please advise. thanks in advance.
I just picked zero to illustrate that one has to initialize variables before using them as array sizes. The user could define i and j as well through additional input.
int nb_of_lines = 0, i = 0, j = 0, k = 0, l = 0, n; int triangle[i][j];
This is not valid. you need to provide a size for the 2-d array.
try this
CODE
int nb_of_lines = 0, i = 0, j = 0, k = 0, l = 0, n;
// you have to give a size to your subscripts or else // create this 2-d array dynamically. int triangle[100][100] = { {0}, {0} };
I changed it to that and got it to work with your code
however, the output on screen shows like this: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
As the desired output should be like the one in attached file. i have coded to be appear that way but it doesn't work out. kindly advise. thanks in advance.
Attached File(s) 1.doc ( 23.5k )
Number of downloads: 6
int main(void) { int nb_of_lines=0, i=0, j=0, k = 0, l = 0, n; int triangle[100][100] = { {0}, {0} };
printf("Enter the number of lines (a strictly positive number): "); scanf("%d", &nb_of_lines);
n = nb_of_lines;
/* Evaluate each component in a triangle based on the number of lines entered. * * Store the values in a two-dimensional array. */ for (i = 0; i < n; i++) { for (j = 0; j <= i; j++) { if (j == 0 || i == 1) triangle[i][j] = 1; else triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } }
/* Display the triangle with numbers stored in the array elements. */ for (i = 0; i < n; i++) { k = n - i - 1; printf("\n"); for (l = 0; l <= k; l++) { /* insert spaces*/ printf(" "); } for (j = 0; j <= i; j++) { printf("%d ", triangle[i][j]); } } system("PAUSE"); return EXIT_SUCCESS; }
This gives the desired result
Notice the
cpp
printf("\n");
that was added inside the first for loop, that is the only change to the previous code
This post has been edited by KYA: 12 Apr, 2008 - 07:16 PM