Print star ('*') diamond in C with nested

2019-02-16 02:27发布

I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.

enter image description here

I have a code that works to make a diamond for user input of 5 but won't work for all odd number inputs..

 half = (size/2)+1;

 for (a=1; a <=  half ; a++) /*top to mid row of diamond*/
   {
     for (b=a; b<half;b++)
       {
     printf(" ");
       }
     for (c= size -2* a; c <=  half; c++)
       {
     printf("*");
       } 
      printf("\n");
   }
 for (a = 1; a < half;a++)
   {
     for (b = a; b>0;b--)
       {
     printf(" ");
       }
     for (c = size-2*a; c >0 ;c--)
       {
     printf("*");
       }
     printf("\n");
   }


  return 0;
}

Any help would be greatly appreciated.Thank you.

Mike

11条回答
甜甜的少女心
2楼-- · 2019-02-16 03:07

I struggled with this a bit. Also sorry, my final code is C++, not strict C, as I haven't used C in 10 years. Here's a more wordy explanation for those trying to understand how to work this out for yourself. Before I begin, one thing you can do to help yourself out is start with a declaration like:

int numStars = 1, numSpaces = 10;

numSpaces doesn't really matter as long as it's small enough to fit on your screen... you can plug in any number and test it later. Then, work out your algorithm, and later on you can work on handling input.

Also, the number of spaces AFTER the stars is irrelevant for this problem.

Then, solve the problem of how to output the top portion of the diamond. I used a counter loop to print spaces until I'd printed the number of spaces for the first row, then a second loop (not nested) to print the number of stars needed for that row. We know that in row 1, we need to print a space 10 times and a star one time.

Once the row has the number of spaces and stars required, you go to the newline ("\n" or endl). But the next row you need to print 2 more stars and 1 less spaces. So you must increment/decrement appropriately.

When you get to the row before the middle row, you'll stop. In other words, the top loop needs to continue while numSpaces is greater than 0. At the end of thefinal pass in the loop, numSpaces is decremented to 0, but it won't print the line with 0 spaces and n stars (2 stars for every space you started with). That happens in your next loop...

Then, you can run the second loop that reverses your increment/decrement at the end, but is otherwise almost the same. Only this time, it should print 0 spaces and 10n stars. Then the newline, and finally decrease the numStars by 2 and increase numSpaces by 1.

If you're not using functions yet or not allowed to in implementation, that's just about it. You could easily call a function, though, passing it numSpaces/numStars, for each of the loops to eliminate duplicate code.

Here's my code in C++:

#include <iostream>

using namespace std;

int main()
{
    int     numStars = 1,
            numSpaces = 10;

    while (numSpaces > 0)
    {
        for (int i = 0; i < numSpaces; i++)
        {
            cout << " ";
        }
        for (int i = 0; i < numStars; i++)
        {
            cout << "*";
        }
        cout << "\n";

        numStars += 2;
        numSpaces--;
    }
    while (numStars > 0)
    {
        for (int i = 0; i < numSpaces; i++)
        {
            cout << " ";
        }
        for (int i = 0; i < numStars; i++)
        {
            cout << "*";
        }
        cout << "\n";
        numStars -= 2;
        numSpaces++;
    }

    return 0;
}
查看更多
该账号已被封号
3楼-- · 2019-02-16 03:08

here the code with matlab

clear all, clc
n=input('Input an Odd Number ');
for i=1:(n+1)/2
    for m=1:(n+1)/2-i
        fprintf(' ');
    end
    for j=1:(2*i)-1
        fprintf('*');
    end
    fprintf('\n');
end
for k=i-1:-1:1
    for m=1:(n+1)/2-k
        fprintf(' ');
    end
    for j=1:(2*k)-1
        fprintf('*');
    end
    fprintf('\n');
end
查看更多
相关推荐>>
4楼-- · 2019-02-16 03:11

I don't want to give you the code, you should learn.

The input number will be the width of the diamond, so make sure to make a relation between the spaces and asterisks that does not surpass this number.

The main idea would be to start at 1 asterisk, and increment by two each time until the input number is reached.

Also, do something to prevent an error if the user enters an even number.

查看更多
一纸荒年 Trace。
5楼-- · 2019-02-16 03:15
#include<stdio.h>
main()
{
int i,j,k,n;
scanf("%d",&n);
n=(n+1)/2;
    for(i=1;i<=n;i++)
    {
        for(j=n;j>=i;j--)
            printf(" ");
        for(k=1;k<=(2*i-1);k++)
            printf("*");
        printf("\n");
    }
    for(i=1;i<=(n-1);i++)
    {
        for(j=0;j<=i;j++)
            printf(" ");
        for(k=(2*n-3);k>=(2*i-1);k--)
            printf("*");
        printf("\n");
}
}
查看更多
迷人小祖宗
6楼-- · 2019-02-16 03:17

Hi i got to best solution for this problem in less no of lines

int b=0;
for(int a=0;b<=50;a=(b<=25) ? a+2 : a-2 ){
    b+=2;
    for(int b=25-a;b>0;b-=2){
        printf(" ");
    }
    for(int c=0;c<=a;c++){
        printf("*");
    }
    printf("\n");
}
查看更多
爷的心禁止访问
7楼-- · 2019-02-16 03:18

Output a diamond in C by asterisk sign

#include<stdio.h>
main()
{
int n, i, j;

printf("Enter a number: ");
scanf("%d",&n);

/* Create up arrow. */
for(i=1; i<=n; i++)
{   /* Left side. */
    for(j=i; j<=n+1; j++)
    {
        printf(" ");

    }
    for(j=1; j<=1; j++)
    {
        printf("*");
    }
    /* Middle. */
    for(j=1; j<i-1; j++)
    {
        printf(" ");

    }

    /* Right side. */
    for(j=1; j<i; j++)
    {
        printf(" ");

    }
    for(j=1; j<=1; j++)
    {
        if(i!=n-(n-1))
            printf("*");
    }
    printf("\n");
}
/* Create down arrow. */
for(i=1; i<=n; i++)
{   /* Left side. */
    for(j=1; j<=i+2; j++)
    {
        printf(" ");

    }
    for(j=1; j<=1; j++)
    {
        if(i!=n)
        printf("*");
    }
    /* Middle. */
    for(j=i; j<n-2; j++)
    {
        printf(" ");

    }
    /* Right side. */
    for(j=i; j<n-1; j++)
    {
        printf(" ");

    }
    for(j=1; j<=1; j++)
    {
        if(i<n-1)
        printf("*");
    }
    printf("\n");
}
}
查看更多
登录 后发表回答