views:

525

answers:

5

Hello Everyone!

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

+2  A: 

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.

danben
+2  A: 

Just a hint:

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

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

anon
A: 

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.

tommieb75
A: 

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.

Secure
A: 

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;
    }
}
geekGod
It seems like I was wanting to over nest everything, Thanks to everyone.
Craig
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
@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.
geekGod
For a learner at Craig's level, usually there is no such thing as "self-describing code".
Secure
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)?
danben
@danben, the requirement is to only use while loop.of course the while ((hat %2) ==0) loop can more efficiently be implemented as an if statement but what's the fun if there are no constraints.
geekGod
There is nothing in the program statement that says "only while loops". The only constraint is on the output statements.
danben