Craig
Craig

Reputation: 3

Printing a Pattern with While loops and only using three output statements in C

I have an assignment for school that is really got the best of me.

Here is the question:

(2) Write a C program using while loop(s) in combination with only the following three output statements (Each one appearing ONLY ONCE in your program): printf("* "); printf("\n"); printf(“^“); to print the pattern:

* * * * * * * * ^
* * * * * * * *
* * * * * * * * ^
* * * * * * * *
* * * * * * * * ^
* * * * * * * *

Note: there is a space between each * and the first, third, and fifth lines have a space before the ^.

And here is my code:

#include<stdio.h>

int main () {

 int star = 0;
 int row = 1;
 int hat = 1;

 while(row < 6) {

  printf(" *");
  star++; 

  while(star > 8) {

   while( (hat % 2) == 1) {

    printf(" ^"); 
   }

   printf("\n");
   row++;
  }
 }
    return 0; 
}

I've tried many different versions of this code, and most of them ended up with infinitely printing rows of *.

If anyone could help it would be great as I've tried and tried at this for a while now and even though I wish I could keep trying deadlines are deadlines and they always seem to come too fast.

Thanks

EDIT:

Rev.2 of the code:

include<stdio.h>

int main () {
int star = 0;
int row = 1;
int hat = 0;

while(row <= 6) {

    printf(" *");
    star++; 

    while(star >= 8) {
        hat++;  

        if( (hat % 2) == 1) {
            printf(" ^");
            hat++;  
        }

        printf("\n");
        row++;
        star = 0;
    }
}
return 0;   
}

Hopefully I am terminating the loops correctly but It seems not to be working. I'm not asking for a "get out of jail free" card but any are welcomed.

Upvotes: 0

Views: 10494

Answers (5)

rajeshnair
rajeshnair

Reputation: 1673

My answer is here

#include<stdio.h>

int main() {
    int row = 0;
    int hat = 0;
    while(row < 6) {
            int col = 0;
            while(col < 8) {
              printf("* ");
              col++;
            }
            while( (hat % 2) == 0) {
              printf("^");
              hat++;
            }
            printf("\n");
            row++;
            hat = row;
    }
}

Upvotes: -1

Secure
Secure

Reputation: 4378

When you have an infinite while-loop, then check your loops for this pattern:

var = inital value;
while (var has not exitcondition)
{
    ....
    var = modify value;
}

The usual cause for an infinite loop is that the modification of var is not inside the loop, so var will never reach the exitcondition.

Upvotes: 0

danben
danben

Reputation: 83270

The first problem is in this block:

 while(star > 8) {

   while( (hat % 2) == 1) {

    printf(" ^"); 
   }

   printf("\n");
   row++;
  }

You never reset star when going to the next row.

Also, I don't see you incrementing hat anywhere.

I'm surprised that your stars print infinitely - since hat starts at 1 and does not change inside that while loop, that loop should never terminate.

Another problem - star counts the number of stars you've printed already, right? So you only want 8 stars per row, but waiting until star > 8 will allow you to print 9 per row.

Upvotes: 2

t0mm13b
t0mm13b

Reputation: 34592

hat is fixed, i.e. no incrementing anywhere and star is not being reset each time in the loop...I have highlighted the problematic lines...

while(row < 6) {

  printf(" *");
  star++; 

  while(star > 8) {

   while( (hat % 2) == 1) { // since hat = 1, 1%2 == 1 is true....

    printf(" ^"); 
   }

   printf("\n");
   row++;
  }

  star = 0; // !!!

 }

Hope this helps, Best regards, Tom.

Upvotes: 0

anon
anon

Reputation:

Just a hint:

   while( (hat % 2) == 1) {

should be an if() and should be placed somewhere else in your program.

Upvotes: 2

Related Questions