Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C++ troubles

  1. Nov 14, 2006 #1
    Hey,
    Im pretty new to c++ so my knowledge is limited.
    I have been set an assignment to create the following triangles only using Do...While loops. I'm having difficulty trying to code it.
    The underscores are just their to illustrate spaces.

    1 *_________________5 * 5__________* * * * 1 * ____3 *
    __2 * *_____________* 4 *___________* * * 2 *______2 * *
    ____3 * * *______3 * 3 * 3 * 3________* * 3 * ______1 * * *
    __4 * * * *______* 2 * 2 * 2 * ________* 4 * _____4 * * * *
    5 * * * * *___1 * 1 * 1 * 1 * 1 * 1______5 *____5 * * * * *

    I have split the shapes into two, with the top half being 3 lines and the bottom half the remaining 2. However, as fas as i can tell this only works for the first shape.

    #include <stdio.h>

    void main()
    {
    // Declaring the variables
    int row,space,number,x, number1, number2;

    // Inserting numbers into variables
    row=1;
    number=1;
    number1=1;
    number2=1;

    do // Top of shapes
    {
    space=1; // First Triangle
    do
    {
    printf(" ");
    space++;
    }
    while (space<=row);


    printf("%d", number);
    number++;


    x=1;
    do
    {
    printf("* ");
    x++;
    }
    while (x<=row);


    space=3;
    do
    {
    printf(" ");
    space--;
    }
    while (space>=row);






    space=5; // Second Triangle
    do
    {
    printf(" ");
    space--;
    }
    while (space>=row);

    x=1;
    do
    {
    printf("%d * ", number1);
    x++;
    }
    while (x<=row);

    printf("%d", number1);
    number1++;

    space=5;
    do
    {
    printf(" ");
    space--;
    }
    while (space>=row);



    printf("\t");




    space=1; // Third Triangle
    do
    {
    printf(" ");
    space++;
    }
    while (space<=row);

    x=5;
    do
    {
    printf("* ");
    x--;
    }
    while (x>=row+1);

    printf("%d *", number2);
    number2++;






    printf("\n");
    row++;
    }
    while (row<=3);




    do // Bottom Of shape
    {
    space=5; // First Shape
    do
    {
    printf(" ");
    space--;
    }
    while(space>=row);

    printf("%d", number);
    number++;


    x=1;
    do
    {
    printf("* ");
    x++;
    }
    while (x<=row);


    space=1;
    do
    {
    printf(" ");
    space++;
    }
    while (space<=1);



    space=5; // Second Triangle
    do
    {
    printf(" ");
    space--;
    }
    while (space>=row);

    x=1;
    do
    {
    printf("%d * ", number1);
    x++;
    }
    while (x<=row);

    printf("%d", number1);
    number1++;

    space=5;
    do
    {
    printf(" ");
    space--;
    }
    while (space>=row);


    printf("\t");


    space=1; // Third Triangle
    do
    {
    printf(" ");
    space++;
    }
    while (space<=row);


    printf("* ");
    printf("%d *", number2);
    number2++;


    printf("\n");
    row++;
    }
    while (row<=5);
    }
     
  2. jcsd
  3. Nov 21, 2006 #2
    It would help if you showed what output that gave you. It is kind of hard working with uncommented, unspaced code. Try using the
    Code (Text):
     
    command? I think they have that here.
     
  4. Dec 15, 2006 #3
    why to write such a big progarm.let me say you in a simple method using for loops.
    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int i,n,j;
    clrscr();
    printf("enter the elements");
    for(i=0;i<=n;i++)
    {
    for(j=0;j<=n;j++)
    {
    printf("%d",j);
    }
    printf("\n");
    }
    getch();
    }
    if you will declare i in place of ("%d",i) you will get the reverse triangle
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook