Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

A diamond made of * given below can be easily printed on the screen using some loops.
*
***
*****
*******
*****
***
*

But i have got a question from one of my friend says print the same * diamond using only one loop.

I have tried with many ways such as complex logic and if-else condition but could not print exact shape with single loop and my curiosity :doh: is also increased to know the solution.

can anybody help me with a logic (equation) that can print the diamond with possible 1 loop.

Thanks
vikas
Posted

You can work around the code snippet below:

C++
char szSpaces[] = "                    ";
char szStars[] = "*****************************************";

int iSpaces = 5;
int iStars = 7;

printf("%.*s%.*s\n", iSpaces, szSpaces, iStars, szStars);


Such a format string instruct the printf function to print the first iSpaces characters of the szSpaces string, followed by the first iStars characters of the szStars string.

Note that if the number of required characters is greater than the string length, the full string is printed without any padding.

You can write your function with a simple loop, where on each step you compute the right number of spaces and asterisks to print.
 
Share this answer
 
v3
What code have you written already?

On seeing that I can probably point you in the right direction quite quickly.
 
Share this answer
 
Comments
vikasvds 16-Jul-10 8:10am    
Hi,

Sorry to not to paste code written by me.
I am almost able to get it with one loops but i am not liking too many variables and if-else.

i am unable to think of any single straight logic/equation to do the same

it is below here -
#define ROWS 10
int main(void)
{
int i = 0, j = 0, counter = 0, start_shrinking = 0;
for(i = 0; i = ((ROWS/2)-j))
printf("*");
else
printf(" ");

counter++;
}
}
}

output is not exactly * arranged in diamond shape as given below
_____*
____***
___*****
__*******
_*********
__*******
___*****
____***
_____*

i have used underscore instead of spaces since in HTML all spaces were disappeared.
This is not the homework i am curiously doing it.

Thanks very much
You could technically print it without any loops what so ever. You think about what you're doing with two loops and reorganize the logic without the inner loop.

Sorry, I cannot give you more based on the lack of code and the chance that this is really just homework in disguise.
 
Share this answer
 
Comments
vikasvds 16-Jul-10 8:10am    
Hi,

Sorry to not to paste code written by me.
I am almost able to get it with one loops but i am not liking too many variables and if-else.

i am unable to think of any single straight logic/equation to do the same

it is below here -
#define ROWS 10
int main(void)
{
int i = 0, j = 0, counter = 0, start_shrinking = 0;
for(i = 0; i = ((ROWS/2)-j))
printf("*");
else
printf(" ");

counter++;
}
}
}

output is not exactly * arranged in diamond shape as given below
_____*
____***
___*****
__*******
_*********
__*******
___*****
____***
_____*

i have used underscore instead of spaces since in HTML all spaces were disappeared.
This is not the homework i am curiously doing it.

Thanks very much - vikasvds 1 hour ago
Just print it on the screen using one write to the console - it's only hello_world in disguise. The only time you'll need a loop is if you're doing something that changes every run of the program and is set up by the program's users.

Cheers,

Ash

PS: As other people have already pointed out solutions how about...

int main()
{
    puts( "    *  \n"
          "  ***  \n"
          "  *****\n"
          "*******\n"
          "  *****\n"
          "  ***  \n"
          "    *  \n" );
}


The only time you'd need a loop is if the requirement for the code said you needed to print a user defined height and width for the shape.
 
Share this answer
 
v2
Try this:

C++
#include <stdio.h>
#include <stdlib.h>

const char szSpaces[] = "                                        ";
const char szStars[] = "********************************************************************************";

int main(int argc, char* argv[])
{
   if (argc != 2) return 1;

   int iSize = atoi(argv[1]);
   if (iSize < 0 || iSize > _countof(szStars)) return 2;

   for(int iRow = 0, iSpaces = iSize / 2; iRow < iSize; iRow++, iSpaces--)
   {
      printf("%.*s%.*s\n", abs(iSpaces), szSpaces, iSize - 2 * abs(iSpaces), szStars);
   }

   return 0;
}


it does what you are asking for; it gets the size of your "diamond" from the command line...
 
Share this answer
 
Hi all,

Sorry to not to paste code written by me.
I am almost able to get it with one loops but i am not liking too many variables and if-else.

i am unable to think of any single straight logic/equation to do the same

it is below here -
C++
#define ROWS 10
int main(void)
{
   int i = 0, j = 0, counter = 0, start_shrinking = 0;
   for(i = 0; i <= ROWS*ROWS; i++)
   {
      if(i != 0 && i%ROWS == 0)
      {
         printf("\n");
         if(j < (((ROWS)/2)-1) && 0 == start_shrinking )
         {
            j++;
         }
         else
         {
            j--;
            start_shrinking = 1;
         }
         counter = 0;
      }
      else
      {
         if(counter <= ((ROWS/2)+j) && counter >= ((ROWS/2)-j))
            printf("*");
         else
            printf(" ");

         counter++;
      }
   }
}


output is not exactly * arranged in diamond shape as given below
_____*
____***
___*****
__*******
_*********
__*******
___*****
____***
_____*

i have used underscore instead of spaces since in HTML all spaces were disappeared.
This is not the homework i am curiously doing it.

Thanks very much
 
Share this answer
 
v2
Comments
Sandeep Mewara 16-Jul-10 5:00am    
Reason for my vote of 1
Not an answer. Should have used the 'Improve Question' link to update the question.
Sauro Viti 16-Jul-10 5:03am    
Use <PRE> tags to improve code readability
vikasvds 16-Jul-10 6:30am    
Hi all,

Sorry to not to paste code written by me.
I am almost able to get it with one loops but i am not liking too many variables and if-else.

i am unable to think of any single straight logic/equation to do the same

it is below here -
#define ROWS 10
int main(void)
{
int i = 0, j = 0, counter = 0, start_shrinking = 0;
for(i = 0; i <= ROWS*ROWS; i++)
{
if(i != 0 && i%ROWS == 0)
{
printf("
");
if(j < (((ROWS)/2)-1) && 0 == start_shrinking )
{
j++;
}
else
{
j--;
start_shrinking = 1;
}
counter = 0;
}
else
{
if(counter <= ((ROWS/2)+j) && counter >= ((ROWS/2)-j))
printf("*");
else
printf(" ");

counter++;
}
}
}

output is not exactly * arranged in diamond shape as given below
_____*
____***
___*****
__*******
_*********
__*******
___*****
____***
_____*

i have used underscore instead of spaces since in HTML all spaces were disappeared.
This is not the homework i am curiously doing it.

Thanks very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900