Jump to content

Drawing diamond shaped pattern with C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
Hello,
I'm new in C language, nowadays I'm just solving the basic problems of C. I couldn't find a way to solve this problem. Could someone show me a way to solve this problem? I don't want C code. Only a simple guide for solving.
Thanks for help.

-- The program will get the height of diamond from keyboard and it will print a diamond
according to this height. If entered value is 9 output will be
    *    
   * *
  *   *
 *     *
  *   *
   * *
    *

Edited by Irfanerol, 11 May 2008 - 12:55 PM.


#2
DevilsCharm

DevilsCharm

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
Is the only input function from the user one of the height; nothing else?

#3
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
just height.
other lenghts of diamond will be calculated by our function

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Am I missing something? The height of the diamond above appears to be 7.

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
assuming you meant 7 for the input:
(7-1)/2 = 3, the number of spaces before the first/last *
each row decreases/increases the lead space by 1
each row increases/decreases the inner spaces by 2 (with 1 center space inserted as well)
So, your spaces are:
3
2,2+1
1,4+1
0,6+1
1,4+1
2,2+1
3
From that, you should be able to build 2 for loops to draw the diamond. Also, you need to be sure the value entered is odd.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
--please del this post--

Edited by Irfanerol, 12 May 2008 - 01:05 PM.


#7
Irfanerol

Irfanerol

    Newbie

  • Members
  • PipPip
  • 16 posts
Ups, I've written the input wrong. Sorry for that.
Assuming the input 7 is right.
WingedPanther thank you very much for your explanation. I'll start to writting after this post. It'll work. Thank you very very much.

#include <stdio.h>

main()
{
	int h, led, inner, star;
	printf("please enter the height of diamond> ");
	scanf("%d", &h);
	
	for ( led = (h-1)/2 ; led<=0 ; led-- )
		printf(" ");
	for ( star = 1 ; star<=2 ; star++ )
			printf("*");
	for ( inner = 0 ; inner<=h-2 ; inner++ )
			 printf(" ");
	for ( led = (h-1)/2 ; led <=0 ; led--) {
			 printf(" ");
			 printf("\n"); }
	system("pause"); 
	return 0;
}
I couldn't understand what's wrong with this code? I think I did everything true?

Edited by Irfanerol, 13 May 2008 - 03:49 AM.


#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You need to wrap your for loops in a for loop to cycle through the rows.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog