0

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.

5 Answers 5

2

Just a hint:

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

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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.

Comments

0

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.

Comments

-1

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;
    }
}

7 Comments

It seems like I was wanting to over nest everything, Thanks to everyone.
Is giving a complete solution without comment really a good thing for a homework question? @Craig, this may work, but do you understand what's wrong with your own non-working solutions?
@Secure, Do you really need comment for such self-describing code? Also by the time i was adding code , people had already provided comments with the problem.
For a learner at Craig's level, usually there is no such thing as "self-describing code".
As long as we're writing entire solutions, this one isn't even very good. Why do you need a while loop for a statement that will only increment once? Why do you need an extra counter to keep track of whether the row is even-numbered? Why do you increment hat just to later assign it to the value of row (other than to get out of that ill-conceived loop)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.