tags:

views:

1187

answers:

5

I am a beginner in C and want to create a program using for loop which gives the following output:

* * * * * * *
* * *   * * *
* *       * *
*           *
+3  A: 

How far have you got with it yourself? Do you know how to write a for loop and print statement? What output do you have so far?

Programming is (partly) about giving your best shot at solving a problem, however roughly or incompletely, then considering what you've been able to do and refining your code incrementally. So unless I know where you've got to already, I risk spoiling the learning experience by revealing to much.

How about showing us your code so far?

Ollie G
+6  A: 

Here's a cheap version, it does the job but doesn't offer much flexibility.

#include <stdio.h>

int main (int argc, char *argv[])
{
    char *strings[4] = { "* * * * * * *",
                         "* * *   * * *",
                         "* *       * *",
                         "*           *" };
    int i;

    for (i = 0; i < 4; i++)
    {
        puts (strings[i]);
    }

    return 0;
}
dreamlax
+1, although it would be nice to have `strings` declared as an array of pointers to *constant* `char`.
avakar
+5  A: 

You're apparently doing this as homework. Here is the solution. But the point of the homework is that you need to understand how this works, what it's doing, etc. Just copying my code won't help you learn.

There are two loops here. The loop using r is for the rows. The loop using c is for the columns. There are 7 columns which are either stars or spaces. On the first row, they're all stars. On the second row, column 3 is a space (bear in mind we're numbering from zero). This is achieved because on the second row, r is 1. The if condition is therefore if (c>3-1 && c<3+1), or if (c>2 && c<4) - so when c is 3, a space will be printed. For the other columns, a star is printed. On the next row, it ends up as if (c>1 && c<5) - so if c is 2, 3 or 4, spaces get printed.

Try playing around with the program, changing the numbers, changing what gets printed in order to get a good understanding of what it's doing.

#include <stdio.h>

int main(void)
{
    int r,c;
    for (r=0; r!=4; r++)
    {
        for (c=0; c<7; c++)
        {
            if (c>3-r && c<3+r)
                printf("  ");
            else
                printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Jon Bright
In my humble opinion, if you want someone to learn, you shouldn't give them code. +1 for good code and explanation, but -1 for giving him the answer. So +/-0 in total.
Chris Lutz
Whoever downvoted: what's your issue with my answer? It compiles and works, it's flexible and it attempts to help with actually understanding what's going on. If there's something I missed, a comment would be more helpful than a downvote...
Jon Bright
Chris, I did hesitate - but I couldn't come up with a way of describing what he needs to do without actually providing the code in question. Hence, giving the code, but also explaining that just copying it isn't the point.
Jon Bright
I probably would have found some pages explaining the syntax of `for` loops and using strings, and maybe given an outline. However, I do agree that the downvote was unnecessary.
Chris Lutz
I don't think you should provide full answers to homework questions when the person asking the question is clearly not going to make any effort to try to answer the question themselves. Obviously I'm in the minority here, but I can't remove my vote now, apparantly it is "too old".
1800 INFORMATION
1800, that's true - but I'm not sure that it's clear he won't make the effort. Asking the question doesn't mean you're going to blindly accept the answer. I personally find it easiest to learn if I have some example code around to learn from and understand. Thanks for the explanation, though - I don't mind the downvote so much as not knowing what's behind it. Now I know...
Jon Bright
As Chris said, +1 for good explanation and -1 for giving the answer. (-: Only thing I'd add is enforcing the usual loop idiom by making the outer loop for (r=0; r<4; r++) {
Rob Wells
+1  A: 

Are you stuck with the algorithm or how to implement it in C? The algorithm looks like this:

Let `n` be the width of the rectange
Print n stars followed by a newline
For i from n/2 to 1 (counting down in steps of 1):
    Print i stars, then n-2*i spaces, then i stars, then a newline

To implement this in C you need a for-loop and some output function like printf.

sepp2k
You mixed up `i` and `x` in your algorithm.
Chris Lutz
Fixed it. Thanks.
sepp2k
+12  A: 

I like to write one-liners to solve simple problems like this. ;)

for(int i=0;i<60;i++)putchar(" *\r\n"[i%15>=11?i%15-11:abs(i%15-6)>(i/15)*2-1&&i%15%2==0?1:0]);
Guffa
+1 LOL xD
Johannes Schaub - litb
Wow, a downvote. Exactly one year later. I suppose that the assignments are used every year, and this year's students come looking for a solution to their homework...
Guffa
And another downvote two months later. You are very, very late in handing in your first assignments for the year...
Guffa