Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Please help with this program

 
Reply to this topicStart new topic

Please help with this program

kckc314
11 Apr, 2008 - 09:38 PM
Post #1

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

when i run the below code
CODE

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: Prompts the user for a strictly positive integer N and         *
*              displays the top N lines of Pascal's triangle.                 *
*                                                                             *
*                                                        *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdlib.h>

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)
Attached File  Doc1.doc ( 107.5k ) Number of downloads: 11
User is offlineProfile CardPM
+Quote Post

KYA
RE: Please Help With This Program
12 Apr, 2008 - 01:52 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
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.

This post has been edited by KYA: 12 Apr, 2008 - 01:54 AM
User is offlineProfile CardPM
+Quote Post

kckc314
RE: Please Help With This Program
12 Apr, 2008 - 05:36 AM
Post #3

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(KYA @ 12 Apr, 2008 - 02:52 AM) *

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.
CODE

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: Prompts the user for a strictly positive integer N and         *
*              displays the top N lines of Pascal's triangle.                 *
*                                                                             *
*                                                        *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdlib.h>

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.
User is offlineProfile CardPM
+Quote Post

skaoth
RE: Please Help With This Program
12 Apr, 2008 - 09:50 AM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 343



Thanked: 10 times
Dream Kudos: 100
My Contributions
QUOTE

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
User is online!Profile CardPM
+Quote Post

KYA
RE: Please Help With This Program
12 Apr, 2008 - 02:52 PM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

kckc314
RE: Please Help With This Program
12 Apr, 2008 - 05:58 PM
Post #6

D.I.C Head
**

Joined: 5 Apr, 2008
Posts: 128

QUOTE(skaoth @ 12 Apr, 2008 - 10:50 AM) *

QUOTE

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)
Attached File  1.doc ( 23.5k ) Number of downloads: 6
User is offlineProfile CardPM
+Quote Post

KYA
RE: Please Help With This Program
12 Apr, 2008 - 07:15 PM
Post #7

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 4,924



Thanked: 105 times
Dream Kudos: 1200
My Contributions
Now you just need to insert "\n" to go to a new line:

cpp

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Description: Prompts the user for a strictly positive integer N and *
* displays the top N lines of Pascal's triangle. *
* *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdlib.h>

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 smile.gif smile.gif

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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month