Juan Solis
Juan Solis

Reputation: 11

I don't see why this doesn't work

The goal of this code was to print a pyramid. First I print a certain number of spaces then print some stars to eventually make a pyramid.

For example to print a pyramid of 5 first it would print a star after 4 spaces then the start and end variables would be changed so the new start would 3 and the new end would be six and it would print 3 stars.

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

void printSpaces(int num){
    int i;
    for(i=0; i<num;i++)
        {
        printf(" ");
        }
}

void pyramid(int n){
    int start=n,end=n+1;
    int k;
    while(start>0 && end<2*n)
       {
       printSpaces(start);
       for (k=start; k<end;k++)
          { 
          printf("*");
          }
       printf("\n");
       start=n-1;
       end=n+1;
       }
}

int main(void) {
    puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
    pyramid(5);
    return 0;
}

The only thing it seems to be doing printing a row of 2 stars over and over.

Upvotes: 0

Views: 97

Answers (4)

ricdnts
ricdnts

Reputation: 11

the procedure that you are using to decrement the value of the variables end and start are wrong

I found this code about patterns in c in this site

http://www.programmingsimplified.com/c-program-print-stars-pyramid

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

void pyramid(int end){
int k,c;
for (k=1; k< end; k++) {
 printf(" ");
 end= end -1;
 for (c=1; c<= 2 * k-1; c++)
    printf("*"); 
    printf("\n");    
  }
 }

int main() {
  pyramid(15);
  return 0;
} 

Upvotes: 0

user3629249
user3629249

Reputation: 16540

Matt McNabb was very close.

The following code contains the correct fix

The main change is to step 'start' and 'end' rather than re-initializing them to the passed parameter

#include <stdio.h>

#include <stdlib.h>


void printSpaces(int num){
  int i;
  for(i=0; i<num;i++)
    {
    printf(" ");
    }
}


void pyramid(int n){
   int start=n,end=n+1;
   int k;
   while(start>0 && end<2*n)
   {
     printSpaces(start);
     for (k=start; k<end;k++)
      {
       printf("*");
      }
     printf("\n");
     start--;  //<< corrected
     end++;    //<< corrected
  }
}


int main(void) {
  puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
  pyramid(5);
  return 0;
}

Upvotes: 0

Walter Delevich
Walter Delevich

Reputation: 427

you set start to n-1, but the value of n never changes. That means that start will continuously be set to the same value, n-1(4). Same for end, your loop will never terminate.

void pyramid(int n){
   int start=n,end=n+1;
   int k;
   while(start>0 && end<2*n)
   {
     printSpaces(start);
     for (k=start; k<end;k++)
      { 
       printf("*");
      }
     printf("\n");
     start=n-1;
     end=n+1;
  }
}

Also, on first invocation, k will be 4 and end will be 6, hence two stars.

Upvotes: 1

Benjamin Linn
Benjamin Linn

Reputation: 16

Your problem is right here: start=n-1; end=n+1;

it should be something like start = start + 1 ?

I don't understand your program perfectly but I can tell that this is your error.

Upvotes: 0

Related Questions